Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2023-09-14 09:23:19 +02:00
parent 8fd91547cd
commit bf431f81c4
4 changed files with 36 additions and 24 deletions
+4 -1
View File
@@ -2,4 +2,7 @@
'@backstage/backend-tasks': patch
---
Fixed bug in backend TaskWorker, 'next_run_start_at' will be always the least between schedule changes.
When starting a task that existed before, with a faster schedule than it
previously had, the task will now correctly obey the faster schedule
immediately. Before this fix, the new schedule was only obeyed after the next
pending (according to the old schedule) run had completed.
-5
View File
@@ -1,5 +0,0 @@
---
'@backstage/backend-tasks': patch
---
Fixed bug in backend TaskWorker, adding `next_run_start_at` as a merge conflict when scheduler changes
@@ -330,6 +330,8 @@ describe('TaskWorker', () => {
const before = fn1.mock.calls.length;
await promise2;
expect(fn1.mock.calls.length).toBeGreaterThan(before);
await knex.destroy();
},
);
@@ -351,6 +353,7 @@ describe('TaskWorker', () => {
const worker = new TaskWorker('task99', fn, knex, logger);
await worker.persistTask(settings);
const row1 = (await knex<DbTasksRow>(DB_TASKS_TABLE))[0];
const settings2 = {
...settings,
@@ -358,15 +361,17 @@ describe('TaskWorker', () => {
initialDelayDuration: 'PT1M',
};
await worker.persistTask(settings2);
const row2 = (await knex<DbTasksRow>(DB_TASKS_TABLE))[0];
expect(row2.next_run_start_at).not.toStrictEqual(row1.next_run_start_at);
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);
expect(row3.next_run_start_at).toStrictEqual(row2.next_run_start_at);
await knex.destroy();
},
);
});
+24 -15
View File
@@ -190,29 +190,38 @@ 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.
const settingsJson = JSON.stringify(settings);
await this.knex<DbTasksRow>(DB_TASKS_TABLE)
.insert({
id: this.taskId,
settings_json: JSON.stringify(settings),
settings_json: settingsJson,
next_run_start_at: startAt,
})
.onConflict('id')
.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,
]),
});
.merge(
this.knex.client.config.client.includes('mysql')
? {
settings_json: settingsJson,
next_run_start_at: this.knex.raw(
`CASE WHEN ?? < ?? THEN ?? ELSE ?? END`,
[startAt, 'next_run_start_at', startAt, 'next_run_start_at'],
),
}
: {
settings_json: this.knex.ref('excluded.settings_json'),
next_run_start_at: this.knex.raw(
`CASE WHEN ?? < ?? THEN ?? ELSE ?? END`,
[
'excluded.next_run_start_at',
`${DB_TASKS_TABLE}.next_run_start_at`,
'excluded.next_run_start_at',
`${DB_TASKS_TABLE}.next_run_start_at`,
],
),
},
);
}
/**