Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2023-02-16 14:57:19 +01:00
parent 3b630ee2cd
commit b69c41bfaf
2 changed files with 19 additions and 13 deletions
@@ -37,17 +37,22 @@ const getTask = async (knex: Knex): Promise<DbTasksRow> => {
describe('PluginTaskSchedulerJanitor', () => {
const logger = getVoidLogger();
const databases = TestDatabases.create({
ids: ['MYSQL_8', 'POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'],
ids: [
/* 'MYSQL_8' not supported yet */
'POSTGRES_13',
'POSTGRES_9',
'SQLITE_3',
],
});
jest.setTimeout(300_000);
jest.setTimeout(60_000);
beforeEach(() => {
jest.resetAllMocks();
});
it.each(databases.eachSupportedId())(
'Should update date if current_run_expires_at expires , %p',
'Should update date if current_run_expires_at expires, %p',
async databaseId => {
const knex = await databases.init(databaseId);
await migrateBackendTasks(knex);
@@ -66,19 +71,16 @@ describe('PluginTaskSchedulerJanitor', () => {
});
const worker = new PluginTaskSchedulerJanitor({
waitBetweenRuns: Duration.fromObject({ milliseconds: 20 }),
knex,
waitBetweenRuns: Duration.fromObject({ minutes: 1 }),
logger,
});
const abortController = new AbortController();
await worker.start(abortController.signal);
const row1 = await getTask(knex);
worker.start(abortController.signal);
await waitForExpect(async () => {
expect(row1).toEqual(
await expect(getTask(knex)).resolves.toEqual(
expect.objectContaining({
id: 'task1',
current_run_ticket: null,
@@ -87,6 +89,8 @@ describe('PluginTaskSchedulerJanitor', () => {
}),
);
});
abortController.abort();
},
);
});
@@ -53,16 +53,18 @@ export class PluginTaskSchedulerJanitor {
private async runOnce() {
const dbNull = this.knex.raw('null');
let tasks: Array<{ id: string }>;
const configClient = this.knex.client.config.client;
let tasks: Array<{ id: string }>;
if (configClient.includes('sqlite3') || configClient.includes('mysql')) {
const now = await this.knex.select(this.knex.fn.now());
tasks = await this.knex<DbTasksRow>(DB_TASKS_TABLE)
.select('id')
.where('current_run_expires_at', '<', now);
.where('current_run_expires_at', '<', this.knex.fn.now());
await this.knex<DbTasksRow>(DB_TASKS_TABLE)
.where('current_run_expires_at', '<', now)
.whereIn(
'id',
tasks.map(t => t.id),
)
.update({
current_run_ticket: dbNull,
current_run_started_at: dbNull,