Skip to content

Commit de8118c

Browse files
committed
fix(redis): clear stale schedule fields on upsert
1 parent 8110dc2 commit de8118c

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

src/drivers/redis_adapter.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ export class RedisAdapter implements Adapter {
703703
async createSchedule(config: ScheduleConfig): Promise<string> {
704704
const id = config.id ?? randomUUID()
705705
const now = Date.now()
706+
const scheduleKey = `${schedulesKey}::${id}`
706707

707708
const scheduleData: Record<string, string> = {
708709
id,
@@ -714,18 +715,19 @@ export class RedisAdapter implements Adapter {
714715
created_at: now.toString(),
715716
}
716717

717-
if (config.cronExpression) scheduleData.cron_expression = config.cronExpression
718-
if (config.everyMs) scheduleData.every_ms = config.everyMs.toString()
719-
if (config.from) scheduleData.from_date = config.from.getTime().toString()
720-
if (config.to) scheduleData.to_date = config.to.getTime().toString()
721-
if (config.limit) scheduleData.run_limit = config.limit.toString()
722-
723-
// Store schedule as hash
724-
const scheduleKey = `${schedulesKey}::${id}`
725-
await this.#connection.hset(scheduleKey, scheduleData)
726-
727-
// Add to index set for listing
728-
await this.#connection.sadd(schedulesIndexKey, id)
718+
if (config.cronExpression !== undefined) scheduleData.cron_expression = config.cronExpression
719+
if (config.everyMs !== undefined) scheduleData.every_ms = config.everyMs.toString()
720+
if (config.from !== undefined) scheduleData.from_date = config.from.getTime().toString()
721+
if (config.to !== undefined) scheduleData.to_date = config.to.getTime().toString()
722+
if (config.limit !== undefined) scheduleData.run_limit = config.limit.toString()
723+
724+
// Upsert schedule and clear stale optional fields from previous config.
725+
await this.#connection
726+
.multi()
727+
.hdel(scheduleKey, 'cron_expression', 'every_ms', 'from_date', 'to_date', 'run_limit')
728+
.hset(scheduleKey, scheduleData)
729+
.sadd(schedulesIndexKey, id)
730+
.exec()
729731

730732
return id
731733
}

tests/_utils/register_driver_test_suite.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,41 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
11321132
assert.equal(schedule!.timezone, 'Europe/Paris')
11331133
})
11341134

1135+
test('createSchedule upsert should clear stale scheduling fields', async ({ assert }) => {
1136+
const adapter = await options.createAdapter()
1137+
1138+
const from = new Date('2024-01-01T00:00:00.000Z')
1139+
const to = new Date('2024-12-31T23:59:59.999Z')
1140+
1141+
await adapter.createSchedule({
1142+
id: 'upsert-stale-fields',
1143+
name: 'TestJob',
1144+
payload: { version: 1 },
1145+
cronExpression: '0 0 * * *',
1146+
timezone: 'UTC',
1147+
from,
1148+
to,
1149+
limit: 10,
1150+
})
1151+
1152+
await adapter.createSchedule({
1153+
id: 'upsert-stale-fields',
1154+
name: 'TestJob',
1155+
payload: { version: 2 },
1156+
everyMs: 30000,
1157+
timezone: 'UTC',
1158+
})
1159+
1160+
const schedule = await adapter.getSchedule('upsert-stale-fields')
1161+
assert.isNotNull(schedule)
1162+
assert.deepEqual(schedule!.payload, { version: 2 })
1163+
assert.equal(schedule!.everyMs, 30000)
1164+
assert.isNull(schedule!.cronExpression)
1165+
assert.isNull(schedule!.from)
1166+
assert.isNull(schedule!.to)
1167+
assert.isNull(schedule!.limit)
1168+
})
1169+
11351170
test('getSchedule should return null for non-existent schedule', async ({ assert }) => {
11361171
const adapter = await options.createAdapter()
11371172

0 commit comments

Comments
 (0)