Skip to content

Commit

Permalink
fix: ExecTask never exiting when spawned process fails (#10)
Browse files Browse the repository at this point in the history
* fix: ExecTask never exiting when errors thrown in spawned process

* refactor: cleanup

* build: bump version
  • Loading branch information
Benjozork authored Oct 2, 2023
1 parent 29cdeab commit 1c2cfed
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flybywiresim/igniter",
"version": "1.2.2",
"version": "1.2.3",
"types": "dist/index.d.ts",
"description": "An intelligent task runner written in Typescript.",
"repository": {
Expand Down
22 changes: 20 additions & 2 deletions src/Library/Tasks/ExecTask.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { exec } from 'child_process';
import { promisify } from 'util';
import GenericTask from './GenericTask';
import { TaskStatus } from '../Contracts/Task';
import ExecTaskError from './ExecTaskError';

export default class ExecTask extends GenericTask {
constructor(
Expand All @@ -12,7 +12,25 @@ export default class ExecTask extends GenericTask {
const commands = typeof command === 'string' ? [command] : command;
super(key, async () => {
for await (const cmd of commands) {
const poolExec = this.context.taskPool.promise.wrap(promisify(exec));
const poolExec = this.context.taskPool.promise.wrap((execCmd) => new Promise((resolve, reject) => {
const p = exec(execCmd);

let stderr = '';
p.stderr.on('data', (data) => {
stderr += data;
});

p.on('exit', (code) => {
p.stdout.destroy();
p.stderr.destroy();

if (code === 0) {
resolve(code);
} else {
reject(new ExecTaskError(stderr));
}
});
}));

const task = poolExec(cmd);

Expand Down
5 changes: 5 additions & 0 deletions src/Library/Tasks/ExecTaskError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class ExecTaskError extends Error {
constructor(public readonly stderr: string) {
super('Error in ExecTask spawned process');
}
}
11 changes: 9 additions & 2 deletions src/Library/Tasks/GenericTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chalk from 'chalk';
import { TaskRunner, Task, TaskStatus } from '../Contracts/Task';
import { generateHashFromPaths, storage } from '../../Helpers';
import { Context } from '../Contracts/Context';
import ExecTaskError from './ExecTaskError';

export default class GenericTask implements Task {
protected context: Context;
Expand Down Expand Up @@ -50,9 +51,15 @@ export default class GenericTask implements Task {
this.context.cache.set(taskKey, generateHash);
}
} catch (error) {
if (this.context.debug) throw error;
if (this.context.debug) {
throw error;
}

this.status = TaskStatus.Failed;
if ('stderr' in error) this.errorOutput = error.stderr;

if (error instanceof ExecTaskError) {
this.errorOutput = error.stderr;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/task-pool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ declare module "task-pool" {

queue: PoolTask<any>[]

wrap(Function, options?: Partial<PoolTaskOptions>): PoolTaskFactory<TPoolStyle>
wrap(Function: (...args: any[]) => Promise<any>, options?: Partial<PoolTaskOptions>): PoolTaskFactory<TPoolStyle>

next()

Expand Down

0 comments on commit 1c2cfed

Please sign in to comment.