From b80e95b86fa4284b5f9512578bbe61fde2789587 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Mon, 30 Aug 2021 00:37:54 +0300 Subject: [PATCH] Scheduler: return task id form Scheduler.add Closes: https://github.com/metarhia/impress/issues/1635 PR-URL: https://github.com/metarhia/impress/pull/1638 --- CHANGELOG.md | 1 + impress.js | 5 ++++- lib/scheduler.js | 29 +++++++++++++++++------------ types/core.d.ts | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8af161d..dcf21d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased][unreleased] - Allow third party plugins (not only metarhia npm modules) +- Scheduler: return task id from `Scheduler.add(task: Task): Promise` ## [2.5.3][] - 2021-08-19 diff --git a/impress.js b/impress.js index c23226ee..b01bc669 100644 --- a/impress.js +++ b/impress.js @@ -88,7 +88,10 @@ const CTRL_C = 3; worker.on('message', (data) => { if (data.type === 'event') { if (data.name === 'started') active++; - if (data.name.startsWith('task:')) scheduler.postMessage(data); + if (data.name.startsWith('task:')) { + const transferList = data.port ? [data.port] : undefined; + scheduler.postMessage(data, transferList); + } } if (active === count && startTimer) { clearTimeout(startTimer); diff --git a/lib/scheduler.js b/lib/scheduler.js index 7fb76199..66854e39 100644 --- a/lib/scheduler.js +++ b/lib/scheduler.js @@ -27,11 +27,11 @@ class Scheduler { async load() { this.executor = true; - worker.parentPort.on('message', async (data) => { - if (data.type !== 'event') return; - if (data.name === 'task:add') this.add(data.task); - else if (data.name === 'task:remove') this.remove(data.task.id); - else if (data.name === 'task:stop') this.stop(data.task.name); + worker.parentPort.on('message', async ({ type, name, task, port }) => { + if (type !== 'event') return; + if (name === 'task:add') port.postMessage({ id: await this.add(task) }); + else if (name === 'task:remove') this.remove(task.id); + else if (name === 'task:stop') this.stop(task.name); }); const now = metautil.nowDate(); @@ -78,17 +78,21 @@ class Scheduler { return this.start(id); } - async add(record) { + async add(task) { if (!this.executor) { - const msg = { type: 'event', name: 'task:add', task: record }; - worker.parentPort.postMessage(msg); - return; + const { port1, port2 } = new worker.MessageChannel(); + const msg = { type: 'event', name: 'task:add', task, port: port1 }; + return new Promise((resolve) => { + port2.on('message', ({ id }) => { + resolve(id); + }); + worker.parentPort.postMessage(msg, [port1]); + }); } const id = metautil.nowDate() + '-id-' + this.nextId.toString(); this.nextId++; - const task = { id, ...record }; - const started = this.restore(task); - if (!started) return; + const started = this.restore({ id, ...task }); + if (!started) return id; const filePath = path.join(this.path, id + '.json'); try { const data = JSON.stringify(task); @@ -96,6 +100,7 @@ class Scheduler { } catch (err) { this.application.console.error(err.stack); } + return id; } async remove(id) { diff --git a/types/core.d.ts b/types/core.d.ts index 4acf54a5..b5ffd7ac 100644 --- a/types/core.d.ts +++ b/types/core.d.ts @@ -6,7 +6,7 @@ export interface Task { } export interface Scheduler { - add(task: Task): void; + add(task: Task): Promise; remove(id: string): void; stop(name: string): void; }