Skip to content

Commit

Permalink
fix(marxan-run): in case of any error, handle it and use failed-reason
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Sep 6, 2021
1 parent b1279b8 commit e6562fd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export class EventsHandler {
await this.apiEvents.createIfNotExists({
topic: job.data.scenarioId,
kind,
data: {
reason: job.failedReason,
},
externalId: eventId,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ test(`failed job`, async () => {
await fixtures.ThenEventCreatedIfNotExisted(
API_EVENT_KINDS.scenario__run__failed__v1__alpha1,
`eventid1`,
{
reason: 'fail description',
},
);
});

Expand Down Expand Up @@ -261,8 +264,10 @@ async function getFixtures() {
const fakeAssets = {
forScenario: jest.fn(),
};

class FakeOutputRepository implements FieldsOf<OutputRepository> {
db: ScenariosOutputResultsApiEntity[] = [];

async saveOutput(job: {
returnvalue: ExecutionResult | undefined;
data: { scenarioId: string };
Expand All @@ -275,6 +280,7 @@ async function getFixtures() {
});
}
}

const testingModule = await Test.createTestingModule({
providers: [
RunHandler,
Expand Down Expand Up @@ -500,6 +506,7 @@ async function getFixtures() {
data: {
scenarioId: `scenario-1`,
},
failedReason: `fail description`,
};
});
},
Expand Down
24 changes: 21 additions & 3 deletions api/apps/geoprocessing/src/marxan-sandboxed-runner/marxan-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,38 @@ export class MarxanRun extends MarxanRunEmitter implements Cancellable {
this.#process = spawn(workspace.marxanBinaryPath, {
cwd: workspace.workingDirectory,
});

assertDefined(this.#process);

this.#process.stderr.on('data', (chunk) => {
this.#stdError.push(chunk.toString());
});

const progressWatcher = new Progress();
this.#process.stdout.on('data', (chunk) => {
const currentProgress = progressWatcher.read(chunk);
this.emit(`progress`, currentProgress);
this.#stdOut.push(chunk.toString());
});

this.#process.on(
`error`,
(
error:
| Error
| {
errno: number;
code: number;
path: string;
syscall: string;
spawnargs: string[];
},
) => {
this.emit('error', {
signal: undefined,
code: 'code' in error ? error.code : undefined,
stdError: ['message' in error ? error.message : ''],
});
return;
},
);
this.#process.on('exit', (code, signal) => {
if (signal) {
this.emit('error', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class MarxanSandboxRunnerService {
marxanRun.stdError,
);
await workspace.cleanup();
reject(result);
reject(JSON.stringify(result));
});
marxanRun.on('finished', async () => {
try {
Expand Down
9 changes: 8 additions & 1 deletion api/apps/geoprocessing/src/modules/scenarios/run.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ export const runWorkerQueueNameProvider: ValueProvider<string> = {
export class RunWorker {
private worker: Worker<JobData, ExecutionResult>;
private queueEvents: QueueEvents;

constructor(
queueEventsBuilder: QueueEventsBuilder,
workerBuilder: WorkerBuilder,
@Inject(runWorkerQueueNameToken) queueName: string,
private readonly marxanRunner: MarxanSandboxRunnerService,
) {
this.worker = workerBuilder.build<JobData, ExecutionResult>(queueName, {
process: (job) => this.run(job),
process: async (job) => {
try {
return await this.run(job);
} catch (error) {
throw new Error(JSON.stringify(error));
}
},
});
this.queueEvents = queueEventsBuilder.buildQueueEvents(queueName);
this.queueEvents.on(`progress`, ({ data }: { data: ProgressData }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe(`given input data is available`, () => {
done(`Shouldn't finish Marxan run.`);
})
.catch((error) => {
expect(error.signal).toEqual('SIGTERM');
expect(JSON.parse(error).signal).toEqual('SIGTERM');
done();
});

Expand Down

0 comments on commit e6562fd

Please sign in to comment.