github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/controllers/allocations/allocation/index.js (about)

     1  /* eslint-disable ember/no-observers */
     2  import Controller from '@ember/controller';
     3  import { inject as service } from '@ember/service';
     4  import { action, computed } from '@ember/object';
     5  import { observes } from '@ember-decorators/object';
     6  import { computed as overridable } from 'ember-overridable-computed';
     7  import { alias } from '@ember/object/computed';
     8  import { task } from 'ember-concurrency';
     9  import Sortable from 'nomad-ui/mixins/sortable';
    10  import { lazyClick } from 'nomad-ui/helpers/lazy-click';
    11  import { watchRecord } from 'nomad-ui/utils/properties/watch';
    12  import messageForError from 'nomad-ui/utils/message-from-adapter-error';
    13  import classic from 'ember-classic-decorator';
    14  
    15  @classic
    16  export default class IndexController extends Controller.extend(Sortable) {
    17    @service token;
    18  
    19    queryParams = [
    20      {
    21        sortProperty: 'sort',
    22      },
    23      {
    24        sortDescending: 'desc',
    25      },
    26    ];
    27  
    28    sortProperty = 'name';
    29    sortDescending = false;
    30  
    31    @alias('model.states') listToSort;
    32    @alias('listSorted') sortedStates;
    33  
    34    // Set in the route
    35    preempter = null;
    36  
    37    @overridable(function() {
    38      // { title, description }
    39      return null;
    40    })
    41    error;
    42  
    43    @computed('model.allocatedResources.ports.@each.label')
    44    get ports() {
    45      return (this.get('model.allocatedResources.ports') || []).sortBy('label');
    46    }
    47  
    48    @computed('model.taskGroup.services.@each.name')
    49    get services() {
    50      return this.get('model.taskGroup.services').sortBy('name');
    51    }
    52  
    53    onDismiss() {
    54      this.set('error', null);
    55    }
    56  
    57    @watchRecord('allocation') watchNext;
    58  
    59    @observes('model.nextAllocation.clientStatus')
    60    observeWatchNext() {
    61      const nextAllocation = this.model.nextAllocation;
    62      if (nextAllocation && nextAllocation.content) {
    63        this.watchNext.perform(nextAllocation);
    64      } else {
    65        this.watchNext.cancelAll();
    66      }
    67    }
    68  
    69    @task(function*() {
    70      try {
    71        yield this.model.stop();
    72        // Eagerly update the allocation clientStatus to avoid flickering
    73        this.model.set('clientStatus', 'complete');
    74      } catch (err) {
    75        this.set('error', {
    76          title: 'Could Not Stop Allocation',
    77          description: messageForError(err, 'manage allocation lifecycle'),
    78        });
    79      }
    80    })
    81    stopAllocation;
    82  
    83    @task(function*() {
    84      try {
    85        yield this.model.restart();
    86      } catch (err) {
    87        this.set('error', {
    88          title: 'Could Not Restart Allocation',
    89          description: messageForError(err, 'manage allocation lifecycle'),
    90        });
    91      }
    92    })
    93    restartAllocation;
    94  
    95    @action
    96    gotoTask(allocation, task) {
    97      this.transitionToRoute('allocations.allocation.task', task);
    98    }
    99  
   100    @action
   101    taskClick(allocation, task, event) {
   102      lazyClick([() => this.send('gotoTask', allocation, task), event]);
   103    }
   104  }