### Installing Dependencies with npm Source: https://github.com/jongolden/agenda-nest/blob/main/examples/multiple-queues/README.md Installs the project dependencies listed in the package.json file using the npm package manager. ```bash $ npm install ``` -------------------------------- ### Running the NestJS Application Source: https://github.com/jongolden/agenda-nest/blob/main/examples/multiple-queues/README.md Provides commands to start the NestJS application in different modes: development, watch mode for automatic restarts, and production. ```bash # development $ npm run start # watch mode $ npm run start:dev # production mode $ npm run start:prod ``` -------------------------------- ### Install Agenda Nest Package Source: https://github.com/jongolden/agenda-nest/blob/main/README.md This command installs the agenda-nest package using npm, adding it to your project's dependencies. ```bash npm install agenda-nest ``` -------------------------------- ### Running Tests for the NestJS Application Source: https://github.com/jongolden/agenda-nest/blob/main/examples/multiple-queues/README.md Shows commands to execute different types of tests for the project: unit tests, end-to-end tests, and generate test coverage reports. ```bash # unit tests $ npm run test # e2e tests $ npm run test:e2e # test coverage $ npm run test:cov ``` -------------------------------- ### Configure AgendaModule Asynchronously Source: https://github.com/jongolden/agenda-nest/blob/main/README.md This example demonstrates asynchronous configuration of AgendaModule using forRootAsync. It leverages a useFactory function with dependency injection (ConfigService) to load configuration from a configuration service. ```typescript import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { AgendaModule } from 'agenda-nest'; import configuration from './configuration'; @Module({ imports: [ ConfigModule.forRoot({ load: [configuration], }), AgendaModule.forRootAsync({ useFactory: (config: ConfigService) => ({ processEvery: config.get('queues.processInterval'), db: { address: config.get('database.connectionString'), }, }), inject: [ConfigService], }) ], providers: [Jobs], }) export class AppModule {} ``` -------------------------------- ### Register Queue Asynchronously Source: https://github.com/jongolden/agenda-nest/blob/main/README.md This example demonstrates asynchronous queue registration using AgendaModule.registerQueueAsync. It uses a useFactory function with dependency injection (ConfigService) to configure the queue based on application configuration. ```typescript import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { AgendaModule } from 'agenda-nest'; @Module({ imports: [ AgendaModule.registerQueueAsync('notifications', { useFactory: (config: ConfigService) => ({ processEvery: config.get('queues.notifications.processInterval'), autoStart: config.get('queues.notifications.autoStart'), }), inject: [ConfigService], }), ], }) export class NotificationsModule {} ``` -------------------------------- ### Define Job Method with @Define Decorator Source: https://github.com/jongolden/agenda-nest/blob/main/README.md This example shows how to define a method within a @Queue decorated class as a job processor using the @Define() decorator. This registers the job definition with Agenda but does not schedule it. ```typescript import { Define, Queue, Job } from 'agenda-nest'; @Queue('notifications') export class NotificationsQueue { @Define() sendNotification(job: Job) {} } ``` -------------------------------- ### Scheduling Immediate Jobs with @Now (JS) Source: https://github.com/jongolden/agenda-nest/blob/main/README.md The @Now decorator is used to define a job that should be scheduled to run immediately upon application startup or when the queue is ready. It can optionally accept a job name. The decorated method will be executed by Agenda as soon as possible. ```js import { Now, Queue, Job } from 'agenda-nest'; @Queue('dance') export class DanceQueue { @Now() async doTheHokeyPokey(job: Job) { hokeyPokey(); } @Now('do the cha-cha') async doTheChaCha(job: Job) { chaCha(); } } ``` -------------------------------- ### Configure AgendaModule Synchronously Source: https://github.com/jongolden/agenda-nest/blob/main/README.md This snippet shows how to configure the AgendaModule synchronously in your NestJS application's root module. It uses the forRoot method with a configuration object, including database connection details. ```typescript import { Module } from '@nestjs/common'; import { AgendaModule } from 'agenda-nest'; @Module({ imports: [ AgendaModule.forRoot({ processEvery: '3 minutes', db: { addresss: 'mongodb://localhost:27017', }, }), ], providers: [Jobs], }) export class AppModule {} ``` -------------------------------- ### Handling Queue Ready Event with @OnQueueReady (JS) Source: https://github.com/jongolden/agenda-nest/blob/main/README.md Event listeners in agenda-nest are defined within @Queue decorated classes using specific decorators for different Agenda events. The @OnQueueReady decorator is used to define a method that will be called when the Agenda queue's MongoDB connection is successfully opened and indices are created. ```js import { OnQueueReady } from 'agenda-nest'; import { Job } from 'agenda'; @Queue() export class JobsQueue { @OnQueueReady() onReady() { console.log('Jobs queue is ready to run our jobs'); } ... } ``` -------------------------------- ### Register Queue Synchronously Source: https://github.com/jongolden/agenda-nest/blob/main/README.md This snippet shows how to register an additional queue synchronously using AgendaModule.registerQueue. It takes the queue name and an optional configuration object that overrides the root configuration. ```typescript import { Module } from '@nestjs/common'; import { AgendaModule } from 'agenda-nest'; @Module({ imports: [ AgendaModule.registerQueue('notifications', { processEvery: '5 minutes', autoStart: false, // default: true collection: 'notificationsqueue', // default: notifications-queue (`${queueName}-queue`) }), ], }) export class NotificationsModule {} ``` -------------------------------- ### Defining Interval Jobs with @Every (JS) Source: https://github.com/jongolden/agenda-nest/blob/main/README.md The @Every decorator is used to define a job that should run repeatedly at a specified interval. It can accept a simple interval string or an options object including a name and interval. The decorated method will be executed by Agenda whenever the job is due. ```js import { Every, Queue, Job } from 'agenda-nest'; @Queue('notifications') export class NotificationsQueue { @Every({ name: 'send notifications', interval: '15 minutes' }) async sendNotifications(job: Job) { const users = await User.doSomethingReallyIntensive(); sendNotification(users, "Welcome!"); } } @Queue('reports') export class ReportsQueue { @Every('15 minutes') async printAnalyticsReport(job: Job) { const users = await User.doSomethingReallyIntensive(); processUserData(users); } } ``` -------------------------------- ### Injecting and Using Agenda Queue Manually (JS) Source: https://github.com/jongolden/agenda-nest/blob/main/README.md The @InjectQueue(queueName) decorator allows injecting the underlying Agenda instance for a specific queue into any injectable class (like a service). This provides direct access to the full Agenda API for manually scheduling, cancelling, or managing jobs. ```js @Injectable() export class NotificationsService { constructor(@InjectQueue('notificiations') private queue: Agenda) {} async scheduleNotification(sendAt: string) { await this.queue.schedule('tomorrow at noon', 'sendNotification', { to: 'user@example.com', }); } } ``` -------------------------------- ### Define Job Processor Class with @Queue Decorator Source: https://github.com/jongolden/agenda-nest/blob/main/README.md This snippet shows how to define a class as a job processor for a specific queue using the @Queue decorator. It demonstrates specifying the queue name and optionally a custom MongoDB collection name. ```typescript import { Queue } from 'agenda-nest'; // will use a "notifications-queue" collection @Queue('notifications') export class NotificationsQueue {} // with custom collection name @Queue('notifications', { collection: 'notificationsqueue' }) export class NotificationsQueue {} ``` -------------------------------- ### Scheduling One-Time Jobs with @Schedule (JS) Source: https://github.com/jongolden/agenda-nest/blob/main/README.md The @Schedule decorator is used to define a job that should run only once at a specific time. It accepts a time string or an options object including a name and the 'when' time. The decorated method will be executed by Agenda at the scheduled time. ```js import { Schedule, Queue, Job } from 'agenda-nest'; @Queue('notifications') export class NotificationsQueue { @Scheduler({ name: 'send notifications', when: 'tomorrow at noon' }) async sendNotifications(job: Job) { const users = await User.doSomethingReallyIntensive(); sendNotification(users, "Welcome!"); } } @Queue('reports') export class ReportsQueue { @Schedule('tomorrow at noon') async printAnalyticsReport(job: Job) { const users = await User.doSomethingReallyIntensive(); processUserData(users); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.