open-nomad/ui/app/components/allocation-row.js

103 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-03-22 21:34:57 +00:00
import Ember from 'ember';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { run } from '@ember/runloop';
import { lazyClick } from '../helpers/lazy-click';
2018-03-22 21:34:57 +00:00
import { task, timeout } from 'ember-concurrency';
2017-09-19 14:47:10 +00:00
export default Component.extend({
store: service(),
2017-09-19 14:47:10 +00:00
tagName: 'tr',
classNames: ['allocation-row', 'is-interactive'],
allocation: null,
// Used to determine whether the row should mention the node or the job
context: null,
2018-03-22 21:34:57 +00:00
// Internal state
stats: null,
statsError: false,
2017-09-19 14:47:10 +00:00
onClick() {},
click(event) {
lazyClick([this.get('onClick'), event]);
2017-09-19 14:47:10 +00:00
},
didReceiveAttrs() {
// TODO: Use this code again once the temporary workaround below
// is resolved.
// If the job for this allocation is incomplete, reload it to get
// detailed information.
// const allocation = this.get('allocation');
// if (
// allocation &&
// allocation.get('job') &&
// !allocation.get('job.isPending') &&
// !allocation.get('taskGroup')
// ) {
// const job = allocation.get('job.content');
// job && job.reload();
// }
// TEMPORARY: https://github.com/emberjs/data/issues/5209
// Ember Data doesn't like it when relationships aren't reflective,
// which means the allocation's job will be null if it hasn't been
// resolved through the allocation (allocation.get('job')) before
// being resolved through the store (store.findAll('job')). The
// workaround is to persist the jobID as a string on the allocation
// and manually re-link the two records here.
2018-03-22 21:34:57 +00:00
const allocation = this.get('allocation');
if (allocation) {
this.get('fetchStats').perform(allocation);
} else {
this.get('fetchStats').cancelAll();
this.set('stats', null);
}
run.scheduleOnce('afterRender', this, qualifyJob);
},
2018-03-22 21:34:57 +00:00
fetchStats: task(function*(allocation) {
const maxTiming = 5500;
const backoffSequence = [500, 800, 1300, 2100, 3400];
do {
try {
const stats = yield allocation.fetchStats();
this.set('stats', stats);
} catch (error) {
this.set('statsError', true);
break;
}
yield timeout(backoffSequence.shift() || maxTiming);
} while (!Ember.testing);
}).drop(),
2017-09-19 14:47:10 +00:00
});
function qualifyJob() {
const allocation = this.get('allocation');
if (allocation.get('originalJobId')) {
const job = this.get('store').peekRecord('job', allocation.get('originalJobId'));
if (job) {
allocation.setProperties({
job,
originalJobId: null,
});
if (job.get('isPartial')) {
job.reload();
}
} else {
this.get('store')
.findRecord('job', allocation.get('originalJobId'))
.then(job => {
allocation.set('job', job);
});
}
}
}