Skip to content

Commit c26de83

Browse files
authored
feat(scheduler): allow custom payload with manual trigger (#7)
1 parent bc8dc23 commit c26de83

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/schedule.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ export class Schedule {
145145
* Also updates runCount and lastRunAt.
146146
*
147147
* If the schedule has reached its limit, the job will not be dispatched.
148+
*
149+
* @param payload - Optional custom payload for the job
148150
*/
149-
async trigger(): Promise<void> {
151+
async trigger(payload?: any): Promise<void> {
150152
// Check if limit is reached
151153
if (this.#data.limit !== null && this.#data.runCount >= this.#data.limit) {
152154
return
@@ -155,7 +157,7 @@ export class Schedule {
155157
const adapter = QueueManager.use()
156158

157159
// Dispatch the job
158-
const dispatcher = new JobDispatcher(this.#data.name, this.#data.payload)
160+
const dispatcher = new JobDispatcher(this.#data.name, payload ?? this.#data.payload)
159161
await dispatcher.run()
160162

161163
// Update run metadata

tests/schedule.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,22 @@ test.group('Schedule', (group) => {
353353
assert.isNull(job2)
354354
})
355355

356+
test('schedule.trigger(payload) should dispatch job with custom payload', async ({ assert }) => {
357+
await new ScheduleBuilder('TriggerJob', { immediate: true, custom: false })
358+
.id('triggerable')
359+
.every('1h')
360+
.run()
361+
362+
const schedule = await Schedule.find('triggerable')
363+
await schedule!.trigger({ immediate: true, custom: true })
364+
365+
// Job should be in the queue
366+
const job = await sharedAdapter.pop()
367+
assert.isNotNull(job)
368+
assert.equal(job!.name, 'TriggerJob')
369+
assert.deepEqual(job!.payload, { immediate: true, custom: true })
370+
})
371+
356372
test('schedule properties should reflect data', async ({ assert }) => {
357373
const fromDate = new Date('2025-01-01')
358374
const toDate = new Date('2025-12-31')

0 commit comments

Comments
 (0)