Files
nomad/ui/app/routes/allocations/allocation.js
Jai 67fd9ffee1 refact: upgrade Promise.then to async/await (#14798)
* refact: upgrade Promise.then to async/await

* naive solution (#14800)

* refact: use id instead of model

* chore:  add changelog entry

* refact: add conditional safety around alloc
2022-10-20 14:25:41 -04:00

58 lines
1.8 KiB
JavaScript

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { collect } from '@ember/object/computed';
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) {
@service store;
startWatchers(controller, model) {
if (model) {
controller.set('watcher', this.watch.perform(model));
const anyGroupServicesAreNomad = !!model.taskGroup?.services?.filterBy(
'provider',
'nomad'
).length;
const anyTaskServicesAreNomad = model.states
.mapBy('task.services')
.compact()
.map((fragmentClass) => fragmentClass.mapBy('provider'))
.flat()
.any((provider) => provider === 'nomad');
// Conditionally Long Poll /checks endpoint if alloc has nomad services
if (anyGroupServicesAreNomad || anyTaskServicesAreNomad) {
controller.set(
'watchHealthChecks',
this.watchHealthChecks.perform(model, 'getServiceHealth', 2000)
);
}
}
}
async model() {
try {
// Preload the job for the allocation since it's required for the breadcrumb trail
const allocation = await super.model(...arguments);
const jobId = allocation?.belongsTo('job').id();
const getJob = this.store.findRecord('job', jobId);
const getNamespaces = this.store.findAll('namespace');
await Promise.all([getJob, getNamespaces]);
return allocation;
} catch {
notifyError(this);
}
}
@watchRecord('allocation') watch;
@watchNonStoreRecords('allocation') watchHealthChecks;
@collect('watch', 'watchHealthChecks') watchers;
}