github.com/anuvu/nomad@v0.8.7-atom1/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      // TODO: Use this code again once the temporary workaround below
    37      // is resolved.
    38  
    39      // If the job for this allocation is incomplete, reload it to get
    40      // detailed information.
    41      // const allocation = this.get('allocation');
    42      // if (
    43      //   allocation &&
    44      //   allocation.get('job') &&
    45      //   !allocation.get('job.isPending') &&
    46      //   !allocation.get('taskGroup')
    47      // ) {
    48      //   const job = allocation.get('job.content');
    49      //   job && job.reload();
    50      // }
    51  
    52      // TEMPORARY: https://github.com/emberjs/data/issues/5209
    53      // Ember Data doesn't like it when relationships aren't reflective,
    54      // which means the allocation's job will be null if it hasn't been
    55      // resolved through the allocation (allocation.get('job')) before
    56      // being resolved through the store (store.findAll('job')). The
    57      // workaround is to persist the jobID as a string on the allocation
    58      // and manually re-link the two records here.
    59      const allocation = this.get('allocation');
    60  
    61      if (allocation) {
    62        run.scheduleOnce('afterRender', this, qualifyAllocation);
    63      } else {
    64        this.get('fetchStats').cancelAll();
    65        this.set('stats', null);
    66      }
    67    },
    68  
    69    fetchStats: task(function*(allocation) {
    70      const backoffSequence = this.get('backoffSequence').slice();
    71      const maxTiming = backoffSequence.pop();
    72  
    73      do {
    74        try {
    75          const stats = yield allocation.fetchStats();
    76          this.set('stats', stats);
    77          this.set('statsError', false);
    78        } catch (error) {
    79          this.set('statsError', true);
    80        }
    81        yield timeout(backoffSequence.shift() || maxTiming);
    82      } while (this.get('enablePolling'));
    83    }).drop(),
    84  });
    85  
    86  function qualifyAllocation() {
    87    const allocation = this.get('allocation');
    88    return allocation.reload().then(() => {
    89      this.get('fetchStats').perform(allocation);
    90      run.scheduleOnce('afterRender', this, qualifyJob);
    91    });
    92  }
    93  
    94  function qualifyJob() {
    95    const allocation = this.get('allocation');
    96    if (allocation.get('originalJobId')) {
    97      const job = this.get('store').peekRecord('job', allocation.get('originalJobId'));
    98      if (job) {
    99        allocation.setProperties({
   100          job,
   101          originalJobId: null,
   102        });
   103        if (job.get('isPartial')) {
   104          job.reload();
   105        }
   106      } else {
   107        this.get('store')
   108          .findRecord('job', allocation.get('originalJobId'))
   109          .then(job => {
   110            allocation.set('job', job);
   111          });
   112      }
   113    }
   114  }