Fix task queue loading

This commit is contained in:
Anton Vakhrushev 2020-03-31 12:13:08 +03:00
parent 017433fdf9
commit 847ad57f66
2 changed files with 11 additions and 2 deletions

View File

@ -146,7 +146,7 @@ export default class Scheduler {
} catch (e) { } catch (e) {
console.warn('ACTION ABORTED', e.message); console.warn('ACTION ABORTED', e.message);
if (e instanceof TryLaterError) { if (e instanceof TryLaterError) {
console.warn('TRY AFTER', e.seconds); console.warn('TRY', task.id, 'AFTER', e.seconds);
this.actionQueue.clear(); this.actionQueue.clear();
this.taskQueue.postpone(task.id, e.seconds); this.taskQueue.postpone(task.id, e.seconds);
this.nextSleepLong(); this.nextSleepLong();

View File

@ -57,6 +57,7 @@ export class TaskQueue {
postpone(id: TaskId, deltaSeconds: number) { postpone(id: TaskId, deltaSeconds: number) {
const [task, items] = this.shiftTask(id); const [task, items] = this.shiftTask(id);
if (task) { if (task) {
this.log('POSTPONE', task);
items.push(task.withTime(task.ts + deltaSeconds)); items.push(task.withTime(task.ts + deltaSeconds));
} }
this.flushItems(items); this.flushItems(items);
@ -75,7 +76,15 @@ export class TaskQueue {
private getItems(): TaskList { private getItems(): TaskList {
const serialized = localStorage.getItem(QUEUE_NAME); const serialized = localStorage.getItem(QUEUE_NAME);
return serialized !== null ? (JSON.parse(serialized) as TaskList) : []; const storedItems =
serialized !== null
? (JSON.parse(serialized) as Array<{ [key: string]: any }>)
: [];
const items: TaskList = [];
storedItems.forEach(obj => {
items.push(new Task(obj.id || uniqId(), +obj.ts, obj.cmd));
});
return items;
} }
private flushItems(items: TaskList): void { private flushItems(items: TaskList): void {