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

81 lines
2 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';
2018-04-21 01:11:32 +00:00
import { computed } from '@ember/object';
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-04-21 01:11:32 +00:00
backoffSequence: computed(() => [500, 800, 1300, 2100, 3400, 5500]),
2018-03-22 21:34:57 +00:00
// Internal state
stats: null,
statsError: false,
enablePolling: computed(() => !Ember.testing),
2018-04-21 01:11:32 +00:00
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() {
2018-03-22 21:34:57 +00:00
const allocation = this.get('allocation');
if (allocation) {
2018-05-02 20:59:28 +00:00
run.scheduleOnce('afterRender', this, qualifyAllocation);
2018-03-22 21:34:57 +00:00
} else {
this.get('fetchStats').cancelAll();
this.set('stats', null);
}
},
2018-03-22 21:34:57 +00:00
fetchStats: task(function*(allocation) {
2018-04-21 01:11:32 +00:00
const backoffSequence = this.get('backoffSequence').slice();
const maxTiming = backoffSequence.pop();
2018-03-22 21:34:57 +00:00
do {
try {
const stats = yield allocation.fetchStats();
this.set('stats', stats);
this.set('statsError', false);
2018-03-22 21:34:57 +00:00
} catch (error) {
this.set('statsError', true);
}
yield timeout(backoffSequence.shift() || maxTiming);
2018-04-21 01:11:32 +00:00
} while (this.get('enablePolling'));
2018-03-22 21:34:57 +00:00
}).drop(),
2017-09-19 14:47:10 +00:00
});
2018-05-02 20:59:28 +00:00
function qualifyAllocation() {
const allocation = this.get('allocation');
return allocation.reload().then(() => {
this.get('fetchStats').perform(allocation);
// Make sure that the job record in the store for this allocation
// is complete and not a partial from the list endpoint
if (
allocation &&
allocation.get('job') &&
!allocation.get('job.isPending') &&
!allocation.get('taskGroup')
) {
const job = allocation.get('job.content');
job && job.reload();
}
});
}