Skip to content

Commit 29d6062

Browse files
committed
refactor: rename to upsertSchedule and deprecate createSchedule
1 parent de8118c commit 29d6062

File tree

9 files changed

+82
-38
lines changed

9 files changed

+82
-38
lines changed

src/contracts/adapter.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ export interface Adapter {
198198
* @param config - The schedule configuration
199199
* @returns The schedule ID
200200
*/
201+
upsertSchedule(config: ScheduleConfig): Promise<string>
202+
203+
/**
204+
* Create or update a schedule.
205+
*
206+
* @deprecated Use `upsertSchedule` instead.
207+
* @param config - The schedule configuration
208+
* @returns The schedule ID
209+
*/
201210
createSchedule(config: ScheduleConfig): Promise<string>
202211

203212
/**

src/drivers/fake_adapter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ export class FakeAdapter implements Adapter {
343343
return Promise.resolve()
344344
}
345345

346-
async createSchedule(config: ScheduleConfig): Promise<string> {
346+
async upsertSchedule(config: ScheduleConfig): Promise<string> {
347347
const id = config.id ?? randomUUID()
348348
const now = new Date()
349349

@@ -368,6 +368,13 @@ export class FakeAdapter implements Adapter {
368368
return id
369369
}
370370

371+
/**
372+
* @deprecated Use `upsertSchedule` instead.
373+
*/
374+
createSchedule(config: ScheduleConfig): Promise<string> {
375+
return this.upsertSchedule(config)
376+
}
377+
371378
async getSchedule(id: string): Promise<ScheduleData | null> {
372379
return this.#schedules.get(id) ?? null
373380
}

src/drivers/knex_adapter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ export class KnexAdapter implements Adapter {
469469
})
470470
}
471471

