|
| 1 | +# Adapter-Aware Workers and Schedules |
| 2 | + |
| 3 | +## New Features |
| 4 | + |
| 5 | +### Select an Adapter per Worker |
| 6 | + |
| 7 | +Workers can now listen on a specific registered Adapter with `worker.adapter`. When omitted, the |
| 8 | +worker continues to use the queue manager's default Adapter. |
| 9 | + |
| 10 | +```typescript |
| 11 | +const config = { |
| 12 | + default: 'redis', |
| 13 | + adapters: { |
| 14 | + redis: redis(redisConfig), |
| 15 | + database: knex(databaseConfig), |
| 16 | + }, |
| 17 | + worker: { |
| 18 | + adapter: 'database', |
| 19 | + concurrency: 5, |
| 20 | + }, |
| 21 | +} |
| 22 | + |
| 23 | +const worker = new Worker(config) |
| 24 | +await worker.start(['default', 'emails']) |
| 25 | +``` |
| 26 | + |
| 27 | +This makes it possible to run separate workers for queues stored by different Adapters. |
| 28 | + |
| 29 | +### Store and Access Schedules on a Specific Adapter |
| 30 | + |
| 31 | +Schedules can now select their owning Adapter with `.with()`: |
| 32 | + |
| 33 | +```typescript |
| 34 | +await CleanupJob.schedule({ days: 30 }).id('daily-cleanup').with('redis').cron('0 0 * * *') |
| 35 | +``` |
| 36 | + |
| 37 | +`Schedule.find()` and `Schedule.list()` accept an Adapter selector when accessing schedules outside |
| 38 | +the default Adapter: |
| 39 | + |
| 40 | +```typescript |
| 41 | +const schedule = await Schedule.find('daily-cleanup', { adapter: 'redis' }) |
| 42 | +const schedules = await Schedule.list({ status: 'active' }, { adapter: 'redis' }) |
| 43 | +``` |
| 44 | + |
| 45 | +A returned `Schedule` retains the selected Adapter for subsequent `pause()`, `resume()`, `delete()`, |
| 46 | +and `trigger()` calls. Jobs dispatched by a schedule stay on the Adapter that owns that schedule. |
| 47 | + |
| 48 | +### Identify Jobs Dispatched by a Schedule |
| 49 | + |
| 50 | +Scheduled jobs now include their originating schedule ID in `JobData.scheduleId`. Jobs can access it |
| 51 | +while executing through `this.context.scheduleId`: |
| 52 | + |
| 53 | +```typescript |
| 54 | +async execute() { |
| 55 | + console.log(this.context.scheduleId) |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +The value is `undefined` for jobs that were not dispatched by a schedule. |
| 60 | + |
| 61 | +## Upgrade Notes |
| 62 | + |
| 63 | +Start a Worker for every Adapter that owns schedules. A Worker only claims schedules and jobs from |
| 64 | +its configured Adapter. |
| 65 | + |
| 66 | +When a schedule does not call `.with()`, its Adapter is resolved from the job's `adapter` option, |
| 67 | +then from the Adapter configured for the job's queue, and finally from the queue manager default. |
| 68 | +An explicit `.with()` always takes precedence. |
0 commit comments