Skip to content

Commit

Permalink
internal(tasks): slightly rework our handling of tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
elpoelma committed Dec 10, 2024
1 parent 3447971 commit a79424b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
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 {
@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',
};

0 comments on commit a79424b

Please sign in to comment.