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