Skip to content

The open alternative to Salesforce, designed for AI.

Distribute orphan Recall cleanup jobs across workspaces (#25114)

nitine9598abe8d04
diff --git a/packages/twenty-apps/public/call-recorder/package.json b/packages/twenty-apps/public/call-recorder/package.json
index 17b9a51305b..c652decec9e 100644
--- a/packages/twenty-apps/public/call-recorder/package.json
+++ b/packages/twenty-apps/public/call-recorder/package.json
@@ -1,6 +1,6 @@
{
"name": "@twentyhq/call-recorder",
- "version": "1.10.0",
+ "version": "1.11.0",
"license": "MIT",
"engines": {
"node": "^24.5.0",
diff --git a/packages/twenty-apps/public/call-recorder/src/constants/enqueue-orphaned-recall-bot-cleanup-logic-function-universal-identifier.ts b/packages/twenty-apps/public/call-recorder/src/constants/enqueue-orphaned-recall-bot-cleanup-logic-function-universal-identifier.ts
new file mode 100644
index 00000000000..b5785d46878
--- /dev/null
+++ b/packages/twenty-apps/public/call-recorder/src/constants/enqueue-orphaned-recall-bot-cleanup-logic-function-universal-identifier.ts
@@ -0,0 +1,2 @@
+export const ENQUEUE_ORPHANED_RECALL_BOT_CLEANUP_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER =
+ '455458ce-bbc3-4b39-a3f2-950540c9eda7';
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/cleanup-orphaned-recall-bots.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/cleanup-orphaned-recall-bots.ts
index df55e067c80..53c5019987b 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/cleanup-orphaned-recall-bots.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/cleanup-orphaned-recall-bots.ts
@@ -2,7 +2,6 @@ import { CoreApiClient } from 'twenty-client-sdk/core';
import { defineLogicFunction } from 'twenty-sdk/define';
import { CLEANUP_ORPHANED_RECALL_BOTS_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER } from 'src/constants/cleanup-orphaned-recall-bots-logic-function-universal-identifier';
-import { CLEANUP_ORPHANED_RECALL_BOTS_CRON_PATTERN } from 'src/logic-functions/constants/cleanup-orphaned-recall-bots-cron-pattern';
import {
cleanupOrphanedRecallBots,
type CleanupOrphanedRecallBotsResult,
@@ -41,10 +40,7 @@ export default defineLogicFunction({
CLEANUP_ORPHANED_RECALL_BOTS_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER,
name: 'cleanup-orphaned-recall-bots',
description:
- 'Daily cleanup that lists workspace Recall bots and cancels those no CallRecording request claims.',
+ 'Lists workspace Recall bots and cancels those no CallRecording request claims.',
timeoutSeconds: 250,
handler: cleanupOrphanedRecallBotsHandler,
- cronTriggerSettings: {
- pattern: CLEANUP_ORPHANED_RECALL_BOTS_CRON_PATTERN,
- },
});
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/constants/orphaned-recall-bot-cleanup-distribution-window-ms.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/constants/orphaned-recall-bot-cleanup-distribution-window-ms.ts
new file mode 100644
index 00000000000..75a1a7fdc30
--- /dev/null
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/constants/orphaned-recall-bot-cleanup-distribution-window-ms.ts
@@ -0,0 +1,2 @@
+export const ORPHANED_RECALL_BOT_CLEANUP_DISTRIBUTION_WINDOW_MS =
+ 60 * 60 * 1000;
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/__tests__/compute-orphaned-recall-bot-cleanup-delay.test.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/__tests__/compute-orphaned-recall-bot-cleanup-delay.test.ts
new file mode 100644
index 00000000000..bbe52085134
--- /dev/null
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/__tests__/compute-orphaned-recall-bot-cleanup-delay.test.ts
@@ -0,0 +1,13 @@
+import { describe, expect, it } from 'vitest';
+
+import { computeOrphanedRecallBotCleanupDelay } from 'src/logic-functions/domain/compute-orphaned-recall-bot-cleanup-delay.util';
+
+describe('computeOrphanedRecallBotCleanupDelay', () => {
+ it('returns the same delay for the same workspace', () => {
+ const workspaceId = '20202020-1111-4444-8888-303030303030';
+
+ expect(computeOrphanedRecallBotCleanupDelay(workspaceId)).toBe(
+ computeOrphanedRecallBotCleanupDelay(workspaceId),
+ );
+ });
+});
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/compute-orphaned-recall-bot-cleanup-delay.util.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/compute-orphaned-recall-bot-cleanup-delay.util.ts
new file mode 100644
index 00000000000..a1ed5ff25d0
--- /dev/null
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/compute-orphaned-recall-bot-cleanup-delay.util.ts
@@ -0,0 +1,9 @@
+import { createHash } from 'crypto';
+
+import { ORPHANED_RECALL_BOT_CLEANUP_DISTRIBUTION_WINDOW_MS } from 'src/logic-functions/constants/orphaned-recall-bot-cleanup-distribution-window-ms';
+
+export const computeOrphanedRecallBotCleanupDelay = (
+ workspaceId: string,
+): number =>
+ createHash('sha256').update(workspaceId).digest().readUInt32BE(0) %
+ ORPHANED_RECALL_BOT_CLEANUP_DISTRIBUTION_WINDOW_MS;
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/enqueue-orphaned-recall-bot-cleanup.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/enqueue-orphaned-recall-bot-cleanup.ts
new file mode 100644
index 00000000000..d876abb426f
--- /dev/null
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/enqueue-orphaned-recall-bot-cleanup.ts
@@ -0,0 +1,53 @@
+import {
+ enqueueJobs,
+ type LogicFunctionExecutionContext,
+} from 'twenty-sdk/logic-function';
+import { defineLogicFunction } from 'twenty-sdk/define';
+
+import { CLEANUP_ORPHANED_RECALL_BOTS_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER } from 'src/constants/cleanup-orphaned-recall-bots-logic-function-universal-identifier';
+import { ENQUEUE_ORPHANED_RECALL_BOT_CLEANUP_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER } from 'src/constants/enqueue-orphaned-recall-bot-cleanup-logic-function-universal-identifier';
+import { CLEANUP_ORPHANED_RECALL_BOTS_CRON_PATTERN } from 'src/logic-functions/constants/cleanup-orphaned-recall-bots-cron-pattern';
+import { ENQUEUED_JOB_RETRY_LIMIT } from 'src/logic-functions/constants/enqueued-job-retry-limit';
+import { computeOrphanedRecallBotCleanupDelay } from 'src/logic-functions/domain/compute-orphaned-recall-bot-cleanup-delay.util';
+import { buildRetryableStepFailure } from 'src/logic-functions/utils/build-step-failure.util';
+
+type EnqueueOrphanedRecallBotCleanupResult = {
+ delayMs: number;
+};
+
+export const enqueueOrphanedRecallBotCleanupHandler = async (
+ _payload: unknown,
+ { workspaceId }: LogicFunctionExecutionContext,
+): Promise<EnqueueOrphanedRecallBotCleanupResult> => {
+ const delayMs = computeOrphanedRecallBotCleanupDelay(workspaceId);
+
+ try {
+ await enqueueJobs({
+ logicFunctionUniversalIdentifier:
+ CLEANUP_ORPHANED_RECALL_BOTS_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER,
+ payloads: [{}],
+ retryLimit: ENQUEUED_JOB_RETRY_LIMIT,
+ delayMs,
+ });
+ } catch (error) {
+ throw buildRetryableStepFailure(
+ 'orphaned Recall bot cleanup enqueueing',
+ error,
+ );
+ }
+
+ return { delayMs };
+};
+
+export default defineLogicFunction({
+ universalIdentifier:
+ ENQUEUE_ORPHANED_RECALL_BOT_CLEANUP_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER,
+ name: 'enqueue-orphaned-recall-bot-cleanup',
+ description:
+ 'Enqueues orphaned Recall bot cleanup at a stable workspace-specific delay to distribute Recall API traffic.',
+ timeoutSeconds: 30,
+ handler: enqueueOrphanedRecallBotCleanupHandler,
+ cronTriggerSettings: {
+ pattern: CLEANUP_ORPHANED_RECALL_BOTS_CRON_PATTERN,
+ },
+});