diff --git a/src/schedule.ts b/src/schedule.ts index 943744c..be6103a 100644 --- a/src/schedule.ts +++ b/src/schedule.ts @@ -145,8 +145,10 @@ export class Schedule { * Also updates runCount and lastRunAt. * * If the schedule has reached its limit, the job will not be dispatched. + * + * @param payload - Optional custom payload for the job */ - async trigger(): Promise { + async trigger(payload?: any): Promise { // Check if limit is reached if (this.#data.limit !== null && this.#data.runCount >= this.#data.limit) { return @@ -155,7 +157,7 @@ export class Schedule { const adapter = QueueManager.use() // Dispatch the job - const dispatcher = new JobDispatcher(this.#data.name, this.#data.payload) + const dispatcher = new JobDispatcher(this.#data.name, payload ?? this.#data.payload) await dispatcher.run() // Update run metadata diff --git a/tests/schedule.spec.ts b/tests/schedule.spec.ts index 2e2d6eb..1e99417 100644 --- a/tests/schedule.spec.ts +++ b/tests/schedule.spec.ts @@ -353,6 +353,22 @@ test.group('Schedule', (group) => { assert.isNull(job2) }) + test('schedule.trigger(payload) should dispatch job with custom payload', async ({ assert }) => { + await new ScheduleBuilder('TriggerJob', { immediate: true, custom: false }) + .id('triggerable') + .every('1h') + .run() + + const schedule = await Schedule.find('triggerable') + await schedule!.trigger({ immediate: true, custom: true }) + + // Job should be in the queue + const job = await sharedAdapter.pop() + assert.isNotNull(job) + assert.equal(job!.name, 'TriggerJob') + assert.deepEqual(job!.payload, { immediate: true, custom: true }) + }) + test('schedule properties should reflect data', async ({ assert }) => { const fromDate = new Date('2025-01-01') const toDate = new Date('2025-12-31')