Skip to content

Commit

Permalink
ui: long poll /checks endpoint (#14354)
Browse files Browse the repository at this point in the history
* chore: add lodash isEqual package

* ui: fetch non ember-data records

* ui: create watcher to poll non ember-data records
  • Loading branch information
ChaiWithJai committed Aug 26, 2022
1 parent 7a7278f commit 8a86862
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
8 changes: 8 additions & 0 deletions ui/app/adapters/allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export default class AllocationAdapter extends Watchable {
)
.then(handleFSResponse);
}

async check(model) {
const res = await this.token.authorizedRequest(
`/v1/client/allocation/${model.id}/checks`
);
const data = await res.json();
return data;
}
}

async function handleFSResponse(response) {
Expand Down
13 changes: 13 additions & 0 deletions ui/app/models/allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { equal, none } from '@ember/object/computed';
import Model from '@ember-data/model';
import { attr, belongsTo, hasMany } from '@ember-data/model';
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
import isEqual from 'lodash.isequal';
import intersection from 'lodash.intersection';
import shortUUIDProperty from '../utils/properties/short-uuid';
import classic from 'ember-classic-decorator';
Expand All @@ -20,6 +21,7 @@ const STATUS_ORDER = {
@classic
export default class Allocation extends Model {
@service token;
@service store;

@shortUUIDProperty('id') shortId;
@belongsTo('job') job;
Expand All @@ -40,6 +42,17 @@ export default class Allocation extends Model {
@attr('string') clientStatus;
@attr('string') desiredStatus;

@attr healthChecks;

async getServiceHealth() {
const data = await this.store.adapterFor('allocation').check(this);

// Compare Results
if (!isEqual(this.healthChecks, data)) {
this.set('healthChecks', data);
}
}

@computed('')
get plainJobId() {
return JSON.parse(this.belongsTo('job').id())[0];
Expand Down
10 changes: 9 additions & 1 deletion ui/app/routes/allocations/allocation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { collect } from '@ember/object/computed';
import { watchRecord } from 'nomad-ui/utils/properties/watch';
import {
watchRecord,
watchNonStoreRecords,
} from 'nomad-ui/utils/properties/watch';
import WithWatchers from 'nomad-ui/mixins/with-watchers';
import notifyError from 'nomad-ui/utils/notify-error';
export default class AllocationRoute extends Route.extend(WithWatchers) {
Expand All @@ -10,6 +13,10 @@ export default class AllocationRoute extends Route.extend(WithWatchers) {
startWatchers(controller, model) {
if (model) {
controller.set('watcher', this.watch.perform(model));
controller.set(
'watchHealthChecks',
this.watchHealthChecks.perform(model, 'getServiceHealth')
);
}
}

Expand All @@ -28,6 +35,7 @@ export default class AllocationRoute extends Route.extend(WithWatchers) {
}

@watchRecord('allocation') watch;
@watchNonStoreRecords('allocation') watchHealthChecks;

@collect('watch') watchers;
}
21 changes: 21 additions & 0 deletions ui/app/utils/properties/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ export function watchRelationship(relationshipName, replace = false) {
}).drop();
}

export function watchNonStoreRecords(modelName) {
return task(function* (model, asyncCallbackName, throttle = 5000) {
assert(
'To watch a non-store records, the adapter of the model provided to the watchNonStoreRecords task MUST extend Watchable',
this.store.adapterFor(modelName) instanceof Watchable
);
while (isEnabled && !Ember.testing) {
const controller = new AbortController();
try {
yield model[asyncCallbackName]();
yield wait(throttle);
} catch (e) {
yield e;
break;
} finally {
controller.abort();
}
}
}).drop();
}

export function watchAll(modelName) {
return task(function* (throttle = 2000) {
assert(
Expand Down
1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"jsonlint": "^1.6.3",
"lint-staged": "^11.2.6",
"loader.js": "^4.7.0",
"lodash.isequal": "^4.5.0",
"lodash.intersection": "^4.4.0",
"morgan": "^1.3.2",
"npm-run-all": "^4.1.5",
Expand Down
2 changes: 1 addition & 1 deletion ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14205,7 +14205,7 @@ lodash.isempty@^4.4.0:
lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=
integrity sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==

lodash.isfunction@^3.0.9:
version "3.0.9"
Expand Down

0 comments on commit 8a86862

Please sign in to comment.