github.com/aminovpavel/nomad@v0.11.8/ui/app/components/drain-popover.js (about)

     1  import Component from '@ember/component';
     2  import { computed } from '@ember/object';
     3  import { equal } from '@ember/object/computed';
     4  import { computed as overridable } from 'ember-overridable-computed';
     5  import { task } from 'ember-concurrency';
     6  import Duration from 'duration-js';
     7  
     8  export default Component.extend({
     9    tagName: '',
    10  
    11    client: null,
    12    isDisabled: false,
    13  
    14    onError() {},
    15    onDrain() {},
    16  
    17    parseError: '',
    18  
    19    deadlineEnabled: false,
    20    forceDrain: false,
    21    drainSystemJobs: true,
    22  
    23    selectedDurationQuickOption: overridable(function() {
    24      return this.durationQuickOptions[0];
    25    }),
    26  
    27    durationIsCustom: equal('selectedDurationQuickOption.value', 'custom'),
    28    customDuration: '',
    29  
    30    durationQuickOptions: computed(() => [
    31      { label: '1 Hour', value: '1h' },
    32      { label: '4 Hours', value: '4h' },
    33      { label: '8 Hours', value: '8h' },
    34      { label: '12 Hours', value: '12h' },
    35      { label: '1 Day', value: '1d' },
    36      { label: 'Custom', value: 'custom' },
    37    ]),
    38  
    39    deadline: computed(
    40      'deadlineEnabled',
    41      'durationIsCustom',
    42      'customDuration',
    43      'selectedDurationQuickOption.value',
    44      function() {
    45        if (!this.deadlineEnabled) return 0;
    46        if (this.durationIsCustom) return this.customDuration;
    47        return this.selectedDurationQuickOption.value;
    48      }
    49    ),
    50  
    51    drain: task(function*(close) {
    52      if (!this.client) return;
    53      const isUpdating = this.client.isDraining;
    54  
    55      let deadline;
    56      try {
    57        deadline = new Duration(this.deadline).nanoseconds();
    58      } catch (err) {
    59        this.set('parseError', err.message);
    60        return;
    61      }
    62  
    63      const spec = {
    64        Deadline: deadline,
    65        IgnoreSystemJobs: !this.drainSystemJobs,
    66      };
    67  
    68      close();
    69  
    70      try {
    71        if (this.forceDrain) {
    72          yield this.client.forceDrain(spec);
    73        } else {
    74          yield this.client.drain(spec);
    75        }
    76        this.onDrain(isUpdating);
    77      } catch (err) {
    78        this.onError(err);
    79      }
    80    }),
    81  
    82    preventDefault(e) {
    83      e.preventDefault();
    84    },
    85  });