Fixing the lost of the initial state after a task recovery.

Signed-off-by: bnechyporenko <bnechyporenko@bol.com>
This commit is contained in:
bnechyporenko
2024-03-13 09:39:45 +01:00
parent e5f26f9f2d
commit 1f09ac792e
2 changed files with 42 additions and 2 deletions
@@ -161,6 +161,44 @@ describe('DatabaseTaskStore', () => {
expect(claimedTask.status).toBe('processing');
});
it('should restore the state of the task after the task recovery', async () => {
const { store } = await createStore();
const { taskId } = await store.createTask({
spec: {} as TaskSpec,
createdBy: 'me',
});
const task = await store.getTask(taskId);
expect(task.status).toBe('open');
await store.claimTask();
const state = {
state: {
checkpoints: {
'v1.task.checkpoint.deploy.to.stg': {
status: 'success',
value: true,
},
'v1.task.checkpoint.deploy.to.pro': {
status: 'success',
value: true,
},
},
},
};
await store.saveTaskState({
taskId,
state,
});
await store.recoverTasks({ timeout: { milliseconds: 0 } });
await store.claimTask();
const claimedTask = await store.getTask(taskId);
expect(claimedTask.state).toEqual({ state });
});
it('should shutdown the running task', async () => {
const { store } = await createStore();
const { taskId } = await store.createTask({
@@ -212,6 +212,7 @@ export class DatabaseTaskStore implements TaskStore {
try {
const spec = JSON.parse(result.spec);
const secrets = result.secrets ? JSON.parse(result.secrets) : undefined;
const state = result.state ? JSON.parse(result.state) : undefined;
return {
id: result.id,
spec,
@@ -220,6 +221,7 @@ export class DatabaseTaskStore implements TaskStore {
createdAt: parseSqlDateToIsoString(result.created_at),
createdBy: result.created_by ?? undefined,
secrets,
state,
};
} catch (error) {
throw new Error(`Failed to parse spec of task '${taskId}', ${error}`);
@@ -536,14 +538,14 @@ export class DatabaseTaskStore implements TaskStore {
status: 'open',
last_heartbeat_at: this.db.fn.now(),
},
['id', 'spec'],
['id', 'spec', 'state'],
);
taskIdsToRecover.push(...result.map(i => i.id));
for (const { id, spec } of result) {
const taskSpec = JSON.parse(spec as string) as TaskSpec;
await this.db<RawDbTaskEventRow>('task_events').insert({
await tx<RawDbTaskEventRow>('task_events').insert({
task_id: id,
event_type: 'recovered',
body: JSON.stringify({