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.
feat: Enhancements to MessageQueue Module with Decorators (twentyhq#5657
) ### Overview This PR introduces significant enhancements to the MessageQueue module by integrating `@Processor`, `@Process`, and `@InjectMessageQueue` decorators. These changes streamline the process of defining and managing queue processors and job handlers, and also allow for request-scoped handlers, improving compatibility with services that rely on scoped providers like TwentyORM repositories. ### Key Features 1. **Decorator-based Job Handling**: Use `@Processor` and `@Process` decorators to define job handlers declaratively. 2. **Request Scope Support**: Job handlers can be scoped per request, enhancing integration with request-scoped services. ### Usage #### Defining Processors and Job Handlers The `@Processor` decorator is used to define a class that processes jobs for a specific queue. The `@Process` decorator is applied to methods within this class to define specific job handlers. ##### Example 1: Specific Job Handlers ```typescript import { Processor, Process, InjectMessageQueue } from 'src/engine/integrations/message-queue'; @processor('taskQueue') export class TaskProcessor { @process('taskA') async handleTaskA(job: { id: string, data: any }) { console.log(`Handling task A with data:`, job.data); // Logic for task A } @process('taskB') async handleTaskB(job: { id: string, data: any }) { console.log(`Handling task B with data:`, job.data); // Logic for task B } } ``` In the example above, `TaskProcessor` is responsible for processing jobs in the `taskQueue`. The `handleTaskA` method will only be called for jobs with the name `taskA`, while `handleTaskB` will be called for `taskB` jobs. ##### Example 2: General Job Handler ```typescript import { Processor, Process, InjectMessageQueue } from 'src/engine/integrations/message-queue'; @processor('generalQueue') export class GeneralProcessor { @process() async handleAnyJob(job: { id: string, name: string, data: any }) { console.log(`Handling job ${job.name} with data:`, job.data); // Logic for any job } } ``` In this example, `GeneralProcessor` handles all jobs in the `generalQueue`, regardless of the job name. The `handleAnyJob` method will be invoked for every job added to the `generalQueue`. #### Adding Jobs to a Queue You can use the `@InjectMessageQueue` decorator to inject a queue into a service and add jobs to it. ##### Example: ```typescript import { Injectable } from '@nestjs/common'; import { InjectMessageQueue, MessageQueue } from 'src/engine/integrations/message-queue'; @Injectable() export class TaskService { constructor( @InjectMessageQueue('taskQueue') private readonly taskQueue: MessageQueue, ) {} async addTaskA(data: any) { await this.taskQueue.add('taskA', data); } async addTaskB(data: any) { await this.taskQueue.add('taskB', data); } } ``` In this example, `TaskService` adds jobs to the `taskQueue`. The `addTaskA` and `addTaskB` methods add jobs named `taskA` and `taskB`, respectively, to the queue. #### Using Scoped Job Handlers To utilize request-scoped job handlers, specify the scope in the `@Processor` decorator. This is particularly useful for services that use scoped repositories like those in TwentyORM. ##### Example: ```typescript import { Processor, Process, InjectMessageQueue, Scope } from 'src/engine/integrations/message-queue'; @processor({ name: 'scopedQueue', scope: Scope.REQUEST }) export class ScopedTaskProcessor { @process('scopedTask') async handleScopedTask(job: { id: string, data: any }) { console.log(`Handling scoped task with data:`, job.data); // Logic for scoped task, which might use request-scoped services } } ``` Here, the `ScopedTaskProcessor` is associated with `scopedQueue` and operates with request scope. This setup is essential when the job handler relies on services that need to be instantiated per request, such as scoped repositories. ### Migration Notes - **Decorators**: Refactor job handlers to use `@Processor` and `@Process` decorators. - **Request Scope**: Utilize the scope option in `@Processor` if your job handlers depend on request-scoped services. Fix twentyhq#5628 --------- Co-authored-by: Weiko <[email protected]>
- Loading branch information
Showing
92 changed files
with
952 additions
and
521 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
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
12 changes: 6 additions & 6 deletions
12
...erver/src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.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
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
14 changes: 6 additions & 8 deletions
14
...server/src/engine/api/graphql/workspace-query-runner/jobs/record-position-backfill.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
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
16 changes: 9 additions & 7 deletions
16
packages/twenty-server/src/engine/core-modules/billing/jobs/update-subscription.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
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
Oops, something went wrong.