Skip to content

Commit

Permalink
Scheduler: return task id form Scheduler.add
Browse files Browse the repository at this point in the history
Closes: #1635

PR-URL: #1638
  • Loading branch information
tshemsedinov committed Aug 29, 2021
1 parent 557be32 commit b80e95b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>`

## [2.5.3][] - 2021-08-19

Expand Down
5 changes: 4 additions & 1 deletion impress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 17 additions & 12 deletions lib/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -78,24 +78,29 @@ 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);
await fsp.writeFile(filePath, data);
} catch (err) {
this.application.console.error(err.stack);
}
return id;
}

async remove(id) {
Expand Down
2 changes: 1 addition & 1 deletion types/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface Task {
}

export interface Scheduler {
add(task: Task): void;
add(task: Task): Promise<string>;
remove(id: string): void;
stop(name: string): void;
}
Expand Down

0 comments on commit b80e95b

Please sign in to comment.