Skip to content

Commit

Permalink
Boot the user off the job if it gets deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Jul 11, 2023
1 parent 80b9ff6 commit 23907ad
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
25 changes: 25 additions & 0 deletions ui/app/controllers/jobs/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
* SPDX-License-Identifier: MPL-2.0
*/

// @ts-check
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';

export default class JobController extends Controller {
@service router;
@service notifications;
queryParams = [
{
jobNamespace: 'namespace',
Expand All @@ -16,4 +21,24 @@ export default class JobController extends Controller {
get job() {
return this.model;
}

get jobHasBeenYoinked() {
return (
this.watchers.job.isError &&
this.watchers.job.error.errors.some((e) => e.status === '404')
);
}

@action yoinkedJobHandler() {
if (this.jobHasBeenYoinked) {
this.notifications.add({
title: `Job ${this.job.name} has been deleted`,
message:
'The job you were looking at has been deleted; this is usually because it was purged from elsewhere.',
color: 'critical',
destroyOnClick: false,
});
this.router.transitionTo('jobs');
}
}
}
16 changes: 15 additions & 1 deletion ui/app/routes/jobs/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import Route from '@ember/routing/route';
import RSVP from 'rsvp';
import notifyError from 'nomad-ui/utils/notify-error';
import classic from 'ember-classic-decorator';
import { watchRecord } from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';

@classic
export default class JobRoute extends Route {
export default class JobRoute extends Route.extend(WithWatchers) {
@service can;
@service store;
@service token;
@service router;
@service notifications;

serialize(model) {
return { job_name: model.get('idWithNamespace') };
Expand Down Expand Up @@ -57,4 +60,15 @@ export default class JobRoute extends Route {
})
.catch(notifyError(this));
}

startWatchers(controller, model) {
if (!model) {
return;
}
controller.set('watchers', {
job: this.watch.perform(model),
});
}

@watchRecord('job', { shouldSurfaceErrors: true }) watch;
}
6 changes: 3 additions & 3 deletions ui/app/routes/jobs/job/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class IndexRoute extends Route.extend(WithWatchers) {
return;
}
controller.set('watchers', {
model: this.watch.perform(model),
// model: this.watch.perform(model),
summary: this.watchSummary.perform(model.get('summary')),
allocations: this.watchAllocations.perform(model),
evaluations: this.watchEvaluations.perform(model),
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class IndexRoute extends Route.extend(WithWatchers) {
return super.setupController(...arguments);
}

@watchRecord('job') watch;
// @watchRecord('job') watch;
@watchQuery('job') watchAllJobs;
@watchAll('node') watchNodes;
@watchRecord('job-summary') watchSummary;
Expand All @@ -68,7 +68,7 @@ export default class IndexRoute extends Route.extend(WithWatchers) {
@watchRelationship('latestDeployment') watchLatestDeployment;

@collect(
'watch',
// 'watch',
'watchAllJobs',
'watchSummary',
'watchAllocations',
Expand Down
1 change: 1 addition & 0 deletions ui/app/templates/jobs/job.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
SPDX-License-Identifier: MPL-2.0
~}}

{{did-update this.yoinkedJobHandler this.jobHasBeenYoinked}}
<Breadcrumb @crumb={{hash type="job" job=this.job}} />{{outlet}}
16 changes: 15 additions & 1 deletion ui/app/utils/properties/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: MPL-2.0
*/

// @ts-check

import Ember from 'ember';
import { get } from '@ember/object';
import { assert } from '@ember/debug';
Expand All @@ -15,7 +17,16 @@ import config from 'nomad-ui/config/environment';

const isEnabled = config.APP.blockingQueries !== false;

export function watchRecord(modelName) {
/**
* @typedef watchRecordOptions
* @property {boolean} [shouldSurfaceErrors=false] - If true, the task will throw errors instead of yielding them.
*/

/**
* @param {string} modelName - The name of the model to watch.
* @param {watchRecordOptions} [options]
*/
export function watchRecord(modelName, { shouldSurfaceErrors = false } = {}) {
return task(function* (id, throttle = 2000) {
assert(
'To watch a record, the record adapter MUST extend Watchable',
Expand All @@ -35,6 +46,9 @@ export function watchRecord(modelName) {
wait(throttle),
]);
} catch (e) {
if (shouldSurfaceErrors) {
throw e;
}
yield e;
break;
} finally {
Expand Down

0 comments on commit 23907ad

Please sign in to comment.