Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
async trigger(payload?: any): Promise<void> {
// Check if limit is reached
if (this.#data.limit !== null && this.#data.runCount >= this.#data.limit) {
return
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/schedule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading