github.com/hernad/nomad@v1.6.112/ui/app/components/drain-popover.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import Component from '@ember/component';
     7  import { computed } from '@ember/object';
     8  import { equal } from '@ember/object/computed';
     9  import { computed as overridable } from 'ember-overridable-computed';
    10  import { task } from 'ember-concurrency';
    11  import Duration from 'duration-js';
    12  import { tagName } from '@ember-decorators/component';
    13  import classic from 'ember-classic-decorator';
    14  import localStorageProperty from 'nomad-ui/utils/properties/local-storage';
    15  
    16  @classic
    17  @tagName('')
    18  export default class DrainPopover extends Component {
    19    client = null;
    20    isDisabled = false;
    21  
    22    onError() {}
    23    onDrain() {}
    24  
    25    parseError = '';
    26  
    27    deadlineEnabled = false;
    28    forceDrain = false;
    29    drainSystemJobs = true;
    30  
    31    @localStorageProperty('nomadDrainOptions', {}) drainOptions;
    32  
    33    didReceiveAttrs() {
    34      super.didReceiveAttrs();
    35      // Load drain config values from local storage if availabe.
    36      [
    37        'deadlineEnabled',
    38        'customDuration',
    39        'forceDrain',
    40        'drainSystemJobs',
    41        'selectedDurationQuickOption',
    42      ].forEach((k) => {
    43        if (k in this.drainOptions) {
    44          this[k] = this.drainOptions[k];
    45        }
    46      });
    47    }
    48  
    49    @overridable(function () {
    50      return this.durationQuickOptions[0];
    51    })
    52    selectedDurationQuickOption;
    53  
    54    @equal('selectedDurationQuickOption.value', 'custom') durationIsCustom;
    55    customDuration = '';
    56  
    57    @computed
    58    get durationQuickOptions() {
    59      return [
    60        { label: '1 Hour', value: '1h' },
    61        { label: '4 Hours', value: '4h' },
    62        { label: '8 Hours', value: '8h' },
    63        { label: '12 Hours', value: '12h' },
    64        { label: '1 Day', value: '1d' },
    65        { label: 'Custom', value: 'custom' },
    66      ];
    67    }
    68  
    69    @computed(
    70      'deadlineEnabled',
    71      'durationIsCustom',
    72      'customDuration',
    73      'selectedDurationQuickOption.value'
    74    )
    75    get deadline() {
    76      if (!this.deadlineEnabled) return 0;
    77      if (this.durationIsCustom) return this.customDuration;
    78      return this.selectedDurationQuickOption.value;
    79    }
    80  
    81    @task(function* (close) {
    82      if (!this.client) return;
    83      const isUpdating = this.client.isDraining;
    84  
    85      let deadline;
    86      try {
    87        deadline = new Duration(this.deadline).nanoseconds();
    88      } catch (err) {
    89        this.set('parseError', err.message);
    90        return;
    91      }
    92  
    93      const spec = {
    94        Deadline: deadline,
    95        IgnoreSystemJobs: !this.drainSystemJobs,
    96      };
    97  
    98      this.drainOptions = {
    99        deadlineEnabled: this.deadlineEnabled,
   100        customDuration: this.deadline,
   101        selectedDurationQuickOption: this.selectedDurationQuickOption,
   102        drainSystemJobs: this.drainSystemJobs,
   103        forceDrain: this.forceDrain,
   104      };
   105  
   106      close();
   107  
   108      try {
   109        if (this.forceDrain) {
   110          yield this.client.forceDrain(spec);
   111        } else {
   112          yield this.client.drain(spec);
   113        }
   114        this.onDrain(isUpdating);
   115      } catch (err) {
   116        this.onError(err);
   117      }
   118    })
   119    drain;
   120  
   121    preventDefault(e) {
   122      e.preventDefault();
   123    }
   124  }