github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/ui/app/components/allocation-row.js (about) 1 import Ember from 'ember'; 2 import { inject as service } from '@ember/service'; 3 import Component from '@ember/component'; 4 import { computed } from '@ember/object'; 5 import { run } from '@ember/runloop'; 6 import { lazyClick } from '../helpers/lazy-click'; 7 import { task, timeout } from 'ember-concurrency'; 8 9 export default Component.extend({ 10 store: service(), 11 12 tagName: 'tr', 13 14 classNames: ['allocation-row', 'is-interactive'], 15 16 allocation: null, 17 18 // Used to determine whether the row should mention the node or the job 19 context: null, 20 21 backoffSequence: computed(() => [500, 800, 1300, 2100, 3400, 5500]), 22 23 // Internal state 24 stats: null, 25 statsError: false, 26 27 enablePolling: computed(() => !Ember.testing), 28 29 onClick() {}, 30 31 click(event) { 32 lazyClick([this.get('onClick'), event]); 33 }, 34 35 didReceiveAttrs() { 36 const allocation = this.get('allocation'); 37 38 if (allocation) { 39 run.scheduleOnce('afterRender', this, qualifyAllocation); 40 } else { 41 this.get('fetchStats').cancelAll(); 42 this.set('stats', null); 43 } 44 }, 45 46 fetchStats: task(function*(allocation) { 47 const backoffSequence = this.get('backoffSequence').slice(); 48 const maxTiming = backoffSequence.pop(); 49 50 do { 51 try { 52 const stats = yield allocation.fetchStats(); 53 this.set('stats', stats); 54 this.set('statsError', false); 55 } catch (error) { 56 this.set('statsError', true); 57 } 58 yield timeout(backoffSequence.shift() || maxTiming); 59 } while (this.get('enablePolling')); 60 }).drop(), 61 }); 62 63 function qualifyAllocation() { 64 const allocation = this.get('allocation'); 65 return allocation.reload().then(() => { 66 this.get('fetchStats').perform(allocation); 67 68 // Make sure that the job record in the store for this allocation 69 // is complete and not a partial from the list endpoint 70 if ( 71 allocation && 72 allocation.get('job') && 73 !allocation.get('job.isPending') && 74 !allocation.get('taskGroup') 75 ) { 76 const job = allocation.get('job.content'); 77 job && job.reload(); 78 } 79 }); 80 }