Skip to content

The open alternative to Salesforce, designed for AI.

Return job handles from enqueueJob(s) and add a getJobs query (#25204)

martmullcda35be5b0ad
diff --git a/.github/workflows/ci-breaking-changes.yaml b/.github/workflows/ci-breaking-changes.yaml
index 0106b960a18..eb2073c6bbc 100644
--- a/.github/workflows/ci-breaking-changes.yaml
+++ b/.github/workflows/ci-breaking-changes.yaml
@@ -248,6 +248,10 @@ jobs:
# Move current branch files to temp directory
mv current-* /tmp/current-branch-files/ 2>/dev/null || echo "No current-* files to preserve"
+ # Both servers must be introspected with the same query, and checking
+ # out main would otherwise swap in main's copy of it
+ cp packages/twenty-utils/graphql-introspection-query.graphql /tmp/graphql-introspection-query.graphql
+
echo "Preserved current branch files for later restoration"
- name: Stop current branch server
@@ -370,8 +374,8 @@ jobs:
# Read admin token from shared test tokens file (single source of truth)
ADMIN_TOKEN=$(jq -r '.APPLE_JANE_ADMIN_ACCESS_TOKEN' packages/twenty-server/test/integration/constants/test-tokens.json)
- # Load introspection query from file
- INTROSPECTION_QUERY=$(cat packages/twenty-utils/graphql-introspection-query.graphql)
+ # Load introspection query preserved from the current branch
+ INTROSPECTION_QUERY=$(cat /tmp/graphql-introspection-query.graphql)
# Prepare the query payload
QUERY_PAYLOAD=$(echo "$INTROSPECTION_QUERY" | tr '\n' ' ' | sed 's/"/\\"/g')
diff --git a/packages/twenty-client-sdk/scripts/generate-metadata-client.ts b/packages/twenty-client-sdk/scripts/generate-metadata-client.ts
index 3d0166f6452..b6e93b751e0 100644
--- a/packages/twenty-client-sdk/scripts/generate-metadata-client.ts
+++ b/packages/twenty-client-sdk/scripts/generate-metadata-client.ts
@@ -21,7 +21,12 @@ const introspectSchema = async (url: string): Promise<string> => {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ query: getIntrospectionQuery() }),
+ // Deprecated input fields are omitted from introspection by default, which
+ // would drop them from the client and break callers a deprecation is meant
+ // to keep working
+ body: JSON.stringify({
+ query: getIntrospectionQuery({ inputValueDeprecation: true }),
+ }),
});
const json = await response.json();
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
index 187031453c3..25ab105688b 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
@@ -3208,12 +3208,35 @@ type WorkspaceAiStats {
type EnqueueJobResult {
enqueued: Boolean!
logicFunctionUniversalIdentifier: String!
+ jobId: String!
}
type EnqueueJobsResult {
enqueued: Boolean!
logicFunctionUniversalIdentifier: String!
enqueuedJobsCount: Int!
+ jobIds: [String!]!
+}
+
+type JobStatus {
+ jobId: String!
+ state: JobState!
+ attemptsMade: Int!
+ failedReason: String
+ enqueuedAt: Float!
+ startedAt: Float
+ finishedAt: Float
+}
+
+"""Job state in the queue"""
+enum JobState {
+ COMPLETED
+ FAILED
+ ACTIVE
+ WAITING
+ DELAYED
+ PRIORITIZED
+ WAITING_CHILDREN
}
type AppKeyValue {
@@ -3521,6 +3544,7 @@ type Query {
myCalendarChannels(connectedAccountId: UUID): [CalendarChannel!]!
minimalMetadata: MinimalMetadata!
appKeyValue(key: String!, scope: AppKeyValueScope = WORKSPACE): AppKeyValue
+ getJobs(jobIds: [String!]!): [JobStatus!]!
appConnections(filter: ListAppConnectionsInput): [AppConnection!]!
appConnection(id: ID!): AppConnection!
findWorkspaceAiStats: WorkspaceAiStats!
@@ -5022,17 +5046,24 @@ input SetAppKeyValueInput {
input EnqueueJobInput {
logicFunctionUniversalIdentifier: String!
payload: JSON
+ jobId: String
retryLimit: Int
delayMs: Int
}
input EnqueueJobsInput {
logicFunctionUniversalIdentifier: String!
- payloads: [JSON!]!
+ payloads: [JSON!] @deprecated(reason: "Use jobs instead.")
+ jobs: [EnqueueJobItemInput!]
retryLimit: Int
delayMs: Int
}
+input EnqueueJobItemInput {
+ payload: JSON
+ jobId: String
+}
+
input FileAttachmentInput {
id: UUID!
filename: String!
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.ts b/packages/twenty-client-sdk/src/metadata/generated/schema.ts
index fa67443329d..23f9e879342 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/schema.ts
+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.ts
@@ -2841,6 +2841,7 @@ export interface WorkspaceAiStats {
export interface EnqueueJobResult {
enqueued: Scalars['Boolean']
logicFunctionUniversalIdentifier: Scalars['String']
+ jobId: Scalars['String']
__typename: 'EnqueueJobResult'
}
@@ -2848,9 +2849,25 @@ export interface EnqueueJobsResult {
enqueued: Scalars['Boolean']
logicFunctionUniversalIdentifier: Scalars['String']
enqueuedJobsCount: Scalars['Int']
+ jobIds: Scalars['String'][]
__typename: 'EnqueueJobsResult'
}
+export interface JobStatus {
+ jobId: Scalars['String']
+ state: JobState
+ attemptsMade: Scalars['Int']
+ failedReason?: Scalars['String']
+ enqueuedAt: Scalars['Float']
+ startedAt?: Scalars['Float']
+ finishedAt?: Scalars['Float']
+ __typename: 'JobStatus'
+}
+
+
+/** Job state in the queue */
+export type JobState = 'COMPLETED' | 'FAILED' | 'ACTIVE' | 'WAITING' | 'DELAYED' | 'PRIORITIZED' | 'WAITING_CHILDREN'
+
export interface AppKeyValue {
key: Scalars['String']
value?: Scalars['JSON']
@@ -3082,6 +3099,7 @@ export interface Query {
myCalendarChannels: CalendarChannel[]
minimalMetadata: MinimalMetadata
appKeyValue?: AppKeyValue
+ getJobs: JobStatus[]
appConnections: AppConnection[]
appConnection: AppConnection
findWorkspaceAiStats: WorkspaceAiStats
@@ -6362,6 +6380,7 @@ export interface WorkspaceAiStatsGenqlSelection{
export interface EnqueueJobResultGenqlSelection{
enqueued?: boolean | number
logicFunctionUniversalIdentifier?: boolean | number
+ jobId?: boolean | number
__typename?: boolean | number
__scalar?: boolean | number
}
@@ -6370,6 +6389,19 @@ export interface EnqueueJobsResultGenqlSelection{
enqueued?: boolean | number
logicFunctionUniversalIdentifier?: boolean | number
enqueuedJobsCount?: boolean | number
+ jobIds?: boolean | number
+ __typename?: boolean | number
+ __scalar?: boolean | number
+}
+
+export interface JobStatusGenqlSelection{
+ jobId?: boolean | number
+ state?: boolean | number
+ attemptsMade?: boolean | number
+ failedReason?: boolean | number
+ enqueuedAt?: boolean | number
+ startedAt?: boolean | number
+ finishedAt?: boolean | number
__typename?: boolean | number
__scalar?: boolean | number
}
@@ -6608,6 +6640,7 @@ export interface QueryGenqlSelection{
myCalendarChannels?: (CalendarChannelGenqlSelection & { __args?: {connectedAccountId?: (Scalars['UUID'] | null)} })
minimalMetadata?: MinimalMetadataGenqlSelection
appKeyValue?: (AppKeyValueGenqlSelection & { __args: {key: Scalars['String'], scope?: (AppKeyValueScope | null)} })
+ getJobs?: (JobStatusGenqlSelection & { __args: {jobIds: Scalars['String'][]} })
appConnections?: (AppConnectionGenqlSelection & { __args?: {filter?: (ListAppConnectionsInput | null)} })
appConnection?: (AppConnectionGenqlSelection & { __args: {id: Scalars['ID']} })
findWorkspaceAiStats?: WorkspaceAiStatsGenqlSelection
@@ -7307,9 +7340,11 @@ export interface UpdateCalendarChannelInputUpdates {visibility?: (CalendarChanne
export interface SetAppKeyValueInput {key: Scalars['String'],value?: (Scalars['JSON'] | null),scope?: (AppKeyValueScope | null)}
-export interface EnqueueJobInput {logicFunctionUniversalIdentifier: Scalars['String'],payload?: (Scalars['JSON'] | null),retryLimit?: (Scalars['Int'] | null),delayMs?: (Scalars['Int'] | null)}
+export interface EnqueueJobInput {logicFunctionUniversalIdentifier: Scalars['String'],payload?: (Scalars['JSON'] | null),jobId?: (Scalars['String'] | null),retryLimit?: (Scalars['Int'] | null),delayMs?: (Scalars['Int'] | null)}
-export interface EnqueueJobsInput {logicFunctionUniversalIdentifier: Scalars['String'],payloads: Scalars['JSON'][],retryLimit?: (Scalars['Int'] | null),delayMs?: (Scalars['Int'] | null)}
+export interface EnqueueJobsInput {logicFunctionUniversalIdentifier: Scalars['String'],payloads?: (Scalars['JSON'][] | null),jobs?: (EnqueueJobItemInput[] | null),retryLimit?: (Scalars['Int'] | null),delayMs?: (Scalars['Int'] | null)}
+
+export interface EnqueueJobItemInput {payload?: (Scalars['JSON'] | null),jobId?: (Scalars['String'] | null)}
export interface FileAttachmentInput {id: Scalars['UUID'],filename: Scalars['String']}
@@ -9513,6 +9548,14 @@ export interface LogicFunctionLogsInput {applicationId?: (Scalars['UUID'] | null
+ const JobStatus_possibleTypes: string[] = ['JobStatus']
+ export const isJobStatus = (obj?: { __typename?: any } | null): obj is JobStatus => {
+ if (!obj?.__typename) throw new Error('__typename is missing in "isJobStatus"')
+ return JobStatus_possibleTypes.includes(obj.__typename)
+ }
+
+
+
const AppKeyValue_possibleTypes: string[] = ['AppKeyValue']
export const isAppKeyValue = (obj?: { __typename?: any } | null): obj is AppKeyValue => {
if (!obj?.__typename) throw new Error('__typename is missing in "isAppKeyValue"')
@@ -10330,6 +10373,16 @@ export const enumWorkspaceSetupChatOutcome = {
UNAVAILABLE: 'UNAVAILABLE' as const
}
+export const enumJobState = {
+ COMPLETED: 'COMPLETED' as const,
+ FAILED: 'FAILED' as const,
+ ACTIVE: 'ACTIVE' as const,
+ WAITING: 'WAITING' as const,
+ DELAYED: 'DELAYED' as const,
+ PRIORITIZED: 'PRIORITIZED' as const,
+ WAITING_CHILDREN: 'WAITING_CHILDREN' as const
+}
+
export const enumAppKeyValueScope = {
WORKSPACE: 'WORKSPACE' as const,
SERVER: 'SERVER' as const
diff --git a/packages/twenty-client-sdk/src/metadata/generated/types.ts b/packages/twenty-client-sdk/src/metadata/generated/types.ts
index 1c99761d934..3096945fc2e 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/types.ts
+++ b/packages/twenty-client-sdk/src/metadata/generated/types.ts
@@ -88,16 +88,17 @@ export default {
354,
361,
363,
- 364,
365,
366,
+ 367,
368,
370,
- 375,
- 390,
- 402,
- 517,
- 541
+ 372,
+ 377,
+ 392,
+ 404,
+ 519,
+ 544
],
"types": {
"BillingProductDTO": {
@@ -6358,6 +6359,9 @@ export default {
"logicFunctionUniversalIdentifier": [
1
],
+ "jobId": [
+ 1
+ ],
"__typename": [
1
]
@@ -6372,10 +6376,40 @@ export default {
"enqueuedJobsCount": [
26
],
+ "jobIds": [
+ 1
+ ],
+ "__typename": [
+ 1
+ ]
+ },
+ "JobStatus": {
+ "jobId": [
+ 1
+ ],
+ "state": [
+ 361
+ ],
+ "attemptsMade": [
+ 26
+ ],
+ "failedReason": [
+ 1
+ ],
+ "enqueuedAt": [
+ 15
+ ],
+ "startedAt": [
+ 15
+ ],
+ "finishedAt": [
+ 15
+ ],
"__typename": [
1
]
},
+ "JobState": {},
"AppKeyValue": {
"key": [
1
@@ -6384,7 +6418,7 @@ export default {
9
],
"scope": [
- 361
+ 363
],
"__typename": [
1
@@ -6399,19 +6433,19 @@ export default {
1
],
"syncStatus": [
- 363
+ 365
],
"syncStage": [
- 364
+ 366
],
"visibility": [
- 365
+ 367
],
"isContactAutoCreationEnabled": [
8
],
"contactAutoCreationPolicy": [
- 366
+ 368
],
"isSyncEnabled": [
8
@@ -6462,7 +6496,7 @@ export default {
1
],
"pendingSyncAction": [
- 368
+ 370
],
"messageChannelId": [
3
@@ -6504,7 +6538,7 @@ export default {
1
],
"provenance": [
- 370
+ 372
],
"__typename": [
1
@@ -6533,7 +6567,7 @@ export default {
3
],
"through": [
- 371
+ 373
],
"__typename": [
1
@@ -6553,7 +6587,7 @@ export default {
1
],
"emit": [
- 372
+ 374
],
"action": [
1
@@ -6588,7 +6622,7 @@ export default {
},
"CollectionHash": {
"collectionName": [
- 375
+ 377
],
"hash": [
1
@@ -6652,13 +6686,13 @@ export default {
},
"MinimalMetadata": {
"objectMetadataItems": [
- 376
+ 378
],
"views": [
- 377
+ 379
],
"collectionHashes": [
- 374
+ 376
],
"__typename": [
1
@@ -6718,7 +6752,7 @@ export default {
174,
{
"input": [
- 380
+ 382
]
}
],
@@ -6843,7 +6877,7 @@ export default {
2,
{
"input": [
- 381,
+ 383,
"GetApiKeyInput!"
]
}
@@ -6952,7 +6986,7 @@ export default {
10,
{
"input": [
- 382,
+ 384,
"AgentIdInput!"
]
}
@@ -6965,7 +6999,7 @@ export default {
"CursorPaging!"
],
"filter": [
- 383,
+ 385,
"ObjectFilter!"
]
}
@@ -6995,7 +7029,7 @@ export default {
21,
{
"input": [
- 384,
+ 386,
"LogicFunctionIdInput!"
]
}
@@ -7007,7 +7041,7 @@ export default {
9,
{
"input": [
- 384,
+ 386,
"LogicFunctionIdInput!"
]
}
@@ -7016,7 +7050,7 @@ export default {
1,
{
"input": [
- 384,
+ 386,
"LogicFunctionIdInput!"
]
}
@@ -7238,7 +7272,7 @@ export default {
308,
{
"input": [
- 385,
+ 387,
"PreviewMessageCampaignAudienceInput!"
]
}
@@ -7247,7 +7281,7 @@ export default {
315,
{
"input": [
- 386,
+ 388,
"FindMessageSuppressionsInput!"
]
}
@@ -7291,7 +7325,7 @@ export default {
}
],
"myMessageFolders": [
- 367,
+ 369,
{
"messageChannelId": [
3
@@ -7299,7 +7333,7 @@ export default {
}
],
"myCalendarChannels": [
- 362,
+ 364,
{
"connectedAccountId": [
3
@@ -7307,17 +7341,26 @@ export default {
}
],
"minimalMetadata": [
- 378
+ 380
],
"appKeyValue": [
- 360,
+ 362,
{
"key": [
1,
"String!"
],
"scope": [
- 361
+ 363
+ ]
+ }
+ ],
+ "getJobs": [
+ 360,
+ {
+ "jobIds": [
+ 1,
+ "[String!]!"
]
}
],
@@ -7325,7 +7368,7 @@ export default {
217,
{
"filter": [
- 387
+ 389
]
}
],
@@ -7396,13 +7439,13 @@ export default {
}
],
"timelineActivityTypes": [
- 373
+ 375
],
"metadataTranslations": [
- 369,
+ 371,
{
"input": [
- 388,
+ 390,
"MetadataTranslationsInput!"
]
}
@@ -7468,7 +7511,7 @@ export default {
343,
{
"input": [
- 389,
+ 391,
"EventLogQueryInput!"
]
}
@@ -7477,7 +7520,7 @@ export default {
337,
{
"input": [
- 393,
+ 395,
"PieChartDataInput!"
]
}
@@ -7486,7 +7529,7 @@ export default {
335,
{
"input": [
- 394,
+ 396,
"LineChartDataInput!"
]
}
@@ -7495,7 +7538,7 @@ export default {
332,
{
"input": [
- 395,
+ 397,
"BarChartDataInput!"
]
}
@@ -7595,10 +7638,10 @@ export default {
},
"ObjectFilter": {
"and": [
- 383
+ 385
],
"or": [
- 383
+ 385
],
"id": [
29
@@ -7700,10 +7743,10 @@ export default {
},
"EventLogQueryInput": {
"table": [
- 390
+ 392
],
"filters": [
- 391
+ 393
],
"first": [
26
@@ -7724,7 +7767,7 @@ export default {
1
],
"dateRange": [
- 392
+ 394
],
"recordId": [
1
@@ -7785,7 +7828,7 @@ export default {
8,
{
"input": [
- 397,
+ 399,
"AddQuerySubscriptionInput!"
]
}
@@ -7794,7 +7837,7 @@ export default {
8,
{
"input": [
- 398,
+ 400,
"RemoveQueryFromEventStreamInput!"
]
}
@@ -7803,7 +7846,7 @@ export default {
155,
{
"inputs": [
- 399,
+ 401,
"[CreateNavigationMenuItemInput!]!"
]
}
@@ -7812,7 +7855,7 @@ export default {
155,
{
"input": [
- 399,
+ 401,
"CreateNavigationMenuItemInput!"
]
}
@@ -7821,7 +7864,7 @@ export default {
155,
{
"inputs": [
- 400,
+ 402,
"[UpdateOneNavigationMenuItemInput!]!"
]
}
@@ -7830,7 +7873,7 @@ export default {
155,
{
"input": [
- 400,
+ 402,
"UpdateOneNavigationMenuItemInput!"
]
}
@@ -7904,7 +7947,7 @@ export default {
152,
{
"file": [
- 402,
+ 404,
"Upload!"
]
}
@@ -7913,7 +7956,7 @@ export default {
152,
{
"file": [
- 402,
+ 404,
"Upload!"
]
}
@@ -7922,7 +7965,7 @@ export default {
152,
{
"file": [
- 402,
+ 404,
"Upload!"
]
}
@@ -7931,7 +7974,7 @@ export default {
152,
{
"file": [
- 402,
+ 404,
"Upload!"
]
}
@@ -7940,7 +7983,7 @@ export default {
152,
{
"file": [
- 402,
+ 404,
"Upload!"
]
}
@@ -7949,7 +7992,7 @@ export default {
152,
{
"file": [
- 402,
+ 404,
"Upload!"
],
"fieldMetadataId": [
@@ -7962,7 +8005,7 @@ export default {
152,
{
"file": [
- 402,
+ 404,
"Upload!"
],
"fieldMetadataUniversalIdentifier": [
@@ -7975,7 +8018,7 @@ export default {
75,
{
"input": [
- 403,
+ 405,
"UpsertUsageLimitInput!"
]
}
@@ -7993,7 +8036,7 @@ export default {
54,
{
"input": [
- 404,
+ 406,
"CreateViewFilterGroupInput!"
]
}
@@ -8006,7 +8049,7 @@ export default {
"String!"
],
"input": [
- 405,
+ 407,
"UpdateViewFilterGroupInput!"
]
}
@@ -8033,7 +8076,7 @@ export default {
56,
{
"input": [
- 406,
+ 408,
"CreateViewFilterInput!"
]
}
@@ -8042,7 +8085,7 @@ export default {
56,
{
"input": [
- 407,
+ 409,
"UpdateViewFilterInput!"
]
}
@@ -8051,7 +8094,7 @@ export default {
56,
{
"input": [
- 409,
+ 411,
"DeleteViewFilterInput!"
]
}
@@ -8060,7 +8103,7 @@ export default {
56,
{
"input": [
- 410,
+ 412,
"DestroyViewFilterInput!"
]
}
@@ -8069,7 +8112,7 @@ export default {
62,
{
"input": [
- 411,
+ 413,
"CreateViewInput!"
]
}
@@ -8082,7 +8125,7 @@ export default {
"String!"
],
"input": [
- 412,
+ 414,
"UpdateViewInput!"
]
}
@@ -8109,7 +8152,7 @@ export default {
62,
{
"input": [
- 413,
+ 415,
"UpsertViewWidgetInput!"
]
}
@@ -8118,7 +8161,7 @@ export default {
59,
{
"input": [
- 419,
+ 421,
"CreateViewSortInput!"
]
}
@@ -8127,7 +8170,7 @@ export default {
59,
{
"input": [
- 420,
+ 422,
"UpdateViewSortInput!"
]
}
@@ -8136,7 +8179,7 @@ export default {
8,
{
"input": [
- 422,
+ 424,
"DeleteViewSortInput!"
]
}
@@ -8145,7 +8188,7 @@ export default {
8,
{
"input": [
- 423,
+ 425,
"DestroyViewSortInput!"
]
}
@@ -8154,7 +8197,7 @@ export default {
52,
{
"input": [
- 424,
+ 426,
"UpdateViewFieldInput!"
]
}
@@ -8163,7 +8206,7 @@ export default {
52,
{
"input": [
- 426,
+ 428,
"CreateViewFieldInput!"
]
}
@@ -8172,7 +8215,7 @@ export default {
52,
{
"inputs": [
- 426,
+ 428,
"[CreateViewFieldInput!]!"
]
}
@@ -8181,7 +8224,7 @@ export default {
52,
{
"input": [
- 427,
+ 429,
"DeleteViewFieldInput!"
]
}
@@ -8190,7 +8233,7 @@ export default {
52,
{
"input": [
- 428,
+ 430,
"DestroyViewFieldInput!"
]
}
@@ -8199,7 +8242,7 @@ export default {
61,
{
"input": [
- 429,
+ 431,
"UpdateViewFieldGroupInput!"
]
}
@@ -8208,7 +8251,7 @@ export default {
61,
{
"input": [
- 431,
+ 433,
"CreateViewFieldGroupInput!"
]
}
@@ -8217,7 +8260,7 @@ export default {
61,
{
"inputs": [
- 431,
+ 433,
"[CreateViewFieldGroupInput!]!"
]
}
@@ -8226,7 +8269,7 @@ export default {
61,
{
"input": [
- 432,
+ 434,
"DeleteViewFieldGroupInput!"
]
}
@@ -8235,7 +8278,7 @@ export default {
61,
{
"input": [
- 433,
+ 435,
"DestroyViewFieldGroupInput!"
]
}
@@ -8244,7 +8287,7 @@ export default {
62,
{
"input": [
- 434,
+ 436,
"UpsertFieldsWidgetInput!"
]
}
@@ -8253,7 +8296,7 @@ export default {
2,
{
"input": [
- 437,
+ 439,
"CreateApiKeyInput!"
]
}
@@ -8262,7 +8305,7 @@ export default {
2,
{
"input": [
- 438,
+ 440,
"UpdateApiKeyInput!"
]
}
@@ -8271,7 +8314,7 @@ export default {
2,
{
"input": [
- 439,
+ 441,
"RevokeApiKeyInput!"
]
}
@@ -8473,7 +8516,7 @@ export default {
175,
{
"input": [
- 440,
+ 442,
"CreateApprovedAccessDomainInput!"
]
}
@@ -8482,7 +8525,7 @@ export default {
8,
{
"input": [
- 441,
+ 443,
"DeleteApprovedAccessDomainInput!"
]
}
@@ -8491,7 +8534,7 @@ export default {
175,
{
"input": [
- 442,
+ 444,
"ValidateApprovedAccessDomainInput!"
]
}
@@ -8500,7 +8543,7 @@ export default {
126,
{
"input": [
- 443,
+ 445,
"CreatePageLayoutTabInput!"
]
}
@@ -8513,7 +8556,7 @@ export default {
"String!"
],
"input": [
- 444,
+ 446,
"UpdatePageLayoutTabInput!"
]
}
@@ -8531,7 +8574,7 @@ export default {
127,
{
"input": [
- 445,
+ 447,
"CreatePageLayoutInput!"
]
}
@@ -8544,7 +8587,7 @@ export default {
"String!"
],
"input": [
- 446,
+ 448,
"UpdatePageLayoutInput!"
]
}
@@ -8566,7 +8609,7 @@ export default {
"String!"
],
"input": [
- 447,
+ 449,
"UpdatePageLayoutWithTabsInput!"
]
}
@@ -8602,7 +8645,7 @@ export default {
83,
{
"input": [
- 450,
+ 452,
"CreatePageLayoutWidgetInput!"
]
}
@@ -8615,7 +8658,7 @@ export default {
"String!"
],
"input": [
- 451,
+ 453,
"UpdatePageLayoutWidgetInput!"
]
}
@@ -8633,7 +8676,7 @@ export default {
10,
{
"input": [
- 452,
+ 454,
"CreateAgentInput!"
]
}
@@ -8642,7 +8685,7 @@ export default {
10,
{
"input": [
- 453,
+ 455,
"UpdateAgentInput!"
]
}
@@ -8651,7 +8694,7 @@ export default {
10,
{
"input": [
- 382,
+ 384,
"AgentIdInput!"
]
}
@@ -8660,7 +8703,7 @@ export default {
23,
{
"input": [
- 454,
+ 456,
"CreateOneObjectInput!"
]
}
@@ -8669,7 +8712,7 @@ export default {
23,
{
"input": [
- 456,
+ 458,
"DeleteOneObjectInput!"
]
}
@@ -8678,7 +8721,7 @@ export default {
23,
{
"input": [
- 457,
+ 459,
"UpdateOneObjectInput!"
]
}
@@ -8687,7 +8730,7 @@ export default {
245,
{
"input": [
- 460,
+ 462,
"CreateOneIndexInput!"
]
}
@@ -8696,7 +8739,7 @@ export default {
245,
{
"input": [
- 463,
+ 465,
"DeleteOneIndexInput!"
]
}
@@ -8705,7 +8748,7 @@ export default {
21,
{
"input": [
- 384,
+ 386,
"LogicFunctionIdInput!"
]
}
@@ -8714,7 +8757,7 @@ export default {
21,
{
"input": [
- 464,
+ 466,
"CreateLogicFunctionFromSourceInput!"
]
}
@@ -8723,7 +8766,7 @@ export default {
148,
{
"input": [
- 465,
+ 467,
"ExecuteOneLogicFunctionInput!"
]
}
@@ -8732,7 +8775,7 @@ export default {
8,
{
"input": [
- 466,
+ 468,
"UpdateLogicFunctionFromSourceInput!"
]
}
@@ -8741,7 +8784,7 @@ export default {
14,
{
"input": [
- 468,
+ 470,
"CreateCommandMenuItemInput!"
]
}
@@ -8750,7 +8793,7 @@ export default {
14,
{
"input": [
- 469,
+ 471,
"UpdateCommandMenuItemInput!"
]
}
@@ -8777,7 +8820,7 @@ export default {
13,
{
"input": [
- 470,
+ 472,
"CreateFrontComponentInput!"
]
}
@@ -8786,7 +8829,7 @@ export default {
13,
{
"input": [
- 471,
+ 473,
"UpdateFrontComponentInput!"
]
}
@@ -8804,7 +8847,7 @@ export default {
68,
{
"data": [
- 473,
+ 475,
"ActivateWorkspaceInput!"
]
}
@@ -8813,7 +8856,7 @@ export default {
68,
{
"data": [
- 474,
+ 476,
"UpdateWorkspaceInput!"
]
}
@@ -8844,7 +8887,7 @@ export default {
214,
{
"input": [
- 475,
+ 477,
"CreateApplicationRegistrationInput!"
]
}
@@ -8853,7 +8896,7 @@ export default {
74,
{
"input": [
- 476,
+ 478,
"UpdateApplicationRegistrationInput!"
]
}
@@ -8880,7 +8923,7 @@ export default {
188,
{
"input": [
- 478,
+ 480,
"CreateApplicationRegistrationVariableInput!"
]
}
@@ -8889,7 +8932,7 @@ export default {
188,
{
"input": [
- 479,
+ 481,
"UpdateApplicationRegistrationVariableInput!"
]
}
@@ -8907,7 +8950,7 @@ export default {
74,
{
"file": [
- 402,
+ 404,
"Upload!"
],
"universalIdentifier": [
@@ -8969,7 +9012,7 @@ export default {
"UUID!"
],
"input": [
- 481,
+ 483,
"UpdateApplicationInput!"
]
}
@@ -8990,7 +9033,7 @@ export default {
237,
{
"input": [
- 482,
+ 484,
"CreateOneFieldMetadataInput!"
]
}
@@ -8999,7 +9042,7 @@ export default {
237,
{
"input": [
- 484,
+ 486,
"UpdateOneFieldMetadataInput!"
]
}
@@ -9008,7 +9051,7 @@ export default {
237,
{
"input": [
- 486,
+ 488,
"DeleteOneFieldInput!"
]
}
@@ -9017,7 +9060,7 @@ export default {
58,
{
"input": [
- 487,
+ 489,
"CreateViewGroupInput!"
]
}
@@ -9026,7 +9069,7 @@ export default {
58,
{
"inputs": [
- 487,
+ 489,
"[CreateViewGroupInput!]!"
]
}
@@ -9035,7 +9078,7 @@ export default {
58,
{
"input": [
- 488,
+ 490,
"UpdateViewGroupInput!"
]
}
@@ -9044,7 +9087,7 @@ export default {
58,
{
"inputs": [
- 488,
+ 490,
"[UpdateViewGroupInput!]!"
]
}
@@ -9053,7 +9096,7 @@ export default {
58,
{
"input": [
- 490,
+ 492,
"DeleteViewGroupInput!"
]
}
@@ -9062,7 +9105,7 @@ export default {
58,
{
"input": [
- 491,
+ 493,
"DestroyViewGroupInput!"
]
}
@@ -9084,7 +9127,7 @@ export default {
46,
{
"createRoleInput": [
- 492,
+ 494,
"CreateRoleInput!"
]
}
@@ -9093,7 +9136,7 @@ export default {
46,
{
"updateRoleInput": [
- 493,
+ 495,
"UpdateRoleInput!"
]
}
@@ -9111,7 +9154,7 @@ export default {
43,
{
"upsertObjectPermissionsInput": [
- 495,
+ 497,
"UpsertObjectPermissionsInput!"
]
}
@@ -9120,7 +9163,7 @@ export default {
44,
{
"upsertPermissionFlagsInput": [
- 497,
+ 499,
"UpsertPermissionFlagsInput!"
]
}
@@ -9129,7 +9172,7 @@ export default {
38,
{
"upsertFieldPermissionsInput": [
- 498,
+ 500,
"UpsertFieldPermissionsInput!"
]
}
@@ -9138,7 +9181,7 @@ export default {
258,
{
"input": [
- 500,
+ 502,
"UpsertRowLevelPermissionPredicatesInput!"
]
}
@@ -9169,7 +9212,7 @@ export default {
310,
{
"input": [
- 503,
+ 505,
"SendEmailViaDomainInput!"
]
}
@@ -9178,7 +9221,7 @@ export default {
311,
{
"input": [
- 504,
+ 506,
"SendMessageCampaignInput!"
]
}
@@ -9187,7 +9230,7 @@ export default {
309,
{
"input": [
- 505,
+ 507,
"CancelMessageCampaignInput!"
]
}
@@ -9196,7 +9239,7 @@ export default {
310,
{
"input": [
- 506,
+ 508,
"SendMessageCampaignTestInput!"
]
}
@@ -9205,7 +9248,7 @@ export default {
312,
{
"input": [
- 507,
+ 509,
"CreateMessageSuppressionInput!"
]
}
@@ -9223,7 +9266,7 @@ export default {
316,
{
"input": [
- 508,
+ 510,
"CreateUnsubscribeTopicInput!"
]
}
@@ -9232,7 +9275,7 @@ export default {
316,
{
"input": [
- 509,
+ 511,
"UpdateUnsubscribeTopicInput!"
]
}
@@ -9250,7 +9293,7 @@ export default {
299,
{
"input": [
- 510,
+ 512,
"UpdateMessageChannelInput!"
]
}
@@ -9259,7 +9302,7 @@ export default {
307,
{
"input": [
- 512,
+ 514,
"CreateEmailGroupChannelInput!"
]
}
@@ -9268,7 +9311,7 @@ export default {
299,
{
"input": [
- 513,
+ 515,
"UpdateEmailGroupChannelInput!"
]
}
@@ -9286,7 +9329,7 @@ export default {
295,
{
"input": [
- 514,
+ 516,
"CreateEmailingDomainInput!"
]
}
@@ -9313,7 +9356,7 @@ export default {
328,
{
"input": [
- 515,
+ 517,
"RunAgentInput!"
]
}
@@ -9322,7 +9365,7 @@ export default {
325,
{
"input": [
- 518,
+ 520,
"CreateWebhookInput!"
]
}
@@ -9331,7 +9374,7 @@ export default {
325,
{
"input": [
- 519,
+ 521,
"UpdateWebhookInput!"
]
}
@@ -9346,37 +9389,37 @@ export default {
}
],
"updateMessageFolder": [
- 367,
+ 369,
{
"input": [
- 521,
+ 523,
"UpdateMessageFolderInput!"
]
}
],
"updateMessageFolders": [
- 367,
+ 369,
{
"input": [
- 523,
+ 525,
"UpdateMessageFoldersInput!"
]
}
],
"updateCalendarChannel": [
- 362,
+ 364,
{
"input": [
- 524,
+ 526,
"UpdateCalendarChannelInput!"
]
}
],
"setAppKeyValue": [
- 360,
+ 362,
{
"input": [
- 526,
+ 528,
"SetAppKeyValueInput!"
]
}
@@ -9389,7 +9432,7 @@ export default {
"String!"
],
"scope": [
- 361
+ 363
]
}
],
@@ -9397,7 +9440,7 @@ export default {
358,
{
"input": [
- 527,
+ 529,
"EnqueueJobInput!"
]
}
@@ -9406,7 +9449,7 @@ export default {
359,
{
"input": [
- 528,
+ 530,
"EnqueueJobsInput!"
]
}
@@ -9436,7 +9479,7 @@ export default {
1
],
"fileAttachments": [
- 529,
+ 532,
"[FileAttachmentInput!]"
]
}
@@ -9465,14 +9508,14 @@ export default {
"UUID!"
],
"answers": [
- 530,
+ 533,
"[AgentChatQuestionAnswerInput!]!"
],
"modelId": [
1
],
"fileAttachments": [
- 529,
+ 532,
"[FileAttachmentInput!]"
]
}
@@ -9550,7 +9593,7 @@ export default {
344,
{
"input": [
- 531,
+ 534,
"CreateSkillInput!"
]
}
@@ -9559,7 +9602,7 @@ export default {
344,
{
"input": [
- 532,
+ 535,
"UpdateSkillInput!"
]
}
@@ -9614,16 +9657,16 @@ export default {
}
],
"updateTimelineActivityType": [
- 373,
+ 375,
{
"input": [
- 533,
+ 536,
"UpdateTimelineActivityTypeInput!"
]
}
],
"resetTimelineActivityType": [
- 373,
+ 375,
{
"id": [
3,
@@ -9635,7 +9678,7 @@ export default {
267,
{
"input": [
- 534,
+ 537,
"GetAuthorizationUrlForSSOInput!"
]
}
@@ -9801,7 +9844,7 @@ export default {
270,
{
"input": [
- 535
+ 538
]
}
],
@@ -9813,7 +9856,7 @@ export default {
"String!"
],
"file": [
- 402,
+ 404,
"Upload!"
]
}
@@ -9979,7 +10022,7 @@ export default {
8,
{
"input": [
- 536,
+ 539,
"UpdateWorkspaceMemberSettingsInput!"
]
}
@@ -10013,7 +10056,7 @@ export default {
224,
{
"input": [
- 537,
+ 540,
"SetupOIDCSsoInput!"
]
}
@@ -10022,7 +10065,7 @@ export default {
224,
{
"input": [
- 538,
+ 541,
"SetupSAMLSsoInput!"
]
}
@@ -10031,7 +10074,7 @@ export default {
220,
{
"input": [
- 539,
+ 542,
"DeleteSsoInput!"
]
}
@@ -10040,7 +10083,7 @@ export default {
221,
{
"input": [
- 540,
+ 543,
"EditSsoInput!"
]
}
@@ -10069,7 +10112,7 @@ export default {
340,
{
"type": [
- 541,
+ 544,
"AnalyticsType!"
],
"name": [
@@ -10112,7 +10155,7 @@ export default {
330,
{
"input": [
- 542,
+ 545,
"CreateCalendarEventInput!"
]
}
@@ -10121,7 +10164,7 @@ export default {
339,
{
"input": [
- 543,
+ 546,
"SendEmailInput!"
]
}
@@ -10143,7 +10186,7 @@ export default {
"String!"
],
"connectionParameters": [
- 545,
+ 548,
"EmailAccountConnectionParameters!"
],
"id": [
@@ -10155,7 +10198,7 @@ export default {
185,
{
"input": [
- 547,
+ 550,
"UpdateLabPublicFeatureFlagInput!"
]
}
@@ -10220,7 +10263,7 @@ export default {
284,
{
"file": [
- 402,
+ 404,
"Upload!"
],
"applicationUniversalIdentifier": [
@@ -10245,7 +10288,7 @@ export default {
"String!"
],
"files": [
- 548,
+ 551,
"[ApplicationFileUploadRequestInput!]!"
]
}
@@ -10368,7 +10411,7 @@ export default {
3
],
"update": [
- 401
+ 403
],
"__typename": [
1
@@ -10507,7 +10550,7 @@ export default {
3
],
"update": [
- 408
+ 410
],
"__typename": [
1
@@ -10678,19 +10721,19 @@ export default {
3
],
"view": [
- 414
+ 416
],
"viewFields": [
- 415
+ 417
],
"viewFilters": [
- 416
+ 418
],
"viewFilterGroups": [
- 417
+ 419
],
"viewSorts": [
- 418
+ 420
],
"__typename": [
1
@@ -10839,7 +10882,7 @@ export default {
3
],
"update": [
- 421
+ 423
],
"__typename": [
1
@@ -10877,7 +10920,7 @@ export default {
3
],
"update": [
- 425
+ 427
],
"__typename": [
1
@@ -10953,7 +10996,7 @@ export default {
3
],
"update": [
- 430
+ 432
],
"__typename": [
1
@@ -11017,10 +11060,10 @@ export default {
3
],
"groups": [
- 435
+ 437
],
"fields": [
- 436
+ 438
],
"__typename": [
1
@@ -11040,7 +11083,7 @@ export default {
8
],
"fields": [
- 436
+ 438
],
"__typename": [
1
@@ -11211,7 +11254,7 @@ export default {
8
],
"tabs": [
- 448
+ 450
],
"__typename": [
1
@@ -11234,7 +11277,7 @@ export default {
87
],
"widgets": [
- 449
+ 451
],
"__typename": [
1
@@ -11399,7 +11442,7 @@ export default {
},
"CreateOneObjectInput": {
"object": [
- 455
+ 457
],
"__typename": [
1
@@ -11459,7 +11502,7 @@ export default {
},
"UpdateOneObjectInput": {
"update": [
- 458
+ 460
],
"id": [
3
@@ -11512,7 +11555,7 @@ export default {
24
],
"translations": [
- 459
+ 461
],
"__typename": [
1
@@ -11534,7 +11577,7 @@ export default {
},
"CreateOneIndexInput": {
"index": [
- 461
+ 463
],
"__typename": [
1
@@ -11545,7 +11588,7 @@ export default {
3
],
"fields": [
- 462
+ 464
],
"indexType": [
246
@@ -11630,7 +11673,7 @@ export default {
3
],
"update": [
- 467
+ 469
],
"__typename": [
1
@@ -11793,7 +11836,7 @@ export default {
3
],
"update": [
- 472
+ 474
],
"__typename": [
1
@@ -11920,7 +11963,7 @@ export default {
1
],
"update": [
- 477
+ 479
],
"__typename": [
1
@@ -11974,7 +12017,7 @@ export default {
1
],
"update": [
- 480
+ 482
],
"__typename": [
1
@@ -12004,7 +12047,7 @@ export default {
},
"CreateOneFieldMetadataInput": {
"field": [
- 483
+ 485
],
"__typename": [
1
@@ -12077,7 +12120,7 @@ export default {
3
],
"update": [
- 485
+ 487
],
"__typename": [
1
@@ -12136,7 +12179,7 @@ export default {
9
],
"translations": [
- 459
+ 461
],
"__typename": [
1
@@ -12175,7 +12218,7 @@ export default {
3
],
"update": [
- 489
+ 491
],
"__typename": [
1
@@ -12260,7 +12303,7 @@ export default {
},
"UpdateRoleInput": {
"update": [
- 494
+ 496
],
"id": [
3
@@ -12315,7 +12358,7 @@ export default {
3
],
"objectPermissions": [
- 496
+ 498
],
"__typename": [
1
@@ -12357,7 +12400,7 @@ export default {
3
],
"fieldPermissions": [
- 499
+ 501
],
"__typename": [
1
@@ -12388,10 +12431,10 @@ export default {
3
],
"predicates": [
- 501
+ 503
],
"predicateGroups": [
- 502
+ 504
],
"__typename": [
1
@@ -12564,7 +12607,7 @@ export default {
3
],
"update": [
- 511
+ 513
],
"__typename": [
1
@@ -12637,7 +12680,7 @@ export default {
3
],
"messages": [
- 516
+ 518
],
"__typename": [
1
@@ -12645,7 +12688,7 @@ export default {
},
"RunAgentMessageInput": {
"role": [
- 517
+ 519
],
"content": [
1
@@ -12680,7 +12723,7 @@ export default {
3
],
"update": [
- 520
+ 522
],
"__typename": [
1
@@ -12708,7 +12751,7 @@ export default {
3
],
"update": [
- 522
+ 524
],
"__typename": [
1
@@ -12727,7 +12770,7 @@ export default {
3
],
"update": [
- 522
+ 524
],
"__typename": [
1
@@ -12738,7 +12781,7 @@ export default {
3
],
"update": [
- 525
+ 527
],
"__typename": [
1
@@ -12746,13 +12789,13 @@ export default {
},
"UpdateCalendarChannelInputUpdates": {
"visibility": [
- 365
+ 367
],
"isContactAutoCreationEnabled": [
8
],
"contactAutoCreationPolicy": [
- 366
+ 368
],
"isSyncEnabled": [
8
@@ -12769,7 +12812,7 @@ export default {
9
],
"scope": [
- 361
+ 363
],
"__typename": [
1
@@ -12782,6 +12825,9 @@ export default {
"payload": [
9
],
+ "jobId": [
+ 1
+ ],
"retryLimit": [
26
],
@@ -12799,6 +12845,9 @@ export default {
"payloads": [
9
],
+ "jobs": [
+ 531
+ ],
"retryLimit": [
26
],
@@ -12809,6 +12858,17 @@ export default {
1
]
},
+ "EnqueueJobItemInput": {
+ "payload": [
+ 9
+ ],
+ "jobId": [
+ 1
+ ],
+ "__typename": [
+ 1
+ ]
+ },
"FileAttachmentInput": {
"id": [
3
@@ -12897,7 +12957,7 @@ export default {
8
],
"translations": [
- 459
+ 461
],
"__typename": [
1
@@ -13063,7 +13123,7 @@ export default {
1
],
"files": [
- 544
+ 547
],
"__typename": [
1
@@ -13085,13 +13145,13 @@ export default {
1
],
"IMAP": [
- 546
+ 549
],
"SMTP": [
- 546
+ 549
],
"CALDAV": [
- 546
+ 549
],
"__typename": [
1
@@ -13156,7 +13216,7 @@ export default {
259,
{
"input": [
- 550,
+ 553,
"LogicFunctionLogsInput!"
]
}
@@ -13174,7 +13234,7 @@ export default {
341,
{
"table": [
- 390,
+ 392,
"EventLogTable!"
]
}
diff --git a/packages/twenty-docs/developers/extend/apps/logic/background-jobs.mdx b/packages/twenty-docs/developers/extend/apps/logic/background-jobs.mdx
index 7c5e3f9245b..34e8431184c 100644
--- a/packages/twenty-docs/developers/extend/apps/logic/background-jobs.mdx
+++ b/packages/twenty-docs/developers/extend/apps/logic/background-jobs.mdx
@@ -24,20 +24,70 @@ import { enqueueJobs } from 'twenty-sdk/logic-function';
await enqueueJobs({
logicFunctionUniversalIdentifier: '9f1c3d7e-51b8-4a29-8f0d-7c4e2a6b1d33',
- payloads: [{ page: 1 }],
+ jobs: [{ payload: { page: 1 } }],
});
```
-Each run receives its payload as the handler argument, exactly like any other trigger. The target must belong to the **same application** as the caller — enqueuing another app's function is rejected with `Logic function not found` and nothing is enqueued. A single call accepts up to `200` payloads.
+Each run receives its payload as the handler argument, exactly like any other trigger. The target must belong to the **same application** as the caller — enqueuing another app's function is rejected with `Logic function not found` and nothing is enqueued. A single call accepts up to `200` jobs.
<Note>
-`enqueueJobs` returns as soon as the jobs are accepted, not when they have run. It does not return the targets' results — have each target write what it produces to the [key-value store](/developers/extend/apps/logic/key-value-store) or to a workspace record if you need to read it back.
+`enqueueJobs` returns as soon as the jobs are accepted, not when they have run. It does not return the targets' results — have each target write what it produces to the [key-value store](/developers/extend/apps/logic/key-value-store) or to a workspace record if you need to read it back. To find out whether a run finished, poll its [job status](#poll-jobs).
</Note>
<Note>
-The older `enqueueJob` helper, which enqueues a single job per call, is deprecated. Use `enqueueJobs` with a one-element `payloads` list instead.
+The older `enqueueJob` helper, which enqueues a single job per call, is deprecated. Use `enqueueJobs` with a one-element `jobs` list instead. The `payloads` argument is deprecated too — pass `jobs` instead, which takes the same payload per run and lets you name each one.
</Note>
+## Poll jobs
+
+`enqueueJobs` returns one `jobId` per enqueued run, in the same order as the jobs you passed. `getJobs` reads back where those runs got to, in one call.
+
+```ts
+import { enqueueJobs, getJobs } from 'twenty-sdk/logic-function';
+
+const { jobIds } = await enqueueJobs({
+ logicFunctionUniversalIdentifier: ENRICH_COMPANY,
+ jobs: [{ payload: { companyId: 'abc' } }],
+});
+
+const statuses = await getJobs(jobIds);
+```
+
+A single `getJobs` call reads at most `200` ids, the same ceiling `enqueueJobs` applies to a batch.
+
+Each entry carries the id you asked for:
+
+| Field | What it holds |
+|-------|---------------|
+| `jobId` | The id you polled with. |
+| `state` | `WAITING`, `PRIORITIZED`, `DELAYED`, `ACTIVE`, `WAITING_CHILDREN`, `COMPLETED` or `FAILED`. |
+| `attemptsMade` | Queue attempts made so far, including the current one. |
+| `failedReason` | The error message of the last failed attempt, otherwise `null`. |
+| `enqueuedAt` | When the job was accepted, in epoch milliseconds. |
+| `startedAt` | When a worker picked it up, `null` while it is still waiting. |
+| `finishedAt` | When it completed or failed, `null` while it is still running. |
+
+A job that has not been picked up yet normally reads as `PRIORITIZED` rather than `WAITING`, because application jobs are queued at a fixed low priority. `COMPLETED` and `FAILED` are the terminal states. A poll loop should also give up on its own after a bounded number of attempts rather than waiting forever, since a run is only readable for as long as the queue retains it: 4 hours after completing, 7 days after failing, and in both cases only the most recent 1000 jobs in that state.
+
+`getJobs` returns only the jobs it still holds, so an id that has been evicted — or never existed — is simply absent from the result rather than erroring the whole batch. Compare what you get back against what you asked for to spot those, and treat a missing id as "no longer known", which is not the same answer as "it never ran".
+
+Statuses are scoped to the workspace the call is made from, so one workspace can never read another workspace's job.
+
+## Choose your own job id
+
+Pass `jobs` instead of `payloads` to name each run yourself. The id you pass is the one `getJobs` takes, so a caller can poll a job without having to store what `enqueueJobs` returned.
+
+```ts
+await enqueueJobs({
+ logicFunctionUniversalIdentifier: ENRICH_COMPANY,
+ jobs: [{ payload: { companyId: 'abc' }, jobId: 'enrich-abc' }],
+});
+```
+
+An id is at most 128 characters of letters, digits, `_`, `.` or `-`, and no two jobs in one call may share one. Pass either `payloads` or `jobs`, never both. When you omit `jobId`, Twenty generates one.
+
+A `jobId` is also an idempotency key: enqueuing an id that the queue still holds is accepted but does **not** start a second run, and `getJobs` keeps reporting the first one. This makes a retried enqueue safe. Two caveats: the guarantee only lasts as long as the job is retained, so the same id can start a fresh run once the original has been evicted, and ids are shared by every app in a workspace, so prefix yours if a collision would matter.
+
## Job options
Options apply to every run in the batch.
@@ -50,7 +100,7 @@ Options apply to every run in the batch.
```ts
await enqueueJobs({
logicFunctionUniversalIdentifier: '9f1c3d7e-51b8-4a29-8f0d-7c4e2a6b1d33',
- payloads: [{ page: 1 }],
+ jobs: [{ payload: { page: 1 } }],
retryLimit: 3,
delayMs: 60_000,
});
@@ -114,7 +164,7 @@ const handler = async (params: { cursor?: string }) => {
if (nextCursor) {
await enqueueJobs({
logicFunctionUniversalIdentifier: SYNC_CONTACTS_PAGE,
- payloads: [{ cursor: nextCursor }],
+ jobs: [{ payload: { cursor: nextCursor } }],
delayMs: 2_000,
});
}
@@ -139,7 +189,7 @@ const companies = await listCompaniesToEnrich();
await enqueueJobs({
logicFunctionUniversalIdentifier: ENRICH_COMPANY,
- payloads: companies.map((company) => ({ companyId: company.id })),
+ jobs: companies.map((company) => ({ payload: { companyId: company.id } })),
retryLimit: 2,
});
```
@@ -173,7 +223,7 @@ const handler = async (params: { offset?: number }) => {
if (companies.length === CHUNK_SIZE) {
await enqueueJobs({
logicFunctionUniversalIdentifier: ENRICH_COMPANIES_BATCH,
- payloads: [{ offset: offset + CHUNK_SIZE }],
+ jobs: [{ payload: { offset: offset + CHUNK_SIZE } }],
});
}
diff --git a/packages/twenty-front/src/generated-metadata/graphql.ts b/packages/twenty-front/src/generated-metadata/graphql.ts
index 5faaa12c29e..58168f6bd23 100644
--- a/packages/twenty-front/src/generated-metadata/graphql.ts
+++ b/packages/twenty-front/src/generated-metadata/graphql.ts
@@ -1771,21 +1771,28 @@ export enum EngineComponentKey {
export type EnqueueJobInput = {
delayMs?: InputMaybe<Scalars['Int']['input']>;
+ jobId?: InputMaybe<Scalars['String']['input']>;
logicFunctionUniversalIdentifier: Scalars['String']['input'];
payload?: InputMaybe<Scalars['JSON']['input']>;
retryLimit?: InputMaybe<Scalars['Int']['input']>;
};
+export type EnqueueJobItemInput = {
+ jobId?: InputMaybe<Scalars['String']['input']>;
+ payload?: InputMaybe<Scalars['JSON']['input']>;
+};
+
export type EnqueueJobResult = {
__typename?: 'EnqueueJobResult';
enqueued: Scalars['Boolean']['output'];
+ jobId: Scalars['String']['output'];
logicFunctionUniversalIdentifier: Scalars['String']['output'];
};
export type EnqueueJobsInput = {
delayMs?: InputMaybe<Scalars['Int']['input']>;
+ jobs?: InputMaybe<Array<EnqueueJobItemInput>>;
logicFunctionUniversalIdentifier: Scalars['String']['input'];
- payloads: Array<Scalars['JSON']['input']>;
retryLimit?: InputMaybe<Scalars['Int']['input']>;
};
@@ -1793,6 +1800,7 @@ export type EnqueueJobsResult = {
__typename?: 'EnqueueJobsResult';
enqueued: Scalars['Boolean']['output'];
enqueuedJobsCount: Scalars['Int']['output'];
+ jobIds: Array<Scalars['String']['output']>;
logicFunctionUniversalIdentifier: Scalars['String']['output'];
};
@@ -2280,6 +2288,28 @@ export type InviteSuggestion = {
email: Scalars['String']['output'];
};
+/** Job state in the queue */
+export enum JobState {
+ ACTIVE = 'ACTIVE',
+ COMPLETED = 'COMPLETED',
+ DELAYED = 'DELAYED',
+ FAILED = 'FAILED',
+ PRIORITIZED = 'PRIORITIZED',
+ WAITING = 'WAITING',
+ WAITING_CHILDREN = 'WAITING_CHILDREN'
+}
+
+export type JobStatus = {
+ __typename?: 'JobStatus';
+ attemptsMade: Scalars['Int']['output'];
+ enqueuedAt: Scalars['Float']['output'];
+ failedReason?: Maybe<Scalars['String']['output']>;
+ finishedAt?: Maybe<Scalars['Float']['output']>;
+ jobId: Scalars['String']['output'];
+ startedAt?: Maybe<Scalars['Float']['output']>;
+ state: JobState;
+};
+
export type LineChartConfiguration = {
__typename?: 'LineChartConfiguration';
aggregateFieldMetadataId: Scalars['UUID']['output'];
@@ -4798,6 +4828,7 @@ export type Query = {
getConnectedImapSmtpCaldavAccount: ConnectedImapSmtpCaldavAccount;
getEmailingDomains: Array<EmailingDomain>;
getInviteSuggestions: Array<InviteSuggestion>;
+ getJobs: Array<JobStatus>;
getLogicFunctionSourceCode?: Maybe<Scalars['String']['output']>;
getPageLayout?: Maybe<PageLayout>;
getPageLayoutTab: PageLayoutTab;
@@ -5070,6 +5101,11 @@ export type QueryGetConnectedImapSmtpCaldavAccountArgs = {
};
+export type QueryGetJobsArgs = {
+ jobIds: Array<Scalars['String']['input']>;
+};
+
+
export type QueryGetLogicFunctionSourceCodeArgs = {
input: LogicFunctionIdInput;
};
diff --git a/packages/twenty-sdk/src/sdk/logic-function/index.ts b/packages/twenty-sdk/src/sdk/logic-function/index.ts
index 8342dabad0c..6a4e9cef88d 100644
--- a/packages/twenty-sdk/src/sdk/logic-function/index.ts
+++ b/packages/twenty-sdk/src/sdk/logic-function/index.ts
@@ -62,12 +62,16 @@ export type { RunAgentInput, RunAgentResult } from 'twenty-shared/application';
export { enqueueJob } from '@/sdk/logic-function/jobs/enqueue-job';
export { enqueueJobs } from '@/sdk/logic-function/jobs/enqueue-jobs';
+export { getJobs } from '@/sdk/logic-function/jobs/get-jobs';
export type {
EnqueueJobInput,
+ EnqueueJobItem,
EnqueueJobOptions,
EnqueueJobResult,
EnqueueJobsInput,
EnqueueJobsResult,
+ JobStatusResult,
+ JobStatusState,
} from 'twenty-shared/application';
export { createTimelineActivity } from '@/sdk/logic-function/timeline/create-timeline-activity';
diff --git a/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/enqueue-job.spec.ts b/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/enqueue-job.spec.ts
index 3baf2842e24..09671e2a97b 100644
--- a/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/enqueue-job.spec.ts
+++ b/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/enqueue-job.spec.ts
@@ -9,6 +9,7 @@ import {
} from 'vitest';
const TARGET_UNIVERSAL_IDENTIFIER = '5a2f4d2a-1a1e-4c66-8a54-1f0a2b3c4d5e';
+const JOB_ID = '0b9c8d7e-6f5a-4b3c-2d1e-0f9a8b7c6d5e';
const importEnqueueJob = async () => {
const module = await import('@/sdk/logic-function/jobs/enqueue-job');
@@ -27,6 +28,7 @@ const successResponse = () =>
enqueueJob: {
enqueued: true,
logicFunctionUniversalIdentifier: TARGET_UNIVERSAL_IDENTIFIER,
+ jobId: JOB_ID,
},
});
@@ -61,6 +63,7 @@ describe('enqueueJob', () => {
expect(result).toEqual({
enqueued: true,
logicFunctionUniversalIdentifier: TARGET_UNIVERSAL_IDENTIFIER,
+ jobId: JOB_ID,
});
const [url, requestInit] = fetchSpy.mock.calls[0];
@@ -70,7 +73,7 @@ describe('enqueueJob', () => {
const sentBody = JSON.parse(requestInit?.body as string);
expect(sentBody.query).toContain(
- 'enqueueJob(input:$v1){enqueued,logicFunctionUniversalIdentifier}',
+ 'enqueueJob(input:$v1){enqueued,logicFunctionUniversalIdentifier,jobId}',
);
expect(Object.values(sentBody.variables)).toEqual([
{
diff --git a/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/enqueue-jobs.spec.ts b/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/enqueue-jobs.spec.ts
index bf0089ab8e7..b927cf3eb89 100644
--- a/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/enqueue-jobs.spec.ts
+++ b/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/enqueue-jobs.spec.ts
@@ -9,6 +9,10 @@ import {
} from 'vitest';
const TARGET_UNIVERSAL_IDENTIFIER = '5a2f4d2a-1a1e-4c66-8a54-1f0a2b3c4d5e';
+const JOB_IDS = [
+ '0b9c8d7e-6f5a-4b3c-2d1e-0f9a8b7c6d5e',
+ '1c8b7a6d-5e4f-3a2b-1c0d-9e8f7a6b5c4d',
+];
const importEnqueueJobs = async () => {
const module = await import('@/sdk/logic-function/jobs/enqueue-jobs');
@@ -28,6 +32,7 @@ const successResponse = (enqueuedJobsCount: number) =>
enqueued: true,
logicFunctionUniversalIdentifier: TARGET_UNIVERSAL_IDENTIFIER,
enqueuedJobsCount,
+ jobIds: JOB_IDS.slice(0, enqueuedJobsCount),
},
});
@@ -63,6 +68,7 @@ describe('enqueueJobs', () => {
enqueued: true,
logicFunctionUniversalIdentifier: TARGET_UNIVERSAL_IDENTIFIER,
enqueuedJobsCount: 2,
+ jobIds: JOB_IDS,
});
const [url, requestInit] = fetchSpy.mock.calls[0];
@@ -72,7 +78,7 @@ describe('enqueueJobs', () => {
const sentBody = JSON.parse(requestInit?.body as string);
expect(sentBody.query).toContain(
- 'enqueueJobs(input:$v1){enqueued,logicFunctionUniversalIdentifier,enqueuedJobsCount}',
+ 'enqueueJobs(input:$v1){enqueued,logicFunctionUniversalIdentifier,enqueuedJobsCount,jobIds}',
);
expect(Object.values(sentBody.variables)).toEqual([
{
diff --git a/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/get-jobs.spec.ts b/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/get-jobs.spec.ts
new file mode 100644
index 00000000000..8e1d710932e
--- /dev/null
+++ b/packages/twenty-sdk/src/sdk/logic-function/jobs/__tests__/get-jobs.spec.ts
@@ -0,0 +1,115 @@
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ vi,
+ type MockInstance,
+} from 'vitest';
+
+const JOB_IDS = [
+ '0b9c8d7e-6f5a-4b3c-2d1e-0f9a8b7c6d5e',
+ '1c8b7a6d-5e4f-3a2b-1c0d-9e8f7a6b5c4d',
+];
+
+const importGetJobs = async () => {
+ const module = await import('@/sdk/logic-function/jobs/get-jobs');
+
+ return module.getJobs;
+};
+
+const graphqlResponse = (data: unknown) =>
+ new Response(JSON.stringify({ data }), {
+ status: 200,
+ headers: { 'Content-Type': 'application/json' },
+ });
+
+const successResponse = () =>
+ graphqlResponse({
+ getJobs: [
+ {
+ jobId: JOB_IDS[0],
+ state: 'COMPLETED',
+ attemptsMade: 1,
+ failedReason: null,
+ enqueuedAt: 1788000000000,
+ startedAt: 1788000001000,
+ finishedAt: 1788000002000,
+ },
+ ],
+ });
+
+describe('getJobs', () => {
+ let fetchSpy: MockInstance<typeof fetch>;
+
+ beforeEach(() => {
+ vi.resetModules();
+ process.env.TWENTY_API_URL = 'https://api.test';
+ process.env.TWENTY_APP_ACCESS_TOKEN = 'app-token';
+ fetchSpy = vi.spyOn(globalThis, 'fetch');
+ });
+
+ afterEach(() => {
+ delete process.env.TWENTY_API_URL;
+ delete process.env.TWENTY_APP_ACCESS_TOKEN;
+ fetchSpy.mockRestore();
+ });
+
+ it('calls the getJobs query on the metadata API and returns its result', async () => {
+ fetchSpy.mockResolvedValue(successResponse());
+
+ const getJobs = await importGetJobs();
+
+ const result = await getJobs(JOB_IDS);
+
+ expect(result).toEqual([
+ {
+ jobId: JOB_IDS[0],
+ state: 'COMPLETED',
+ attemptsMade: 1,
+ failedReason: null,
+ enqueuedAt: 1788000000000,
+ startedAt: 1788000001000,
+ finishedAt: 1788000002000,
+ },
+ ]);
+
+ const [url, requestInit] = fetchSpy.mock.calls[0];
+
+ expect(url).toBe('https://api.test/metadata');
+
+ const sentBody = JSON.parse(requestInit?.body as string);
+
+ expect(sentBody.query).toContain(
+ 'getJobs(jobIds:$v1){jobId,state,attemptsMade,failedReason,enqueuedAt,startedAt,finishedAt}',
+ );
+ expect(Object.values(sentBody.variables)).toEqual([JOB_IDS]);
+ });
+
+ it('sends the app access token as a bearer credential', async () => {
+ fetchSpy.mockResolvedValue(successResponse());
+
+ const getJobs = await importGetJobs();
+
+ await getJobs(JOB_IDS);
+
+ const [, requestInit] = fetchSpy.mock.calls[0];
+ const headers = new Headers(requestInit?.headers);
+
+ expect(headers.get('authorization')).toBe('Bearer app-token');
+ });
+
+ it('surfaces GraphQL errors as a rejection', async () => {
+ fetchSpy.mockResolvedValue(
+ new Response(JSON.stringify({ errors: [{ message: 'Job not found' }] }), {
+ status: 200,
+ headers: { 'Content-Type': 'application/json' },
+ }),
+ );
+
+ const getJobs = await importGetJobs();
+
+ await expect(getJobs(JOB_IDS)).rejects.toThrow(/Job not found/);
+ });
+});
diff --git a/packages/twenty-sdk/src/sdk/logic-function/jobs/enqueue-job.ts b/packages/twenty-sdk/src/sdk/logic-function/jobs/enqueue-job.ts
index f42b65bbf7e..d6823e9b652 100644
--- a/packages/twenty-sdk/src/sdk/logic-function/jobs/enqueue-job.ts
+++ b/packages/twenty-sdk/src/sdk/logic-function/jobs/enqueue-job.ts
@@ -17,6 +17,7 @@ export const enqueueJob = async (
__args: { input },
enqueued: true,
logicFunctionUniversalIdentifier: true,
+ jobId: true,
},
});
diff --git a/packages/twenty-sdk/src/sdk/logic-function/jobs/enqueue-jobs.ts b/packages/twenty-sdk/src/sdk/logic-function/jobs/enqueue-jobs.ts
index eb2c5abe1a3..d6054fc9a16 100644
--- a/packages/twenty-sdk/src/sdk/logic-function/jobs/enqueue-jobs.ts
+++ b/packages/twenty-sdk/src/sdk/logic-function/jobs/enqueue-jobs.ts
@@ -15,6 +15,7 @@ export const enqueueJobs = async (
enqueued: true,
logicFunctionUniversalIdentifier: true,
enqueuedJobsCount: true,
+ jobIds: true,
},
});
diff --git a/packages/twenty-sdk/src/sdk/logic-function/jobs/get-jobs.ts b/packages/twenty-sdk/src/sdk/logic-function/jobs/get-jobs.ts
new file mode 100644
index 00000000000..c5dc674c249
--- /dev/null
+++ b/packages/twenty-sdk/src/sdk/logic-function/jobs/get-jobs.ts
@@ -0,0 +1,21 @@
+import { MetadataApiClient } from 'twenty-client-sdk/metadata';
+import { type JobStatusResult } from 'twenty-shared/application';
+
+export const getJobs = async (jobIds: string[]): Promise<JobStatusResult[]> => {
+ const client = new MetadataApiClient();
+
+ const { getJobs: result } = await client.query({
+ getJobs: {
+ __args: { jobIds },
+ jobId: true,
+ state: true,
+ attemptsMade: true,
+ failedReason: true,
+ enqueuedAt: true,
+ startedAt: true,
+ finishedAt: true,
+ },
+ });
+
+ return result;
+};
diff --git a/packages/twenty-server/src/engine/core-modules/admin-panel/admin-panel-queue.service.ts b/packages/twenty-server/src/engine/core-modules/admin-panel/admin-panel-queue.service.ts
index cbd4d5786b5..771600c4744 100644
--- a/packages/twenty-server/src/engine/core-modules/admin-panel/admin-panel-queue.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/admin-panel/admin-panel-queue.service.ts
@@ -8,7 +8,7 @@ import {
bullMQToJobStateEnum,
JobStateEnum,
jobStateEnumToBullMQ,
-} from 'src/engine/core-modules/admin-panel/enums/job-state.enum';
+} from 'src/engine/core-modules/message-queue/enums/job-state.enum';
import { InternalServerError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import { QUEUE_RETENTION } from 'src/engine/core-modules/message-queue/constants/queue-retention.constants';
import { type MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
diff --git a/packages/twenty-server/src/engine/core-modules/admin-panel/admin-panel.resolver.ts b/packages/twenty-server/src/engine/core-modules/admin-panel/admin-panel.resolver.ts
index bb34e055b31..4475d12b243 100644
--- a/packages/twenty-server/src/engine/core-modules/admin-panel/admin-panel.resolver.ts
+++ b/packages/twenty-server/src/engine/core-modules/admin-panel/admin-panel.resolver.ts
@@ -43,7 +43,7 @@ import { AdminChatThreadScope } from 'src/engine/core-modules/admin-panel/enums/
import { AdminChatThreadSortDirection } from 'src/engine/core-modules/admin-panel/enums/admin-chat-thread-sort-direction.enum';
import { AdminChatThreadSortField } from 'src/engine/core-modules/admin-panel/enums/admin-chat-thread-sort-field.enum';
import { HealthIndicatorId } from 'src/engine/core-modules/admin-panel/enums/health-indicator-id.enum';
-import { JobStateEnum } from 'src/engine/core-modules/admin-panel/enums/job-state.enum';
+import { JobStateEnum } from 'src/engine/core-modules/message-queue/enums/job-state.enum';
import { QueueMetricsTimeRange } from 'src/engine/core-modules/admin-panel/enums/queue-metrics-time-range.enum';
import { MaintenanceModeService } from 'src/engine/core-modules/admin-panel/maintenance-mode.service';
import { AdminPanelBillingService } from 'src/engine/core-modules/admin-panel/services/admin-panel-billing.service';
diff --git a/packages/twenty-server/src/engine/core-modules/admin-panel/dtos/queue-job.dto.ts b/packages/twenty-server/src/engine/core-modules/admin-panel/dtos/queue-job.dto.ts
index 1d01e03eac3..e02bc48255c 100644
--- a/packages/twenty-server/src/engine/core-modules/admin-panel/dtos/queue-job.dto.ts
+++ b/packages/twenty-server/src/engine/core-modules/admin-panel/dtos/queue-job.dto.ts
@@ -2,7 +2,7 @@ import { Field, ObjectType } from '@nestjs/graphql';
import GraphQLJSON from 'graphql-type-json';
-import { JobStateEnum } from 'src/engine/core-modules/admin-panel/enums/job-state.enum';
+import { JobStateEnum } from 'src/engine/core-modules/message-queue/enums/job-state.enum';
@ObjectType('QueueJob')
export class QueueJobDTO {
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/application-job.resolver.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/application-job.resolver.ts
index f9ab60f407f..973fd55bf0f 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-job/application-job.resolver.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/application-job.resolver.ts
@@ -1,5 +1,5 @@
import { UseFilters, UseGuards, UsePipes } from '@nestjs/common';
-import { Args, Mutation } from '@nestjs/graphql';
+import { Args, Mutation, Query } from '@nestjs/graphql';
import {
type EnqueueJobResult,
@@ -12,6 +12,7 @@ import { EnqueueJobResultDTO } from 'src/engine/core-modules/application/applica
import { EnqueueJobInputDTO } from 'src/engine/core-modules/application/application-job/dtos/enqueue-job.input';
import { EnqueueJobsResultDTO } from 'src/engine/core-modules/application/application-job/dtos/enqueue-jobs-result.dto';
import { EnqueueJobsInputDTO } from 'src/engine/core-modules/application/application-job/dtos/enqueue-jobs.input';
+import { JobStatusDTO } from 'src/engine/core-modules/application/application-job/dtos/job-status.dto';
import { ApplicationJobService } from 'src/engine/core-modules/application/application-job/services/application-job.service';
import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
import { type AuthContextUser } from 'src/engine/core-modules/auth/types/auth-context.type';
@@ -51,6 +52,18 @@ export class ApplicationJobResolver {
});
}
+ @Query(() => [JobStatusDTO])
+ async getJobs(
+ @AuthApplication() application: FlatApplication,
+ @AuthWorkspace() workspace: FlatWorkspace,
+ @Args('jobIds', { type: () => [String] }) jobIds: string[],
+ ): Promise<JobStatusDTO[]> {
+ return this.applicationJobService.getJobs({
+ workspaceId: workspace.id,
+ jobIds,
+ });
+ }
+
@Mutation(() => EnqueueJobsResultDTO)
async enqueueJobs(
@AuthApplication() application: FlatApplication,
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-default-retry-limit.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-default-retry-limit.constant.ts
new file mode 100644
index 00000000000..17ad60490c4
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-default-retry-limit.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_DEFAULT_RETRY_LIMIT = 0;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-max-length.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-max-length.constant.ts
new file mode 100644
index 00000000000..162680f0702
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-max-length.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_ID_MAX_LENGTH = 128;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-min-length.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-min-length.constant.ts
new file mode 100644
index 00000000000..4d3759ed660
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-min-length.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_ID_MIN_LENGTH = 1;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-pattern.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-pattern.constant.ts
new file mode 100644
index 00000000000..7126e5add60
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-id-pattern.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_ID_PATTERN = /^[\w.-]+$/;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-max-delay-ms.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-max-delay-ms.constant.ts
new file mode 100644
index 00000000000..07ad93d7d7e
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-max-delay-ms.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_MAX_DELAY_MS = 7 * 24 * 60 * 60 * 1000;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-max-retry-limit.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-max-retry-limit.constant.ts
new file mode 100644
index 00000000000..7a8814f8f72
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-max-retry-limit.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_MAX_RETRY_LIMIT = 10;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-min-delay-ms.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-min-delay-ms.constant.ts
new file mode 100644
index 00000000000..94d80bb905e
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-min-delay-ms.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_MIN_DELAY_MS = 0;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-min-retry-limit.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-min-retry-limit.constant.ts
new file mode 100644
index 00000000000..bfcd4423be2
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-min-retry-limit.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_MIN_RETRY_LIMIT = 0;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-priority.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-priority.constant.ts
new file mode 100644
index 00000000000..9bc9aeb3d44
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job-priority.constant.ts
@@ -0,0 +1 @@
+export const ENQUEUE_JOB_PRIORITY = 10;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job.constant.ts
deleted file mode 100644
index 72e7afe5ea6..00000000000
--- a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/enqueue-job.constant.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-export const ENQUEUE_JOB_MIN_RETRY_LIMIT = 0;
-export const ENQUEUE_JOB_MAX_RETRY_LIMIT = 10;
-export const ENQUEUE_JOB_DEFAULT_RETRY_LIMIT = 0;
-
-export const ENQUEUE_JOB_PRIORITY = 10;
-
-export const ENQUEUE_JOB_MIN_DELAY_MS = 0;
-export const ENQUEUE_JOB_MAX_DELAY_MS = 7 * 24 * 60 * 60 * 1000;
-
-export const MAX_JOBS_PER_ENQUEUE = 200;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/max-jobs-per-enqueue.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/max-jobs-per-enqueue.constant.ts
new file mode 100644
index 00000000000..37d15053874
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/max-jobs-per-enqueue.constant.ts
@@ -0,0 +1 @@
+export const MAX_JOBS_PER_ENQUEUE = 200;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/constants/max-jobs-per-status-read.constant.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/max-jobs-per-status-read.constant.ts
new file mode 100644
index 00000000000..4276fd3d015
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/constants/max-jobs-per-status-read.constant.ts
@@ -0,0 +1 @@
+export const MAX_JOBS_PER_STATUS_READ = 200;
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job-item.input.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job-item.input.ts
new file mode 100644
index 00000000000..77b50c4a8f1
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job-item.input.ts
@@ -0,0 +1,23 @@
+import { Field, InputType } from '@nestjs/graphql';
+
+import { IsObject, IsOptional, Length, Matches } from 'class-validator';
+import GraphQLJSON from 'graphql-type-json';
+import { type EnqueueJobItem } from 'twenty-shared/application';
+
+import { ENQUEUE_JOB_ID_MAX_LENGTH } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-id-max-length.constant';
+import { ENQUEUE_JOB_ID_MIN_LENGTH } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-id-min-length.constant';
+import { ENQUEUE_JOB_ID_PATTERN } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-id-pattern.constant';
+
+@InputType('EnqueueJobItemInput')
+export class EnqueueJobItemInputDTO implements EnqueueJobItem {
+ @IsObject()
+ @IsOptional()
+ @Field(() => GraphQLJSON, { nullable: true })
+ payload?: Record<string, unknown>;
+
+ @Matches(ENQUEUE_JOB_ID_PATTERN)
+ @Length(ENQUEUE_JOB_ID_MIN_LENGTH, ENQUEUE_JOB_ID_MAX_LENGTH)
+ @IsOptional()
+ @Field(() => String, { nullable: true })
+ jobId?: string;
+}
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job-result.dto.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job-result.dto.ts
index 541f70a4369..95fbf27b619 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job-result.dto.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job-result.dto.ts
@@ -9,4 +9,7 @@ export class EnqueueJobResultDTO implements EnqueueJobResult {
@Field()
logicFunctionUniversalIdentifier: string;
+
+ @Field()
+ jobId: string;
}
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job.input.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job.input.ts
index 0cdccd41ba4..56f8b7cf952 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job.input.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-job.input.ts
@@ -6,18 +6,21 @@ import {
IsObject,
IsOptional,
IsUUID,
+ Length,
+ Matches,
Max,
Min,
} from 'class-validator';
import GraphQLJSON from 'graphql-type-json';
import { type EnqueueJobInput } from 'twenty-shared/application';
-import {
- ENQUEUE_JOB_MAX_DELAY_MS,
- ENQUEUE_JOB_MAX_RETRY_LIMIT,
- ENQUEUE_JOB_MIN_DELAY_MS,
- ENQUEUE_JOB_MIN_RETRY_LIMIT,
-} from 'src/engine/core-modules/application/application-job/constants/enqueue-job.constant';
+import { ENQUEUE_JOB_ID_MAX_LENGTH } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-id-max-length.constant';
+import { ENQUEUE_JOB_ID_MIN_LENGTH } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-id-min-length.constant';
+import { ENQUEUE_JOB_ID_PATTERN } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-id-pattern.constant';
+import { ENQUEUE_JOB_MAX_DELAY_MS } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-max-delay-ms.constant';
+import { ENQUEUE_JOB_MAX_RETRY_LIMIT } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-max-retry-limit.constant';
+import { ENQUEUE_JOB_MIN_DELAY_MS } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-min-delay-ms.constant';
+import { ENQUEUE_JOB_MIN_RETRY_LIMIT } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-min-retry-limit.constant';
@InputType('EnqueueJobInput')
export class EnqueueJobInputDTO implements EnqueueJobInput {
@@ -31,6 +34,12 @@ export class EnqueueJobInputDTO implements EnqueueJobInput {
@Field(() => GraphQLJSON, { nullable: true })
payload?: Record<string, unknown>;
+ @Matches(ENQUEUE_JOB_ID_PATTERN)
+ @Length(ENQUEUE_JOB_ID_MIN_LENGTH, ENQUEUE_JOB_ID_MAX_LENGTH)
+ @IsOptional()
+ @Field(() => String, { nullable: true })
+ jobId?: string;
+
@IsInt()
@Min(ENQUEUE_JOB_MIN_RETRY_LIMIT)
@Max(ENQUEUE_JOB_MAX_RETRY_LIMIT)
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-jobs-result.dto.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-jobs-result.dto.ts
index 4c4ed006413..a52b5831139 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-jobs-result.dto.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-jobs-result.dto.ts
@@ -12,4 +12,7 @@ export class EnqueueJobsResultDTO implements EnqueueJobsResult {
@Field(() => Int)
enqueuedJobsCount: number;
+
+ @Field(() => [String])
+ jobIds: string[];
}
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-jobs.input.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-jobs.input.ts
index e9bde44bc7d..482a5595d5a 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-jobs.input.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/enqueue-jobs.input.ts
@@ -1,5 +1,6 @@
import { Field, InputType, Int } from '@nestjs/graphql';
+import { Type } from 'class-transformer';
import {
ArrayMaxSize,
ArrayMinSize,
@@ -10,17 +11,17 @@ import {
IsUUID,
Max,
Min,
+ ValidateNested,
} from 'class-validator';
import GraphQLJSON from 'graphql-type-json';
import { type EnqueueJobsInput } from 'twenty-shared/application';
-import {
- ENQUEUE_JOB_MAX_DELAY_MS,
- ENQUEUE_JOB_MAX_RETRY_LIMIT,
- ENQUEUE_JOB_MIN_DELAY_MS,
- ENQUEUE_JOB_MIN_RETRY_LIMIT,
- MAX_JOBS_PER_ENQUEUE,
-} from 'src/engine/core-modules/application/application-job/constants/enqueue-job.constant';
+import { ENQUEUE_JOB_MAX_DELAY_MS } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-max-delay-ms.constant';
+import { ENQUEUE_JOB_MAX_RETRY_LIMIT } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-max-retry-limit.constant';
+import { ENQUEUE_JOB_MIN_DELAY_MS } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-min-delay-ms.constant';
+import { ENQUEUE_JOB_MIN_RETRY_LIMIT } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-min-retry-limit.constant';
+import { MAX_JOBS_PER_ENQUEUE } from 'src/engine/core-modules/application/application-job/constants/max-jobs-per-enqueue.constant';
+import { EnqueueJobItemInputDTO } from 'src/engine/core-modules/application/application-job/dtos/enqueue-job-item.input';
@InputType('EnqueueJobsInput')
export class EnqueueJobsInputDTO implements EnqueueJobsInput {
@@ -32,8 +33,20 @@ export class EnqueueJobsInputDTO implements EnqueueJobsInput {
@IsObject({ each: true })
@ArrayMinSize(1)
@ArrayMaxSize(MAX_JOBS_PER_ENQUEUE)
- @Field(() => [GraphQLJSON])
- payloads: Record<string, unknown>[];
+ @IsOptional()
+ @Field(() => [GraphQLJSON], {
+ nullable: true,
+ deprecationReason: 'Use jobs instead.',
+ })
+ payloads?: Record<string, unknown>[];
+
+ @ValidateNested({ each: true })
+ @Type(() => EnqueueJobItemInputDTO)
+ @ArrayMinSize(1)
+ @ArrayMaxSize(MAX_JOBS_PER_ENQUEUE)
+ @IsOptional()
+ @Field(() => [EnqueueJobItemInputDTO], { nullable: true })
+ jobs?: EnqueueJobItemInputDTO[];
@IsInt()
@Min(ENQUEUE_JOB_MIN_RETRY_LIMIT)
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/job-status.dto.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/job-status.dto.ts
new file mode 100644
index 00000000000..e4b9d5b413a
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/dtos/job-status.dto.ts
@@ -0,0 +1,27 @@
+import { Field, Int, ObjectType } from '@nestjs/graphql';
+
+import { JobStateEnum } from 'src/engine/core-modules/message-queue/enums/job-state.enum';
+
+@ObjectType('JobStatus')
+export class JobStatusDTO {
+ @Field()
+ jobId: string;
+
+ @Field(() => JobStateEnum)
+ state: JobStateEnum;
+
+ @Field(() => Int)
+ attemptsMade: number;
+
+ @Field(() => String, { nullable: true })
+ failedReason?: string;
+
+ @Field(() => Number)
+ enqueuedAt: number;
+
+ @Field(() => Number, { nullable: true })
+ startedAt?: number;
+
+ @Field(() => Number, { nullable: true })
+ finishedAt?: number;
+}
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/services/application-job.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/services/application-job.service.ts
index 44c89d93588..b19d87d5d4d 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-job/services/application-job.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/services/application-job.service.ts
@@ -1,17 +1,21 @@
import { Injectable } from '@nestjs/common';
import {
+ type EnqueueJobItem,
type EnqueueJobResult,
type EnqueueJobsResult,
} from 'twenty-shared/application';
import { isDefined } from 'twenty-shared/utils';
+import { v4 } from 'uuid';
-import {
- ENQUEUE_JOB_DEFAULT_RETRY_LIMIT,
- ENQUEUE_JOB_PRIORITY,
-} from 'src/engine/core-modules/application/application-job/constants/enqueue-job.constant';
+import { bullMQToJobStateEnum } from 'src/engine/core-modules/message-queue/enums/job-state.enum';
+import { ENQUEUE_JOB_DEFAULT_RETRY_LIMIT } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-default-retry-limit.constant';
+import { ENQUEUE_JOB_PRIORITY } from 'src/engine/core-modules/application/application-job/constants/enqueue-job-priority.constant';
+import { MAX_JOBS_PER_STATUS_READ } from 'src/engine/core-modules/application/application-job/constants/max-jobs-per-status-read.constant';
import { type EnqueueJobInputDTO } from 'src/engine/core-modules/application/application-job/dtos/enqueue-job.input';
import { type EnqueueJobsInputDTO } from 'src/engine/core-modules/application/application-job/dtos/enqueue-jobs.input';
+import { type JobStatusDTO } from 'src/engine/core-modules/application/application-job/dtos/job-status.dto';
+import { buildQueueJobId } from 'src/engine/core-modules/application/application-job/utils/build-queue-job-id.util';
import {
ApplicationException,
ApplicationExceptionCode,
@@ -48,7 +52,7 @@ export class ApplicationJobService {
userWorkspaceId: string | null;
input: EnqueueJobInputDTO;
}): Promise<EnqueueJobResult> {
- const { enqueued, logicFunctionUniversalIdentifier } =
+ const { enqueued, logicFunctionUniversalIdentifier, jobIds } =
await this.enqueueJobs({
applicationId,
workspaceId,
@@ -57,13 +61,13 @@ export class ApplicationJobService {
input: {
logicFunctionUniversalIdentifier:
input.logicFunctionUniversalIdentifier,
- payloads: [input.payload ?? {}],
+ jobs: [{ payload: input.payload ?? {}, jobId: input.jobId }],
retryLimit: input.retryLimit,
delayMs: input.delayMs,
},
});
- return { enqueued, logicFunctionUniversalIdentifier };
+ return { enqueued, logicFunctionUniversalIdentifier, jobId: jobIds[0] };
}
async enqueueJobs({
@@ -81,6 +85,8 @@ export class ApplicationJobService {
}): Promise<EnqueueJobsResult> {
const { logicFunctionUniversalIdentifier } = input;
+ const jobItems = this.getJobItems(input);
+
const { flatLogicFunctionMaps } =
await this.workspaceCacheService.getOrRecompute(workspaceId, [
'flatLogicFunctionMaps',
@@ -102,14 +108,19 @@ export class ApplicationJobService {
);
}
+ const jobIds = jobItems.map((jobItem) => jobItem.jobId ?? v4());
+
await this.messageQueueService.bulkAdd<LogicFunctionTriggerJobData>(
LogicFunctionTriggerJob.name,
- input.payloads.map((payload) => ({
- logicFunctionId: flatLogicFunction.id,
- workspaceId,
- payload,
- ...(isDefined(userId) ? { userId } : {}),
- ...(isDefined(userWorkspaceId) ? { userWorkspaceId } : {}),
+ jobItems.map((jobItem, index) => ({
+ data: {
+ logicFunctionId: flatLogicFunction.id,
+ workspaceId,
+ payload: jobItem.payload ?? {},
+ ...(isDefined(userId) ? { userId } : {}),
+ ...(isDefined(userWorkspaceId) ? { userWorkspaceId } : {}),
+ },
+ jobId: buildQueueJobId({ workspaceId, jobId: jobIds[index] }),
})),
{
retryLimit: input.retryLimit ?? ENQUEUE_JOB_DEFAULT_RETRY_LIMIT,
@@ -122,7 +133,80 @@ export class ApplicationJobService {
return {
enqueued: true,
logicFunctionUniversalIdentifier,
- enqueuedJobsCount: input.payloads.length,
+ enqueuedJobsCount: jobItems.length,
+ jobIds,
};
}
+
+ async getJobs({
+ workspaceId,
+ jobIds,
+ }: {
+ workspaceId: string;
+ jobIds: string[];
+ }): Promise<JobStatusDTO[]> {
+ if (jobIds.length > MAX_JOBS_PER_STATUS_READ) {
+ throw new ApplicationException(
+ `Cannot read more than ${MAX_JOBS_PER_STATUS_READ} jobs at once`,
+ ApplicationExceptionCode.INVALID_INPUT,
+ );
+ }
+
+ const jobsByQueueJobId = await this.messageQueueService.getJobs(
+ jobIds.map((jobId) => buildQueueJobId({ workspaceId, jobId })),
+ );
+
+ return jobIds.flatMap((jobId) => {
+ const job = jobsByQueueJobId[buildQueueJobId({ workspaceId, jobId })];
+
+ if (!isDefined(job)) {
+ return [];
+ }
+
+ return [
+ {
+ jobId,
+ state: bullMQToJobStateEnum[job.state],
+ attemptsMade: job.attemptsMade,
+ failedReason: job.failedReason,
+ enqueuedAt: job.timestamp,
+ startedAt: job.processedOn,
+ finishedAt: job.finishedOn,
+ },
+ ];
+ });
+ }
+
+ private getJobItems(input: EnqueueJobsInputDTO): EnqueueJobItem[] {
+ if (isDefined(input.jobs) && isDefined(input.payloads)) {
+ throw new ApplicationException(
+ 'Provide either jobs or payloads, not both',
+ ApplicationExceptionCode.INVALID_INPUT,
+ );
+ }
+
+ if (isDefined(input.jobs)) {
+ const callerSuppliedJobIds = input.jobs
+ .map((job) => job.jobId)
+ .filter(isDefined);
+
+ if (new Set(callerSuppliedJobIds).size !== callerSuppliedJobIds.length) {
+ throw new ApplicationException(
+ 'Job ids must be unique within a batch',
+ ApplicationExceptionCode.INVALID_INPUT,
+ );
+ }
+
+ return input.jobs;
+ }
+
+ if (isDefined(input.payloads)) {
+ return input.payloads.map((payload) => ({ payload }));
+ }
+
+ throw new ApplicationException(
+ 'Either jobs or payloads must be provided',
+ ApplicationExceptionCode.INVALID_INPUT,
+ );
+ }
}
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-job/utils/build-queue-job-id.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-job/utils/build-queue-job-id.util.ts
new file mode 100644
index 00000000000..78c5be4c971
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-job/utils/build-queue-job-id.util.ts
@@ -0,0 +1,9 @@
+// BullMQ job ids are queue-global, so the workspace prefix is what keeps one
+// workspace from reading or overwriting another workspace's job
+export const buildQueueJobId = ({
+ workspaceId,
+ jobId,
+}: {
+ workspaceId: string;
+ jobId: string;
+}): string => `${workspaceId}.${jobId}`;
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/triggers/database-event/call-database-event-trigger-jobs.job.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/triggers/database-event/call-database-event-trigger-jobs.job.ts
index 1397fd66d7a..5964d069986 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/triggers/database-event/call-database-event-trigger-jobs.job.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-trigger/triggers/database-event/call-database-event-trigger-jobs.job.ts
@@ -119,7 +119,9 @@ export class CallDatabaseEventTriggerJobsJob {
await this.messageQueueService.bulkAdd<LogicFunctionTriggerJobData>(
LogicFunctionTriggerJob.name,
- logicFunctionPayloads,
+ logicFunctionPayloads.map((logicFunctionPayload) => ({
+ data: logicFunctionPayload,
+ })),
{
retryLimit: 3,
backoff: LOGIC_FUNCTION_QUEUE_RETRY_BACKOFF,
diff --git a/packages/twenty-server/src/engine/core-modules/message-queue/drivers/bullmq.driver.ts b/packages/twenty-server/src/engine/core-modules/message-queue/drivers/bullmq.driver.ts
index 4e042a536ed..17539de2eeb 100644
--- a/packages/twenty-server/src/engine/core-modules/message-queue/drivers/bullmq.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/message-queue/drivers/bullmq.driver.ts
@@ -24,6 +24,8 @@ import {
import {
type InFlightQueueJob,
type MessageQueueDriver,
+ type QueueJobDetails,
+ type QueueJobToAdd,
} from 'src/engine/core-modules/message-queue/drivers/interfaces/message-queue-driver.interface';
import {
type MessageQueueJob,
@@ -383,7 +385,7 @@ export class BullMQDriver
jobName: string,
data: T,
options?: QueueJobOptions,
- ): Promise<void> {
+ ): Promise<string | undefined> {
if (!this.queueMap[queueName]) {
throw new Error(
`Queue ${queueName} is not registered, make sure you have added it as a queue provider`,
@@ -405,39 +407,91 @@ export class BullMQDriver
const queueOptions = this.buildJobsOptions({ queueName, options });
- await this.queueMap[queueName].add(jobName, data, queueOptions);
+ const job = await this.queueMap[queueName].add(jobName, data, queueOptions);
+
+ return job.id;
}
- async bulkAdd<T>(
+ async bulkAdd<T extends MessageQueueJobData>(
queueName: MessageQueue,
jobName: string,
- dataItems: T[],
+ jobs: QueueJobToAdd<T>[],
options?: QueueJobOptions,
- ): Promise<void> {
+ ): Promise<string[]> {
if (!this.queueMap[queueName]) {
throw new Error(
`Queue ${queueName} is not registered, make sure you have added it as a queue provider`,
);
}
- if (dataItems.length === 0) {
- return;
+ if (jobs.length === 0) {
+ return [];
}
const queueOptions = this.buildJobsOptions({ queueName, options });
- await this.queueMap[queueName].addBulk(
- dataItems.map((data, index) => ({
+ const addedJobs = await this.queueMap[queueName].addBulk(
+ jobs.map(({ data, jobId }, index) => ({
name: jobName,
data,
opts: {
...queueOptions,
- jobId: queueOptions.jobId
- ? `${queueOptions.jobId}-${index}`
- : undefined,
+ jobId:
+ jobId ??
+ (queueOptions.jobId ? `${queueOptions.jobId}-${index}` : undefined),
},
})),
);
+
+ return addedJobs.map((job) => job.id).filter(isDefined);
+ }
+
+ async getJobs<T extends MessageQueueJobData>(
+ queueName: MessageQueue,
+ jobIds: string[],
+ ): Promise<Partial<Record<string, QueueJobDetails<T>>>> {
+ if (!this.queueMap[queueName]) {
+ throw new Error(
+ `Queue ${queueName} is not registered, make sure you have added it as a queue provider`,
+ );
+ }
+
+ const jobs = await Promise.all(
+ jobIds.map((jobId) => this.getJobDetails<T>(queueName, jobId)),
+ );
+
+ return Object.fromEntries(
+ jobs.filter(isDefined).map((job) => [job.id, job]),
+ );
+ }
+
+ private async getJobDetails<T extends MessageQueueJobData>(
+ queueName: MessageQueue,
+ jobId: string,
+ ): Promise<QueueJobDetails<T> | undefined> {
+ const job = await this.queueMap[queueName].getJob(jobId);
+
+ if (!isDefined(job) || !isDefined(job.id)) {
+ return undefined;
+ }
+
+ const state = await job.getState();
+
+ // BullMQ reports 'unknown' for a job whose record was evicted by retention
+ if (state === 'unknown') {
+ return undefined;
+ }
+
+ return {
+ id: job.id,
+ data: job.data,
+ state,
+ attemptsMade: job.attemptsMade,
+ failedReason: job.failedReason,
+ timestamp: job.timestamp,
+ processedOn: job.processedOn,
+ finishedOn: job.finishedOn,
+ };
}
async getInFlightJobs<T extends MessageQueueJobData>(
diff --git a/packages/twenty-server/src/engine/core-modules/message-queue/drivers/interfaces/message-queue-driver.interface.ts b/packages/twenty-server/src/engine/core-modules/message-queue/drivers/interfaces/message-queue-driver.interface.ts
index 1ee089e4802..0d9953917b0 100644
--- a/packages/twenty-server/src/engine/core-modules/message-queue/drivers/interfaces/message-queue-driver.interface.ts
+++ b/packages/twenty-server/src/engine/core-modules/message-queue/drivers/interfaces/message-queue-driver.interface.ts
@@ -1,3 +1,5 @@
+import { type JobState } from 'bullmq/dist/esm/types';
+
import {
type QueueCronJobOptions,
type QueueJobOptions,
@@ -13,13 +15,13 @@ export interface MessageQueueDriver {
jobName: string,
data: T,
options?: QueueJobOptions,
- ): Promise<void>;
+ ): Promise<string | undefined>;
bulkAdd<T extends MessageQueueJobData>(
queueName: MessageQueue,
jobName: string,
- dataItems: T[],
+ jobs: QueueJobToAdd<T>[],
options?: QueueJobOptions,
- ): Promise<void>;
+ ): Promise<string[]>;
work<T extends MessageQueueJobData>(
queueName: MessageQueue,
handler: ({ data, id }: { data: T; id: string }) => Promise<void> | void,
@@ -51,8 +53,28 @@ export interface MessageQueueDriver {
getInFlightJobs?<T extends MessageQueueJobData>(
queueName: MessageQueue,
): Promise<InFlightQueueJob<T>[]>;
+ getJobs?<T extends MessageQueueJobData>(
+ queueName: MessageQueue,
+ jobIds: string[],
+ ): Promise<Partial<Record<string, QueueJobDetails<T>>>>;
}
+export type QueueJobToAdd<T extends MessageQueueJobData> = {
+ data: T;
+ jobId?: string;
+};
+
+export type QueueJobDetails<T extends MessageQueueJobData> = {
+ id: string;
+ data: T;
+ state: JobState;
+ attemptsMade: number;
+ failedReason?: string;
+ timestamp: number;
+ processedOn?: number;
+ finishedOn?: number;
+};
+
export interface InFlightQueueJob<T extends MessageQueueJobData> {
id?: string;
data: T;
diff --git a/packages/twenty-server/src/engine/core-modules/message-queue/drivers/sync.driver.ts b/packages/twenty-server/src/engine/core-modules/message-queue/drivers/sync.driver.ts
index 952fc32ce61..804d62a7e79 100644
--- a/packages/twenty-server/src/engine/core-modules/message-queue/drivers/sync.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/message-queue/drivers/sync.driver.ts
@@ -1,8 +1,12 @@
import { Logger } from '@nestjs/common';
import { isDefined } from 'twenty-shared/utils';
+import { v4 } from 'uuid';
-import { type MessageQueueDriver } from 'src/engine/core-modules/message-queue/drivers/interfaces/message-queue-driver.interface';
+import {
+ type MessageQueueDriver,
+ type QueueJobToAdd,
+} from 'src/engine/core-modules/message-queue/drivers/interfaces/message-queue-driver.interface';
import {
type MessageQueueJob,
type MessageQueueJobData,
@@ -23,22 +27,31 @@ export class SyncDriver implements MessageQueueDriver {
queueName: MessageQueue,
jobName: string,
data: T,
- ): Promise<void> {
- await this.processJob(queueName, this.createJob(jobName, data));
+ ): Promise<string | undefined> {
+ const job = this.createJob(jobName, data);
+
+ await this.processJob(queueName, job);
+
+ return job.id;
}
async bulkAdd<T extends MessageQueueJobData>(
queueName: MessageQueue,
jobName: string,
- dataItems: T[],
- ): Promise<void> {
+ jobs: QueueJobToAdd<T>[],
+ ): Promise<string[]> {
let firstError: unknown = undefined;
+ const jobIds: string[] = [];
// Each payload is an independent job in BullMQ, so a failing one must not
// prevent the others from being processed
- for (const data of dataItems) {
+ for (const { data, jobId } of jobs) {
+ const job = this.createJob(jobName, data, jobId);
+
+ jobIds.push(job.id);
+
try {
- await this.processJob(queueName, this.createJob(jobName, data));
+ await this.processJob(queueName, job);
} catch (error) {
firstError = firstError ?? error;
}
@@ -47,6 +60,8 @@ export class SyncDriver implements MessageQueueDriver {
if (isDefined(firstError)) {
throw firstError;
}
+
+ return jobIds;
}
async addCron<T extends MessageQueueJobData | undefined>({
@@ -92,9 +107,10 @@ export class SyncDriver implements MessageQueueDriver {
private createJob<T extends MessageQueueJobData | undefined>(
name: string,
data: T,
+ jobId?: string,
): MessageQueueJob<T> {
const job: MessageQueueJob<T> = {
- id: '',
+ id: jobId ?? v4(),
name,
data,
retryLimit: 0,
diff --git a/packages/twenty-server/src/engine/core-modules/admin-panel/enums/job-state.enum.ts b/packages/twenty-server/src/engine/core-modules/message-queue/enums/job-state.enum.ts
similarity index 100%
rename from packages/twenty-server/src/engine/core-modules/admin-panel/enums/job-state.enum.ts
rename to packages/twenty-server/src/engine/core-modules/message-queue/enums/job-state.enum.ts
diff --git a/packages/twenty-server/src/engine/core-modules/message-queue/services/message-queue.service.ts b/packages/twenty-server/src/engine/core-modules/message-queue/services/message-queue.service.ts
index 1f36a63c492..b77d092dceb 100644
--- a/packages/twenty-server/src/engine/core-modules/message-queue/services/message-queue.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/message-queue/services/message-queue.service.ts
@@ -7,6 +7,8 @@ import {
import {
type InFlightQueueJob,
MessageQueueDriver,
+ type QueueJobDetails,
+ type QueueJobToAdd,
} from 'src/engine/core-modules/message-queue/drivers/interfaces/message-queue-driver.interface';
import {
type MessageQueueJobData,
@@ -34,16 +36,26 @@ export class MessageQueueService {
jobName: string,
data: T,
options?: QueueJobOptions,
- ): Promise<void> {
+ ): Promise<string | undefined> {
return this.driver.add(this.queueName, jobName, data, options);
}
bulkAdd<T extends MessageQueueJobData>(
jobName: string,
- dataItems: T[],
+ jobs: QueueJobToAdd<T>[],
options?: QueueJobOptions,
- ): Promise<void> {
- return this.driver.bulkAdd(this.queueName, jobName, dataItems, options);
+ ): Promise<string[]> {
+ return this.driver.bulkAdd(this.queueName, jobName, jobs, options);
+ }
+
+ getJobs<T extends MessageQueueJobData>(
+ jobIds: string[],
+ ): Promise<Partial<Record<string, QueueJobDetails<T>>>> {
+ if (typeof this.driver.getJobs !== 'function') {
+ return Promise.resolve({});
+ }
+
+ return this.driver.getJobs(this.queueName, jobIds);
}
getInFlightJobs<T extends MessageQueueJobData>(): Promise<
diff --git a/packages/twenty-server/src/modules/emailing/crons/jobs/reconcile-campaign-stats.cron.job.ts b/packages/twenty-server/src/modules/emailing/crons/jobs/reconcile-campaign-stats.cron.job.ts
index e3c0f4b0332..b3de5292f9d 100644
--- a/packages/twenty-server/src/modules/emailing/crons/jobs/reconcile-campaign-stats.cron.job.ts
+++ b/packages/twenty-server/src/modules/emailing/crons/jobs/reconcile-campaign-stats.cron.job.ts
@@ -41,7 +41,7 @@ export class ReconcileCampaignStatsCronJob {
await this.messageQueueService
.bulkAdd<ReconcileWorkspaceCampaignStatsJobData>(
RECONCILE_WORKSPACE_CAMPAIGN_STATS_JOB,
- workspaceIdsBatch.map((workspaceId) => ({ workspaceId })),
+ workspaceIdsBatch.map((workspaceId) => ({ data: { workspaceId } })),
{ retryLimit: CAMPAIGN_JOB_RETRY_LIMIT },
)
.catch((error) => {
diff --git a/packages/twenty-server/src/modules/emailing/services/message-campaign-materialization.service.ts b/packages/twenty-server/src/modules/emailing/services/message-campaign-materialization.service.ts
index c54d7f5b0f4..9d56f962a6b 100644
--- a/packages/twenty-server/src/modules/emailing/services/message-campaign-materialization.service.ts
+++ b/packages/twenty-server/src/modules/emailing/services/message-campaign-materialization.service.ts
@@ -184,7 +184,7 @@ export class MessageCampaignMaterializationService {
await this.messageQueueService.bulkAdd<MaterializeCampaignChunkJobData>(
MATERIALIZE_CAMPAIGN_CHUNK_JOB,
- chunks,
+ chunks.map((chunk) => ({ data: chunk })),
{ retryLimit: 3, backoff: CAMPAIGN_SEND_RETRY_BACKOFF },
);
@@ -338,13 +338,15 @@ export class MessageCampaignMaterializationService {
await this.messageQueueService.bulkAdd<SendCampaignEmailJobData>(
SEND_CAMPAIGN_EMAIL_JOB,
recipients.map((recipient) => ({
- workspaceId,
- campaignId,
- messageId: recipient.messageId,
- personId: recipient.personId,
- recipientEmail: recipient.email,
- emailingDomainId,
- userWorkspaceId,
+ data: {
+ workspaceId,
+ campaignId,
+ messageId: recipient.messageId,
+ personId: recipient.personId,
+ recipientEmail: recipient.email,
+ emailingDomainId,
+ userWorkspaceId,
+ },
})),
{
retryLimit: CAMPAIGN_SEND_RETRY_LIMIT,
diff --git a/packages/twenty-server/test/integration/metadata/suites/application/enqueue-job.integration-spec.ts b/packages/twenty-server/test/integration/metadata/suites/application/enqueue-job.integration-spec.ts
index a2760ee5fc3..4b953c87a5a 100644
--- a/packages/twenty-server/test/integration/metadata/suites/application/enqueue-job.integration-spec.ts
+++ b/packages/twenty-server/test/integration/metadata/suites/application/enqueue-job.integration-spec.ts
@@ -2,8 +2,15 @@ import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
+import { randomUUID } from 'crypto';
+
import gql from 'graphql-tag';
+import { activateWorkspace } from 'test/integration/graphql/utils/activate-workspace.util';
+import { deleteUser } from 'test/integration/graphql/utils/delete-user.util';
import { findManyApplications } from 'test/integration/graphql/utils/find-many-applications.util';
+import { getAuthTokensFromLoginToken } from 'test/integration/graphql/utils/get-auth-tokens-from-login-token.util';
+import { signUpInNewWorkspace } from 'test/integration/graphql/utils/sign-up-in-new-workspace.util';
+import { signUp } from 'test/integration/graphql/utils/sign-up.util';
import { generateApplicationToken } from 'test/integration/metadata/suites/application/utils/generate-application-token.util';
import { createOneLogicFunction } from 'test/integration/metadata/suites/logic-function/utils/create-logic-function.util';
import { deleteLogicFunction } from 'test/integration/metadata/suites/logic-function/utils/delete-logic-function.util';
@@ -12,6 +19,7 @@ import { updateLogicFunctionSource } from 'test/integration/metadata/suites/logi
import { makeMetadataAPIRequest } from 'test/integration/metadata/suites/utils/make-metadata-api-request.util';
import { expectEventually } from 'test/integration/utils/expect-eventually.util';
import { waitForAllJobsToFinish } from 'test/integration/utils/wait-for-all-jobs-to-finish.util';
+import { isDefined } from 'twenty-shared/utils';
import { v4 as uuidv4 } from 'uuid';
import { WORKSPACE_CUSTOM_APPLICATION_NAME } from 'src/engine/core-modules/application/constants/workspace-custom-application.constant';
@@ -27,6 +35,14 @@ export const main = async (params: { markerPath: string }): Promise<object> => {
return { ok: true };
};`;
+const APPENDING_TARGET_SOURCE_CODE = `import { appendFileSync } from 'node:fs';
+
+export const main = async (params: { markerPath: string }): Promise<object> => {
+ appendFileSync(params.markerPath, 'ran\\n', 'utf-8');
+
+ return { ok: true };
+};`;
+
const RETRYABLE_TARGET_SOURCE_CODE = `import { appendFileSync } from 'node:fs';
type RetryableTargetParams = {
@@ -65,6 +81,7 @@ const ENQUEUE_JOB = gql`
enqueueJob(input: $input) {
enqueued
logicFunctionUniversalIdentifier
+ jobId
}
}
`;
@@ -75,6 +92,21 @@ const ENQUEUE_JOBS = gql`
enqueued
logicFunctionUniversalIdentifier
enqueuedJobsCount
+ jobIds
+ }
+ }
+`;
+
+const GET_JOBS = gql`
+ query GetJobs($jobIds: [String!]!) {
+ getJobs(jobIds: $jobIds) {
+ jobId
+ state
+ attemptsMade
+ failedReason
+ enqueuedAt
+ startedAt
+ finishedAt
}
}
`;
@@ -86,6 +118,68 @@ describe('enqueueJob (e2e)', () => {
let logicFunctionUniversalIdentifier: string;
let retryableLogicFunctionId: string;
let retryableLogicFunctionUniversalIdentifier: string;
+ let appendingLogicFunctionId: string;
+ let appendingLogicFunctionUniversalIdentifier: string;
+ let otherWorkspaceUserAccessToken: string | undefined;
+
+ const createOtherWorkspaceApplicationToken = async () => {
+ const email = `enqueue-job-other-workspace-${randomUUID()}@example.com`;
+
+ const { data: signUpData } = await signUp({
+ input: { email, password: 'Test123!@#' },
+ expectToFail: false,
+ });
+
+ otherWorkspaceUserAccessToken =
+ signUpData.signUp.tokens.accessOrWorkspaceAgnosticToken.token;
+
+ await globalThis.testDataSource.query(
+ 'UPDATE core."user" SET "isEmailVerified" = true WHERE email = $1',
+ [email],
+ );
+
+ const { data: newWorkspaceData } = await signUpInNewWorkspace({
+ accessToken: otherWorkspaceUserAccessToken,
+ expectToFail: false,
+ });
+
+ const { data: authTokensData } = await getAuthTokensFromLoginToken({
+ loginToken: newWorkspaceData.signUpInNewWorkspace.loginToken.token,
+ origin:
+ newWorkspaceData.signUpInNewWorkspace.workspace.workspaceUrls
+ .subdomainUrl,
+ expectToFail: false,
+ });
+
+ const workspaceAccessToken =
+ authTokensData.getAuthTokensFromLoginToken.tokens
+ .accessOrWorkspaceAgnosticToken.token;
+
+ await activateWorkspace({
+ accessToken: workspaceAccessToken,
+ expectToFail: false,
+ });
+
+ const { data: applicationsData } = await findManyApplications({
+ accessToken: workspaceAccessToken,
+ expectToFail: false,
+ });
+
+ const otherWorkspaceApplication =
+ applicationsData.findManyApplications.find(
+ (application) => application.name === WORKSPACE_CUSTOM_APPLICATION_NAME,
+ );
+
+ expect(otherWorkspaceApplication).toBeDefined();
+
+ const { data: tokenData } = await generateApplicationToken({
+ applicationId: otherWorkspaceApplication!.id,
+ expectToFail: false,
+ token: workspaceAccessToken,
+ });
+
+ return tokenData.generateApplicationToken.applicationAccessToken.token;
+ };
beforeAll(async () => {
mkdirSync(MARKER_DIRECTORY, { recursive: true });
@@ -121,24 +215,35 @@ describe('enqueueJob (e2e)', () => {
standardApplicationToken =
standardTokenData.generateApplicationToken.applicationAccessToken.token;
- const [{ data: createData }, { data: retryableCreateData }] =
- await Promise.all([
- createOneLogicFunction({
- input: { name: `enqueue-job-target-${uuidv4()}` },
- gqlFields: 'id universalIdentifier',
- expectToFail: false,
- }),
- createOneLogicFunction({
- input: { name: `enqueue-job-retryable-target-${uuidv4()}` },
- gqlFields: 'id universalIdentifier',
- expectToFail: false,
- }),
- ]);
+ const [
+ { data: createData },
+ { data: retryableCreateData },
+ { data: appendingCreateData },
+ ] = await Promise.all([
+ createOneLogicFunction({
+ input: { name: `enqueue-job-target-${uuidv4()}` },
+ gqlFields: 'id universalIdentifier',
+ expectToFail: false,
+ }),
+ createOneLogicFunction({
+ input: { name: `enqueue-job-retryable-target-${uuidv4()}` },
+ gqlFields: 'id universalIdentifier',
+ expectToFail: false,
+ }),
+ createOneLogicFunction({
+ input: { name: `enqueue-job-appending-target-${uuidv4()}` },
+ gqlFields: 'id universalIdentifier',
+ expectToFail: false,
+ }),
+ ]);
expect(createData.createOneLogicFunction.universalIdentifier).toBeDefined();
expect(
retryableCreateData.createOneLogicFunction.universalIdentifier,
).toBeDefined();
+ expect(
+ appendingCreateData.createOneLogicFunction.universalIdentifier,
+ ).toBeDefined();
logicFunctionId = createData.createOneLogicFunction.id;
logicFunctionUniversalIdentifier =
@@ -146,6 +251,9 @@ describe('enqueueJob (e2e)', () => {
retryableLogicFunctionId = retryableCreateData.createOneLogicFunction.id;
retryableLogicFunctionUniversalIdentifier =
retryableCreateData.createOneLogicFunction.universalIdentifier!;
+ appendingLogicFunctionId = appendingCreateData.createOneLogicFunction.id;
+ appendingLogicFunctionUniversalIdentifier =
+ appendingCreateData.createOneLogicFunction.universalIdentifier!;
await updateLogicFunctionSource({
input: {
@@ -155,6 +263,14 @@ describe('enqueueJob (e2e)', () => {
expectToFail: false,
});
+ await updateLogicFunctionSource({
+ input: {
+ id: appendingLogicFunctionId,
+ update: { sourceHandlerCode: APPENDING_TARGET_SOURCE_CODE },
+ },
+ expectToFail: false,
+ });
+
const { data: retryableBuildData } = await executeLogicFunction({
input: {
id: retryableLogicFunctionId,
@@ -167,6 +283,18 @@ describe('enqueueJob (e2e)', () => {
});
expect(retryableBuildData.executeOneLogicFunction.error).toBeNull();
+
+ const { data: appendingBuildData } = await executeLogicFunction({
+ input: {
+ id: appendingLogicFunctionId,
+ payload: {
+ markerPath: join(MARKER_DIRECTORY, 'appending-build.txt'),
+ },
+ },
+ expectToFail: false,
+ });
+
+ expect(appendingBuildData.executeOneLogicFunction.error).toBeNull();
});
afterAll(async () => {
@@ -179,8 +307,16 @@ describe('enqueueJob (e2e)', () => {
input: { id: retryableLogicFunctionId },
expectToFail: false,
}),
+ deleteLogicFunction({
+ input: { id: appendingLogicFunctionId },
+ expectToFail: false,
+ }),
]);
+ if (isDefined(otherWorkspaceUserAccessToken)) {
+ await deleteUser({ accessToken: otherWorkspaceUserAccessToken });
+ }
+
rmSync(MARKER_DIRECTORY, { recursive: true, force: true });
});
@@ -229,6 +365,7 @@ describe('enqueueJob (e2e)', () => {
expect(response.body.data.enqueueJob).toEqual({
enqueued: true,
logicFunctionUniversalIdentifier,
+ jobId: expect.any(String),
});
await waitForAllJobsToFinish();
@@ -352,6 +489,7 @@ describe('enqueueJob (e2e)', () => {
enqueued: true,
logicFunctionUniversalIdentifier,
enqueuedJobsCount: markerPaths.length,
+ jobIds: markerPaths.map(() => expect.any(String)),
});
await waitForAllJobsToFinish();
@@ -396,6 +534,178 @@ describe('enqueueJob (e2e)', () => {
expect(response.body.errors[0].message).toContain('payloads');
});
+ it('reports a caller-supplied job id as completed once the worker ran it', async () => {
+ const markerPath = join(MARKER_DIRECTORY, 'job-status.txt');
+ const jobId = `job-status-${uuidv4()}`;
+
+ const enqueueResponse = await makeMetadataAPIRequest(
+ {
+ query: ENQUEUE_JOBS,
+ variables: {
+ input: {
+ logicFunctionUniversalIdentifier,
+ jobs: [{ payload: { markerPath }, jobId }],
+ },
+ },
+ },
+ customApplicationToken,
+ );
+
+ expect(enqueueResponse.body.errors).toBeUndefined();
+ expect(enqueueResponse.body.data.enqueueJobs.jobIds).toEqual([jobId]);
+
+ await waitForAllJobsToFinish();
+
+ const statusResponse = await makeMetadataAPIRequest(
+ { query: GET_JOBS, variables: { jobIds: [jobId] } },
+ customApplicationToken,
+ );
+
+ expect(statusResponse.body.errors).toBeUndefined();
+ expect(statusResponse.body.data.getJobs).toHaveLength(1);
+ expect(statusResponse.body.data.getJobs[0]).toMatchObject({
+ jobId,
+ state: 'COMPLETED',
+ });
+ });
+
+ it('omits unknown job ids instead of failing the whole read', async () => {
+ const response = await makeMetadataAPIRequest(
+ { query: GET_JOBS, variables: { jobIds: [uuidv4()] } },
+ customApplicationToken,
+ );
+
+ expect(response.body.errors).toBeUndefined();
+ expect(response.body.data.getJobs).toEqual([]);
+ });
+
+ it('does not run a caller-supplied job id a second time', async () => {
+ const markerPath = join(MARKER_DIRECTORY, 'idempotent.txt');
+ const jobId = `idempotent-${uuidv4()}`;
+ const variables = {
+ input: {
+ logicFunctionUniversalIdentifier:
+ appendingLogicFunctionUniversalIdentifier,
+ jobs: [{ payload: { markerPath }, jobId }],
+ },
+ };
+
+ const firstResponse = await makeMetadataAPIRequest(
+ { query: ENQUEUE_JOBS, variables },
+ customApplicationToken,
+ );
+
+ expect(firstResponse.body.errors).toBeUndefined();
+ expect(firstResponse.body.data.enqueueJobs.jobIds).toEqual([jobId]);
+
+ await waitForAllJobsToFinish();
+
+ await expectEventually(() => {
+ expect(existsSync(markerPath)).toBe(true);
+ });
+
+ const secondResponse = await makeMetadataAPIRequest(
+ { query: ENQUEUE_JOBS, variables },
+ customApplicationToken,
+ );
+
+ expect(secondResponse.body.errors).toBeUndefined();
+ expect(secondResponse.body.data.enqueueJobs.jobIds).toEqual([jobId]);
+
+ await waitForAllJobsToFinish();
+
+ expect(readFileSync(markerPath, 'utf-8').trim().split('\n')).toEqual([
+ 'ran',
+ ]);
+ }, 60_000);
+
+ it('does not expose a job to another workspace', async () => {
+ const markerPath = join(MARKER_DIRECTORY, 'cross-workspace.txt');
+ const jobId = `cross-workspace-${uuidv4()}`;
+
+ const enqueueResponse = await makeMetadataAPIRequest(
+ {
+ query: ENQUEUE_JOBS,
+ variables: {
+ input: {
+ logicFunctionUniversalIdentifier,
+ jobs: [{ payload: { markerPath }, jobId }],
+ },
+ },
+ },
+ customApplicationToken,
+ );
+
+ expect(enqueueResponse.body.errors).toBeUndefined();
+
+ await waitForAllJobsToFinish();
+
+ await expectEventually(() => {
+ expect(existsSync(markerPath)).toBe(true);
+ });
+
+ const ownWorkspaceResponse = await makeMetadataAPIRequest(
+ { query: GET_JOBS, variables: { jobIds: [jobId] } },
+ customApplicationToken,
+ );
+
+ expect(ownWorkspaceResponse.body.errors).toBeUndefined();
+ expect(ownWorkspaceResponse.body.data.getJobs).toHaveLength(1);
+
+ const otherWorkspaceApplicationToken =
+ await createOtherWorkspaceApplicationToken();
+
+ const otherWorkspaceResponse = await makeMetadataAPIRequest(
+ { query: GET_JOBS, variables: { jobIds: [jobId] } },
+ otherWorkspaceApplicationToken,
+ );
+
+ expect(otherWorkspaceResponse.body.errors).toBeUndefined();
+ expect(otherWorkspaceResponse.body.data.getJobs).toEqual([]);
+ }, 120_000);
+
+ it('rejects a batch that repeats a caller-supplied job id', async () => {
+ const jobId = `duplicate-${uuidv4()}`;
+
+ const response = await makeMetadataAPIRequest(
+ {
+ query: ENQUEUE_JOBS,
+ variables: {
+ input: {
+ logicFunctionUniversalIdentifier,
+ jobs: [
+ { payload: {}, jobId },
+ { payload: {}, jobId },
+ ],
+ },
+ },
+ },
+ customApplicationToken,
+ );
+
+ expect(response.body.errors).toBeDefined();
+ expect(response.body.errors[0].message).toContain('unique');
+ });
+
+ it('rejects a batch that sets both payloads and jobs', async () => {
+ const response = await makeMetadataAPIRequest(
+ {
+ query: ENQUEUE_JOBS,
+ variables: {
+ input: {
+ logicFunctionUniversalIdentifier,
+ payloads: [{}],
+ jobs: [{ payload: {} }],
+ },
+ },
+ },
+ customApplicationToken,
+ );
+
+ expect(response.body.errors).toBeDefined();
+ expect(response.body.errors[0].message).toContain('not both');
+ });
+
it('rejects job options outside of their allowed range', async () => {
const response = await makeMetadataAPIRequest(
{
diff --git a/packages/twenty-shared/src/application/enqueueJobType.ts b/packages/twenty-shared/src/application/enqueueJobType.ts
index ba2de027d16..8ba40896e35 100644
--- a/packages/twenty-shared/src/application/enqueueJobType.ts
+++ b/packages/twenty-shared/src/application/enqueueJobType.ts
@@ -6,20 +6,48 @@ export type EnqueueJobOptions = {
export type EnqueueJobInput = EnqueueJobOptions & {
logicFunctionUniversalIdentifier: string;
payload?: Record<string, unknown>;
+ jobId?: string;
};
export type EnqueueJobResult = {
enqueued: boolean;
logicFunctionUniversalIdentifier: string;
+ jobId: string;
+};
+
+export type EnqueueJobItem = {
+ payload?: Record<string, unknown>;
+ jobId?: string;
};
export type EnqueueJobsInput = EnqueueJobOptions & {
logicFunctionUniversalIdentifier: string;
- payloads: Record<string, unknown>[];
+ payloads?: Record<string, unknown>[];
+ jobs?: EnqueueJobItem[];
};
export type EnqueueJobsResult = {
enqueued: boolean;
logicFunctionUniversalIdentifier: string;
enqueuedJobsCount: number;
+ jobIds: string[];
+};
+
+export type JobStatusState =
+ | 'COMPLETED'
+ | 'FAILED'
+ | 'ACTIVE'
+ | 'WAITING'
+ | 'DELAYED'
+ | 'PRIORITIZED'
+ | 'WAITING_CHILDREN';
+
+export type JobStatusResult = {
+ jobId: string;
+ state: JobStatusState;
+ attemptsMade: number;
+ failedReason?: string | null;
+ enqueuedAt: number;
+ startedAt?: number | null;
+ finishedAt?: number | null;
};
diff --git a/packages/twenty-shared/src/application/index.ts b/packages/twenty-shared/src/application/index.ts
index 0a406b7af3f..873360dd51b 100644
--- a/packages/twenty-shared/src/application/index.ts
+++ b/packages/twenty-shared/src/application/index.ts
@@ -92,8 +92,11 @@ export type {
EnqueueJobOptions,
EnqueueJobInput,
EnqueueJobResult,
+ EnqueueJobItem,
EnqueueJobsInput,
EnqueueJobsResult,
+ JobStatusState,
+ JobStatusResult,
} from './enqueueJobType';
export { SyncableEntity } from './enums/syncable-entities.enum';
export type {
diff --git a/packages/twenty-utils/graphql-introspection-query.graphql b/packages/twenty-utils/graphql-introspection-query.graphql
index 396d797aa97..a5569477f13 100644
--- a/packages/twenty-utils/graphql-introspection-query.graphql
+++ b/packages/twenty-utils/graphql-introspection-query.graphql
@@ -78,7 +78,7 @@ query IntrospectionQuery {
isDeprecated
deprecationReason
}
- inputFields {
+ inputFields(includeDeprecated: true) {
name
description
type {
@@ -114,6 +114,8 @@ query IntrospectionQuery {
}
}
defaultValue
+ isDeprecated
+ deprecationReason
}
interfaces {
kind