Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): mark scenario run as failed while canceled #521

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions api/apps/api/src/modules/scenarios/marxan-run/cancel.handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Inject, Injectable } from '@nestjs/common';
import { Job, Queue } from 'bullmq';
import { Either, left, right } from 'fp-ts/Either';
import { Either, right } from 'fp-ts/Either';
import { ApiEventsService } from '@marxan-api/modules/api-events/api-events.service';
import { JobData } from '@marxan/scenario-run-queue';
import { isDefined } from '@marxan/utils';
import { runQueueToken } from './tokens';
import { API_EVENT_KINDS } from '@marxan/api-events';

export const notFound = Symbol('not found');
export type NotFound = typeof notFound;
Expand All @@ -13,6 +15,7 @@ export class CancelHandler {
constructor(
@Inject(runQueueToken)
private readonly queue: Queue<JobData>,
private readonly apiEvents: ApiEventsService,
) {}

async cancel(scenarioId: string): Promise<Either<NotFound, void>> {
Expand All @@ -23,7 +26,13 @@ export class CancelHandler {
const scenarioJob = activeJobs.find(
(job) => job.data.scenarioId === scenarioId,
);
if (!isDefined(scenarioJob)) return left(notFound);
if (!isDefined(scenarioJob)) {
await this.apiEvents.create({
topic: scenarioId,
kind: API_EVENT_KINDS.scenario__run__failed__v1__alpha1,
});
return right(void 0);
}

if (await scenarioJob.isActive())
await scenarioJob.updateProgress({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { RunService } from './run.service';
import { AssetsService } from './assets.service';
import { blmDefaultToken, runEventsToken, runQueueToken } from './tokens';
import { RunHandler } from './run.handler';
import { CancelHandler, notFound } from './cancel.handler';
import { CancelHandler } from './cancel.handler';
import { EventsHandler } from './events.handler';

let fixtures: PromiseType<ReturnType<typeof getFixtures>>;
Expand Down Expand Up @@ -99,11 +99,15 @@ test(`canceling job`, async () => {
});

test(`canceling job`, async () => {
fixtures.setupForCancelingNotFoundJob();
fixtures.GivenNoJobsInQueue();

const result = await runService.cancel(`scenario-1`);

expect(result).toStrictEqual(left(notFound));
expect(result).toStrictEqual(right(void 0));
await fixtures.ThenEventCreated(
API_EVENT_KINDS.scenario__run__failed__v1__alpha1,
);
});

test(`canceling active job`, async () => {
Expand Down Expand Up @@ -376,6 +380,9 @@ async function getFixtures() {
return right({});
});
},
setupForCancelingNotFoundJob() {
fakeApiEvents.create.mockImplementation(() => ({}));
},
setupMocksForCompletedJob() {
fakeApiEvents.createIfNotExists.mockImplementation(() => {
return right({});
Expand Down