github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/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 10 @classic 11 @tagName('') 12 export default class DrainPopover extends Component { 13 client = null; 14 isDisabled = false; 15 16 onError() {} 17 onDrain() {} 18 19 parseError = ''; 20 21 deadlineEnabled = false; 22 forceDrain = false; 23 drainSystemJobs = true; 24 25 @overridable(function() { 26 return this.durationQuickOptions[0]; 27 }) 28 selectedDurationQuickOption; 29 30 @equal('selectedDurationQuickOption.value', 'custom') durationIsCustom; 31 customDuration = ''; 32 33 @computed 34 get durationQuickOptions() { 35 return [ 36 { label: '1 Hour', value: '1h' }, 37 { label: '4 Hours', value: '4h' }, 38 { label: '8 Hours', value: '8h' }, 39 { label: '12 Hours', value: '12h' }, 40 { label: '1 Day', value: '1d' }, 41 { label: 'Custom', value: 'custom' }, 42 ]; 43 } 44 45 @computed( 46 'deadlineEnabled', 47 'durationIsCustom', 48 'customDuration', 49 'selectedDurationQuickOption.value' 50 ) 51 get deadline() { 52 if (!this.deadlineEnabled) return 0; 53 if (this.durationIsCustom) return this.customDuration; 54 return this.selectedDurationQuickOption.value; 55 } 56 57 @task(function*(close) { 58 if (!this.client) return; 59 const isUpdating = this.client.isDraining; 60 61 let deadline; 62 try { 63 deadline = new Duration(this.deadline).nanoseconds(); 64 } catch (err) { 65 this.set('parseError', err.message); 66 return; 67 } 68 69 const spec = { 70 Deadline: deadline, 71 IgnoreSystemJobs: !this.drainSystemJobs, 72 }; 73 74 close(); 75 76 try { 77 if (this.forceDrain) { 78 yield this.client.forceDrain(spec); 79 } else { 80 yield this.client.drain(spec); 81 } 82 this.onDrain(isUpdating); 83 } catch (err) { 84 this.onError(err); 85 } 86 }) 87 drain; 88 89 preventDefault(e) { 90 e.preventDefault(); 91 } 92 }