forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trigger workflow on database event (twentyhq#6480)
- Add global listener on database event - Fetch event listener associated - Trigger associated workflow Also updated the runner so it expects the input to be in the payload rather than the trigger
- Loading branch information
Showing
15 changed files
with
271 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ges/twenty-server/src/engine/core-modules/workflow/dtos/run-workflow-version-input.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Field, InputType } from '@nestjs/graphql'; | ||
|
||
import graphqlTypeJson from 'graphql-type-json'; | ||
|
||
@InputType() | ||
export class RunWorkflowVersionInput { | ||
@Field(() => String, { | ||
description: 'Workflow version ID', | ||
nullable: false, | ||
}) | ||
workflowVersionId: string; | ||
|
||
@Field(() => graphqlTypeJson, { | ||
description: 'Execution result in JSON format', | ||
nullable: true, | ||
}) | ||
payload?: JSON; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 28 additions & 6 deletions
34
packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.job.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,54 @@ | ||
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator'; | ||
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator'; | ||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; | ||
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator'; | ||
import { WorkflowRunnerService } from 'src/modules/workflow/workflow-runner/workflow-runner.service'; | ||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
import { WorkflowWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity'; | ||
import { WorkflowCommonService } from 'src/modules/workflow/common/workflow-common.services'; | ||
import { WorkflowRunnerService } from 'src/modules/workflow/workflow-runner/workflow-runner.service'; | ||
|
||
type RunWorkflowJobData = { workspaceId: string; workflowVersionId: string }; | ||
export type RunWorkflowJobData = { | ||
workspaceId: string; | ||
workflowId: string; | ||
payload: object; | ||
}; | ||
|
||
@Processor(MessageQueue.workflowQueue) | ||
export class WorkflowRunnerJob { | ||
constructor( | ||
private readonly workflowCommonService: WorkflowCommonService, | ||
private readonly workflowRunnerService: WorkflowRunnerService, | ||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, | ||
) {} | ||
|
||
@Process(WorkflowRunnerJob.name) | ||
async handle({ | ||
workspaceId, | ||
workflowVersionId, | ||
workflowId, | ||
payload, | ||
}: RunWorkflowJobData): Promise<void> { | ||
const workflowRepository = | ||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowWorkspaceEntity>( | ||
workspaceId, | ||
'workflow', | ||
); | ||
|
||
const workflow = await workflowRepository.findOneByOrFail({ | ||
id: workflowId, | ||
}); | ||
|
||
if (!workflow.publishedVersionId) { | ||
throw new Error('Workflow has no published version'); | ||
} | ||
|
||
const workflowVersion = await this.workflowCommonService.getWorkflowVersion( | ||
workspaceId, | ||
workflowVersionId, | ||
workflow.publishedVersionId, | ||
); | ||
|
||
await this.workflowRunnerService.run({ | ||
action: workflowVersion.trigger.nextAction, | ||
workspaceId, | ||
payload: workflowVersion.trigger.input, | ||
payload, | ||
}); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...es/twenty-server/src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Logger } from '@nestjs/common'; | ||
|
||
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator'; | ||
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator'; | ||
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator'; | ||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; | ||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; | ||
import { | ||
RunWorkflowJobData, | ||
WorkflowRunnerJob, | ||
} from 'src/modules/workflow/workflow-runner/workflow-runner.job'; | ||
|
||
export type WorkflowEventTriggerJobData = { | ||
workspaceId: string; | ||
workflowId: string; | ||
payload: object; | ||
}; | ||
|
||
@Processor(MessageQueue.workflowQueue) | ||
export class WorkflowEventTriggerJob { | ||
private readonly logger = new Logger(WorkflowEventTriggerJob.name); | ||
|
||
constructor( | ||
@InjectMessageQueue(MessageQueue.workflowQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
) {} | ||
|
||
@Process(WorkflowEventTriggerJob.name) | ||
async handle(data: WorkflowEventTriggerJobData): Promise<void> { | ||
this.messageQueueService.add<RunWorkflowJobData>(WorkflowRunnerJob.name, { | ||
workspaceId: data.workspaceId, | ||
workflowId: data.workflowId, | ||
payload: data.payload, | ||
}); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s/twenty-server/src/modules/workflow/workflow-trigger/jobs/workflow-trigger-job.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { WorkflowRunnerModule } from 'src/modules/workflow/workflow-runner/workflow-runner.module'; | ||
import { WorkflowEventTriggerJob } from 'src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job'; | ||
|
||
@Module({ | ||
imports: [WorkflowRunnerModule], | ||
providers: [WorkflowEventTriggerJob], | ||
}) | ||
export class WorkflowTriggerJobModule {} |
98 changes: 98 additions & 0 deletions
98
...server/src/modules/workflow/workflow-trigger/listeners/database-event-trigger.listener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { OnEvent } from '@nestjs/event-emitter'; | ||
|
||
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum'; | ||
import { IsFeatureEnabledService } from 'src/engine/core-modules/feature-flag/services/is-feature-enabled.service'; | ||
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event'; | ||
import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event'; | ||
import { ObjectRecordUpdateEvent } from 'src/engine/integrations/event-emitter/types/object-record-update.event'; | ||
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator'; | ||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; | ||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; | ||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-event-listener.workspace-entity'; | ||
import { | ||
WorkflowEventTriggerJob, | ||
WorkflowEventTriggerJobData, | ||
} from 'src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job'; | ||
|
||
@Injectable() | ||
export class DatabaseEventTriggerListener { | ||
private readonly logger = new Logger('DatabaseEventTriggerListener'); | ||
|
||
constructor( | ||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, | ||
@InjectMessageQueue(MessageQueue.workflowQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
private readonly isFeatureFlagEnabledService: IsFeatureEnabledService, | ||
) {} | ||
|
||
@OnEvent('*.created') | ||
async handleObjectRecordCreateEvent(payload: ObjectRecordCreateEvent<any>) { | ||
await this.handleEvent(payload); | ||
} | ||
|
||
@OnEvent('*.updated') | ||
async handleObjectRecordUpdateEvent(payload: ObjectRecordUpdateEvent<any>) { | ||
await this.handleEvent(payload); | ||
} | ||
|
||
@OnEvent('*.deleted') | ||
async handleObjectRecordDeleteEvent(payload: ObjectRecordDeleteEvent<any>) { | ||
await this.handleEvent(payload); | ||
} | ||
|
||
private async handleEvent( | ||
payload: | ||
| ObjectRecordCreateEvent<any> | ||
| ObjectRecordUpdateEvent<any> | ||
| ObjectRecordDeleteEvent<any>, | ||
) { | ||
const workspaceId = payload.workspaceId; | ||
const eventName = payload.name; | ||
|
||
if (!workspaceId || !eventName) { | ||
this.logger.error( | ||
`Missing workspaceId or eventName in payload ${JSON.stringify( | ||
payload, | ||
)}`, | ||
); | ||
|
||
return; | ||
} | ||
|
||
const isWorkflowEnabled = | ||
await this.isFeatureFlagEnabledService.isFeatureEnabled( | ||
FeatureFlagKey.IsWorkflowEnabled, | ||
workspaceId, | ||
); | ||
|
||
if (!isWorkflowEnabled) { | ||
return; | ||
} | ||
|
||
const workflowEventListenerRepository = | ||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowEventListenerWorkspaceEntity>( | ||
workspaceId, | ||
'workflowEventListener', | ||
); | ||
|
||
const eventListeners = await workflowEventListenerRepository.find({ | ||
where: { | ||
eventName, | ||
}, | ||
}); | ||
|
||
for (const eventListener of eventListeners) { | ||
this.messageQueueService.add<WorkflowEventTriggerJobData>( | ||
WorkflowEventTriggerJob.name, | ||
{ | ||
workspaceId, | ||
workflowId: eventListener.workflowId, | ||
payload, | ||
}, | ||
{ retryLimit: 3 }, | ||
); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...erver/src/modules/workflow/workflow-trigger/listeners/workflow-trigger-listener.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module'; | ||
import { DatabaseEventTriggerListener } from 'src/modules/workflow/workflow-trigger/listeners/database-event-trigger.listener'; | ||
|
||
@Module({ | ||
imports: [FeatureFlagModule], | ||
providers: [DatabaseEventTriggerListener], | ||
}) | ||
export class WorkflowTriggerListenerModule {} |
9 changes: 9 additions & 0 deletions
9
packages/twenty-server/src/modules/workflow/workflow-trigger/workflow-trigger.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { WorkflowTriggerJobModule } from 'src/modules/workflow/workflow-trigger/jobs/workflow-trigger-job.module'; | ||
import { WorkflowTriggerListenerModule } from 'src/modules/workflow/workflow-trigger/listeners/workflow-trigger-listener.module'; | ||
|
||
@Module({ | ||
imports: [WorkflowTriggerJobModule, WorkflowTriggerListenerModule], | ||
}) | ||
export class WorkflowTriggerModule {} |
Oops, something went wrong.