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

Slightly rework mu-task handling #307

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .changeset/sharp-starfishes-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"frontend-reglementaire-bijlage": patch
---

- Fetch tasks using `ember-data` and `mu-cl-resources` endpoint
- Use generic task statuses
6 changes: 6 additions & 0 deletions app/models/task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Model, { attr } from '@ember-data/model';

export default class TaskModel extends Model {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, should this also have the type?

@attr uri;
@attr status;
}
50 changes: 17 additions & 33 deletions app/services/mu-task.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import Service from '@ember/service';
import { task, timeout } from 'ember-concurrency';

const TASK_ENDPOINT = '/tasks';
export const TASK_STATUS_FAILURE =
'http://lblod.data.gift/besluit-publicatie-melding-statuses/failure';
export const TASK_STATUS_CREATED =
'http://lblod.data.gift/besluit-publicatie-melding-statuses/created';
export const TASK_STATUS_SUCCESS =
'http://lblod.data.gift/besluit-publicatie-melding-statuses/success';
export const TASK_STATUS_RUNNING =
'http://lblod.data.gift/besluit-publicatie-melding-statuses/ongoing';
import { JOB_STATUSES } from '../utils/constants';
import { service } from '@ember/service';

export default class MuTaskService extends Service {
@service store;

fetchMuTask(taskId) {
// TODO do this with ember-data and mu-cl-resource
return fetch(`${TASK_ENDPOINT}/${taskId}`);
try {
return this.store.findRecord('task', taskId);
} catch (e) {
throw new Error(`An error occured while fetching task ${taskId}`);
}
}

/**
Expand All @@ -24,35 +21,22 @@ export default class MuTaskService extends Service {
* */
waitForMuTaskTask = task(
async (taskId, pollDelayMs = 1000, timeoutMs = 300000) => {
let resp;
let jsonBody;
let currentStatus;
const startTime = Date.now();
let task;
do {
await timeout(pollDelayMs);
resp = await this.fetchMuTask(taskId);
if (resp.ok) {
jsonBody = await resp.json();
currentStatus = jsonBody.data.status;
} else {
currentStatus = null;
}
task = await this.fetchMuTask(taskId);
} while (
resp.ok &&
(currentStatus === TASK_STATUS_RUNNING ||
currentStatus === TASK_STATUS_CREATED) &&
(task.status === JOB_STATUSES.busy ||
task.status === JOB_STATUSES.scheduled) &&
Date.now() - startTime < timeoutMs
);

if (!resp.ok) {
const reason = await resp.text();
throw new Error(reason);
}
if (currentStatus === TASK_STATUS_SUCCESS) {
return jsonBody;
} else if (currentStatus === TASK_STATUS_FAILURE) {
if (task.status === JOB_STATUSES.success) {
return task;
} else if (task.status === JOB_STATUSES.failed) {
throw new Error('Task failed.');
} else if (currentStatus === TASK_STATUS_RUNNING) {
} else if (task.status === JOB_STATUSES.busy) {
throw new Error('Task timed out.');
} else {
throw new Error('Task in unexpected state');
Expand Down
8 changes: 8 additions & 0 deletions app/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ export const RMW_TAGS = /** @type {const} */ ([
'IVRMW2-LBM-4-geloofsbrieven-leden',
'IVRMW2-LBM-5-eed-leden',
]);

export const JOB_STATUSES = {
scheduled: 'http://redpencil.data.gift/id/concept/JobStatus/scheduled',
busy: 'http://redpencil.data.gift/id/concept/JobStatus/busy',
success: 'http://redpencil.data.gift/id/concept/JobStatus/success',
failed: 'http://redpencil.data.gift/id/concept/JobStatus/failed',
canceled: 'http://redpencil.data.gift/id/concept/JobStatus/canceled',
};