Skip to content

Commit

Permalink
[APD-chore] graceful shutdown for account setup
Browse files Browse the repository at this point in the history
  • Loading branch information
NaLLiFFuNT committed Aug 7, 2019
1 parent a6dde89 commit a92b582
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/core/accounts-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export class AccountsConnector extends Connector {
}

async destroy (): Promise<void> {
await super.destroy();
for (let setup of this.setups.values()) {
await setup.stop();
}
await super.destroy();
}
}
3 changes: 3 additions & 0 deletions src/core/admob-api/account-setup.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ export class AccountSetup extends EventEmitter {
}
})
.catch(err => {
console.error('AccountSetup: runTasks error', err);
if (this.runner.state !== TaskRunnerState.cancelled) {
this.emit('error', err);
Sentry.captureException(err);
} else {
console.error('AccountSetup: is Canceled');
}
})
.finally(() => {
Expand Down
29 changes: 28 additions & 1 deletion src/lib/task-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,45 @@ export class TaskRunner extends EventEmitter {
});
}

cancelableJob<T> (jobFn: () => Promise<T>): Promise<T> {
return new Promise(((resolve, reject) => {
let jobFullFiled = false;
let timerID;

const done = (cb: Function) => v => {
clearTimeout(timerID);
jobFullFiled = true;
cb(v);
};

jobFn().then(done(resolve), done(reject));
const check = () => {
if (jobFullFiled) {
return;
}
if (this.state === TaskRunnerState.cancelled) {
console.log(`Execution terminated. Stop waiting task execution`);
return reject(new Error('Canceled'));
}
timerID = setTimeout(check, 100);
};
check();
}));
}

private exec () {
return new Promise(async (resolve, reject) => {

const runNext = () => {
if (this.state === TaskRunnerState.cancelled) {
console.log(`Execution terminated`);
return reject(new Error('Canceled'));
}
const activeTask = this.tasks[this.activeTaskNum];
let taskArgs = this.taskArgs || [];
this.taskArgs = null;
console.log(`Execute task: ${activeTask['func'].toString()}`);
return activeTask.execute(...taskArgs)
return this.cancelableJob(() => activeTask.execute(...taskArgs))
.then(() => {
if (!this.hasSkip() && !this.hasReturn()) {
this.activeTaskNum++;
Expand Down
33 changes: 17 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,23 @@ function runApp () {
.then(() => store.updateUserWhenOnline());
});


const cleanUpOnExit = () => Promise.all([
closeAllWindows(),
trayIcon.destroy(),
tray.destroy(),
onlineConnector.destroy(),
accountsConnector.destroy(),
aboutConnector.destroy(),
syncConnector.destroy(),
logsConnector.destroy(),
syncService.destroy(),
updatesConnector.destroy(),
onlineService.destroy(),
syncScheduler.destroy(),
deleteDataConnector.destroy()
]);
const cleanUpOnExit = async () => {
await accountsConnector.destroy().catch(console.error);
return Promise.all([
syncService.destroy(),
closeAllWindows(),
trayIcon.destroy(),
tray.destroy(),
onlineConnector.destroy(),
aboutConnector.destroy(),
syncConnector.destroy(),
logsConnector.destroy(),
updatesConnector.destroy(),
onlineService.destroy(),
syncScheduler.destroy(),
deleteDataConnector.destroy()
]);
};

onlineService.on('statusChange', isOnline => {
console.warn('online', isOnline);
Expand Down

0 comments on commit a92b582

Please sign in to comment.