472-
async createSchedule(config: ScheduleConfig): Promise<string> {
472+
async upsertSchedule(config: ScheduleConfig): Promise<string> {
473473
const id = config.id ?? randomUUID()
474474

475475
const data = {
@@ -508,6 +508,13 @@ export class KnexAdapter implements Adapter {
508508
return id
509509
}
510510

511+
/**
512+
* @deprecated Use `upsertSchedule` instead.
513+
*/
514+
createSchedule(config: ScheduleConfig): Promise<string> {
515+
return this.upsertSchedule(config)
516+
}
517+
511518
async getSchedule(id: string): Promise<ScheduleData | null> {
512519
const row = await this.#connection(this.#schedulesTable)
513520
.where('id', id)

src/drivers/redis_adapter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ export class RedisAdapter implements Adapter {
700700
return recovered as number
701701
}
702702

703-
async createSchedule(config: ScheduleConfig): Promise<string> {
703+
async upsertSchedule(config: ScheduleConfig): Promise<string> {
704704
const id = config.id ?? randomUUID()
705705
const now = Date.now()
706706
const scheduleKey = `${schedulesKey}::${id}`
@@ -732,6 +732,13 @@ export class RedisAdapter implements Adapter {
732732
return id
733733
}
734734

735+
/**
736+
* @deprecated Use `upsertSchedule` instead.
737+
*/
738+
createSchedule(config: ScheduleConfig): Promise<string> {
739+
return this.upsertSchedule(config)
740+
}
741+
735742
async getSchedule(id: string): Promise<ScheduleData | null> {
736743
const scheduleKey = `${schedulesKey}::${id}`
737744
const data = await this.#connection.hgetall(scheduleKey)

src/drivers/sync_adapter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,19 @@ export class SyncAdapter implements Adapter {
105105
return Promise.resolve()
106106
}
107107

108-
createSchedule(_config: ScheduleConfig): Promise<string> {
108+
upsertSchedule(_config: ScheduleConfig): Promise<string> {
109109
// No-op: schedules don't make sense for sync adapter
110110
// Return a fake ID so code doesn't break in dev
111111
return Promise.resolve(`sync-schedule-${Date.now()}`)
112112
}
113113

114+
/**
115+
* @deprecated Use `upsertSchedule` instead.
116+
*/
117+
createSchedule(config: ScheduleConfig): Promise<string> {
118+
return this.upsertSchedule(config)
119+
}
120+
114121
getSchedule(_id: string): Promise<ScheduleData | null> {
115122
return Promise.resolve(null)
116123
}

src/schedule_builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export class ScheduleBuilder implements PromiseLike<ScheduleResult> {
153153
}
154154

155155
const adapter = QueueManager.use()
156-
const scheduleId = await adapter.createSchedule(config)
156+
const scheduleId = await adapter.upsertSchedule(config)
157157

158158
// Calculate and set nextRunAt
159159
const nextRunAt = this.#calculateNextRunAt()

tests/_mocks/memory_adapter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export class MemoryAdapter implements Adapter {
257257
return Promise.resolve()
258258
}
259259

260-
async createSchedule(config: ScheduleConfig): Promise<string> {
260+
async upsertSchedule(config: ScheduleConfig): Promise<string> {
261261
const id = config.id ?? randomUUID()
262262
const now = new Date()
263263

@@ -282,6 +282,13 @@ export class MemoryAdapter implements Adapter {
282282
return id
283283
}
284284

285+
/**
286+
* @deprecated Use `upsertSchedule` instead.
287+
*/
288+
createSchedule(config: ScheduleConfig): Promise<string> {
289+
return this.upsertSchedule(config)
290+
}
291+
285292
async getSchedule(id: string): Promise<ScheduleData | null> {
286293
return this.#schedules.get(id) ?? null
287294
}

tests/_utils/register_driver_test_suite.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,10 +1067,10 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
10671067
})
10681068
}
10691069

1070-
test('createSchedule should create a new schedule', async ({ assert }) => {
1070+
test('upsertSchedule should create a new schedule', async ({ assert }) => {
10711071
const adapter = await options.createAdapter()
10721072

1073-
const id = await adapter.createSchedule({
1073+
const id = await adapter.upsertSchedule({
10741074
name: 'TestJob',
10751075
payload: { foo: 'bar' },
10761076
everyMs: 5000,
@@ -1087,10 +1087,10 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
10871087
assert.equal(schedule!.status, 'active')
10881088
})
10891089

1090-
test('createSchedule should use provided id', async ({ assert }) => {
1090+
test('upsertSchedule should use provided id', async ({ assert }) => {
10911091
const adapter = await options.createAdapter()
10921092

1093-
const id = await adapter.createSchedule({
1093+
const id = await adapter.upsertSchedule({
10941094
id: 'my-custom-id',
10951095
name: 'TestJob',
10961096
payload: {},
@@ -1105,11 +1105,11 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
11051105
assert.equal(schedule!.cronExpression, '0 0 * * *')
11061106
})
11071107

1108-
test('createSchedule should upsert when id exists', async ({ assert }) => {
1108+
test('upsertSchedule should upsert when id exists', async ({ assert }) => {
11091109
const adapter = await options.createAdapter()
11101110

11111111
// Create initial schedule
1112-
await adapter.createSchedule({
1112+
await adapter.upsertSchedule({
11131113
id: 'upsert-test',
11141114
name: 'TestJob',
11151115
payload: { version: 1 },
@@ -1118,7 +1118,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
11181118
})
11191119

11201120
// Upsert with new values
1121-
await adapter.createSchedule({
1121+
await adapter.upsertSchedule({
11221122
id: 'upsert-test',
11231123
name: 'TestJob',
11241124
payload: { version: 2 },
@@ -1132,13 +1132,13 @@ 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 }) => {
1135+
test('upsertSchedule upsert should clear stale scheduling fields', async ({ assert }) => {
11361136
const adapter = await options.createAdapter()
11371137

11381138
const from = new Date('2024-01-01T00:00:00.000Z')
11391139
const to = new Date('2024-12-31T23:59:59.999Z')
11401140

1141-
await adapter.createSchedule({
1141+
await adapter.upsertSchedule({
11421142
id: 'upsert-stale-fields',
11431143
name: 'TestJob',
11441144
payload: { version: 1 },
@@ -1149,7 +1149,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
11491149
limit: 10,
11501150
})
11511151

1152-
await adapter.createSchedule({
1152+
await adapter.upsertSchedule({
11531153
id: 'upsert-stale-fields',
11541154
name: 'TestJob',
11551155
payload: { version: 2 },
@@ -1177,14 +1177,14 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
11771177
test('listSchedules should return all schedules', async ({ assert }) => {
11781178
const adapter = await options.createAdapter()
11791179

1180-
await adapter.createSchedule({
1180+
await adapter.upsertSchedule({
11811181
id: 'list-test-1',
11821182
name: 'Job1',
11831183
payload: {},
11841184
everyMs: 5000,
11851185
timezone: 'UTC',
11861186
})
1187-
await adapter.createSchedule({
1187+
await adapter.upsertSchedule({
11881188
id: 'list-test-2',
11891189
name: 'Job2',
11901190
payload: {},
@@ -1202,14 +1202,14 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
12021202
test('listSchedules should filter by status', async ({ assert }) => {
12031203
const adapter = await options.createAdapter()
12041204

1205-
await adapter.createSchedule({
1205+
await adapter.upsertSchedule({
12061206
id: 'filter-active',
12071207
name: 'Job1',
12081208
payload: {},
12091209
everyMs: 5000,
12101210
timezone: 'UTC',
12111211
})
1212-
await adapter.createSchedule({
1212+
await adapter.upsertSchedule({
12131213
id: 'filter-paused',
12141214
name: 'Job2',
12151215
payload: {},
@@ -1231,7 +1231,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
12311231
test('updateSchedule should update status', async ({ assert }) => {
12321232
const adapter = await options.createAdapter()
12331233

1234-
await adapter.createSchedule({
1234+
await adapter.upsertSchedule({
12351235
id: 'update-status-test',
12361236
name: 'TestJob',
12371237
payload: {},
@@ -1248,7 +1248,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
12481248
test('updateSchedule should update run metadata', async ({ assert }) => {
12491249
const adapter = await options.createAdapter()
12501250

1251-
await adapter.createSchedule({
1251+
await adapter.upsertSchedule({
12521252
id: 'update-meta-test',
12531253
name: 'TestJob',
12541254
payload: {},
@@ -1274,7 +1274,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
12741274
test('deleteSchedule should remove schedule', async ({ assert }) => {
12751275
const adapter = await options.createAdapter()
12761276

1277-
await adapter.createSchedule({
1277+
await adapter.upsertSchedule({
12781278
id: 'delete-test',
12791279
name: 'TestJob',
12801280
payload: {},
@@ -1292,7 +1292,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
12921292
const adapter = await options.createAdapter()
12931293

12941294
// Create schedule with nextRunAt in the future
1295-
await adapter.createSchedule({
1295+
await adapter.upsertSchedule({
12961296
id: 'future-schedule',
12971297
name: 'TestJob',
12981298
payload: {},
@@ -1311,7 +1311,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
13111311
test('claimDueSchedule should claim a due schedule', async ({ assert }) => {
13121312
const adapter = await options.createAdapter()
13131313

1314-
await adapter.createSchedule({
1314+
await adapter.upsertSchedule({
13151315
id: 'due-schedule',
13161316
name: 'DueJob',
13171317
payload: { key: 'value' },
@@ -1335,7 +1335,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
13351335
test('claimDueSchedule should update nextRunAt after claiming', async ({ assert }) => {
13361336
const adapter = await options.createAdapter()
13371337

1338-
await adapter.createSchedule({
1338+
await adapter.upsertSchedule({
13391339
id: 'claim-update-test',
13401340
name: 'TestJob',
13411341
payload: {},
@@ -1356,7 +1356,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
13561356
test('claimDueSchedule should increment runCount', async ({ assert }) => {
13571357
const adapter = await options.createAdapter()
13581358

1359-
await adapter.createSchedule({
1359+
await adapter.upsertSchedule({
13601360
id: 'runcount-test',
13611361
name: 'TestJob',
13621362
payload: {},
@@ -1380,7 +1380,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
13801380
test('claimDueSchedule should not claim paused schedules', async ({ assert }) => {
13811381
const adapter = await options.createAdapter()
13821382

1383-
await adapter.createSchedule({
1383+
await adapter.upsertSchedule({
13841384
id: 'paused-claim-test',
13851385
name: 'TestJob',
13861386
payload: {},
@@ -1400,7 +1400,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
14001400
test('claimDueSchedule should not claim when limit reached', async ({ assert }) => {
14011401
const adapter = await options.createAdapter()
14021402

1403-
await adapter.createSchedule({
1403+
await adapter.upsertSchedule({
14041404
id: 'limit-claim-test',
14051405
name: 'TestJob',
14061406
payload: {},
@@ -1425,7 +1425,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
14251425
const adapter2 = await options.createAdapter()
14261426

14271427
// Create a single due schedule
1428-
await adapter1.createSchedule({
1428+
await adapter1.upsertSchedule({
14291429
id: 'concurrent-claim-test',
14301430
name: 'TestJob',
14311431
payload: {},
@@ -1452,7 +1452,7 @@ export function registerDriverTestSuite(options: DriverTestSuiteOptions) {
14521452
const adapters = await Promise.all(Array.from({ length: 10 }, () => options.createAdapter()))
14531453

14541454
// Create a single due schedule
1455-
await adapters[0].createSchedule({
1455+
await adapters[0].upsertSchedule({
14561456
id: 'stress-test-schedule',
14571457
name: 'StressJob',
14581458
payload: { test: true },

0 commit comments

Comments
 (0)