Adding the least between current and previous next_run_start_at to merge conflict when scheduler updates

Signed-off-by: Yobannys Cabrera Gonzalez <ycabrera@webstaurantstore.com>
This commit is contained in:
Yobannys Cabrera Gonzalez
2023-09-12 09:27:05 -04:00
committed by Fredrik Adelöw
parent 6d99b16b28
commit 8fd91547cd
3 changed files with 56 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-tasks': patch
---
Fixed bug in backend TaskWorker, 'next_run_start_at' will be always the least between schedule changes.
@@ -332,4 +332,41 @@ describe('TaskWorker', () => {
expect(fn1.mock.calls.length).toBeGreaterThan(before);
},
);
it.each(databases.eachSupportedId())(
'next_run_start_at is always the min between schedule changes, %p',
async databaseId => {
const knex = await databases.init(databaseId);
await migrateBackendTasks(knex);
const fn = jest.fn(
async () => new Promise<void>(resolve => setTimeout(resolve, 50)),
);
const settings: TaskSettingsV2 = {
version: 2,
cadence: '*/15 * * * *',
initialDelayDuration: 'PT2M',
timeoutAfterDuration: 'PT1M',
};
const worker = new TaskWorker('task99', fn, knex, logger);
await worker.persistTask(settings);
const settings2 = {
...settings,
cadence: '*/2 * * * *',
initialDelayDuration: 'PT1M',
};
await worker.persistTask(settings2);
const row2 = (await knex<DbTasksRow>(DB_TASKS_TABLE))[0];
const settings3 = { ...settings };
await worker.persistTask(settings3);
const row3 = (await knex<DbTasksRow>(DB_TASKS_TABLE))[0];
expect(row3.next_run_start_at).toBe(row2.next_run_start_at);
},
);
});
+14 -1
View File
@@ -190,6 +190,11 @@ export class TaskWorker {
this.logger.debug(`task: ${this.taskId} configured to run at: ${startAt}`);
const start_at_previuos = this.knex(DB_TASKS_TABLE)
.where('id', '=', this.taskId)
.select('next_run_start_at')
.first();
// It's OK if the task already exists; if it does, just replace its
// settings with the new value and start the loop as usual.
await this.knex<DbTasksRow>(DB_TASKS_TABLE)
@@ -199,7 +204,15 @@ export class TaskWorker {
next_run_start_at: startAt,
})
.onConflict('id')
.merge(['settings_json', 'next_run_start_at']);
.merge({
settings_json: JSON.stringify(settings),
next_run_start_at: this.knex.raw('CASE WHEN ? < ? THEN ? ELSE ? END', [
start_at_previuos,
startAt,
start_at_previuos,
startAt,
]),
});
}
/**