2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2019-11-12 01:05:47 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { equal } from '@ember/object/computed';
|
|
|
|
import { computed as overridable } from 'ember-overridable-computed';
|
|
|
|
import { task } from 'ember-concurrency';
|
2019-12-13 20:24:13 +00:00
|
|
|
import Duration from 'duration-js';
|
2020-06-10 13:49:16 +00:00
|
|
|
import { tagName } from '@ember-decorators/component';
|
|
|
|
import classic from 'ember-classic-decorator';
|
2021-10-22 14:51:31 +00:00
|
|
|
import localStorageProperty from 'nomad-ui/utils/properties/local-storage';
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
@tagName('')
|
|
|
|
export default class DrainPopover extends Component {
|
|
|
|
client = null;
|
|
|
|
isDisabled = false;
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
onError() {}
|
|
|
|
onDrain() {}
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
parseError = '';
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
deadlineEnabled = false;
|
|
|
|
forceDrain = false;
|
|
|
|
drainSystemJobs = true;
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2021-10-22 14:51:31 +00:00
|
|
|
@localStorageProperty('nomadDrainOptions', {}) drainOptions;
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
2021-12-28 14:45:20 +00:00
|
|
|
super.didReceiveAttrs();
|
2021-10-22 14:51:31 +00:00
|
|
|
// Load drain config values from local storage if availabe.
|
|
|
|
[
|
|
|
|
'deadlineEnabled',
|
|
|
|
'customDuration',
|
|
|
|
'forceDrain',
|
|
|
|
'drainSystemJobs',
|
|
|
|
'selectedDurationQuickOption',
|
2021-12-28 14:45:20 +00:00
|
|
|
].forEach((k) => {
|
2021-10-22 14:51:31 +00:00
|
|
|
if (k in this.drainOptions) {
|
|
|
|
this[k] = this.drainOptions[k];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@overridable(function () {
|
2019-11-12 22:18:26 +00:00
|
|
|
return this.durationQuickOptions[0];
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
selectedDurationQuickOption;
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@equal('selectedDurationQuickOption.value', 'custom') durationIsCustom;
|
|
|
|
customDuration = '';
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed
|
|
|
|
get durationQuickOptions() {
|
2020-06-09 21:03:28 +00:00
|
|
|
return [
|
|
|
|
{ label: '1 Hour', value: '1h' },
|
|
|
|
{ label: '4 Hours', value: '4h' },
|
|
|
|
{ label: '8 Hours', value: '8h' },
|
|
|
|
{ label: '12 Hours', value: '12h' },
|
|
|
|
{ label: '1 Day', value: '1d' },
|
|
|
|
{ label: 'Custom', value: 'custom' },
|
|
|
|
];
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed(
|
2019-11-12 01:05:47 +00:00
|
|
|
'deadlineEnabled',
|
|
|
|
'durationIsCustom',
|
|
|
|
'customDuration',
|
2020-06-10 13:49:16 +00:00
|
|
|
'selectedDurationQuickOption.value'
|
|
|
|
)
|
|
|
|
get deadline() {
|
|
|
|
if (!this.deadlineEnabled) return 0;
|
|
|
|
if (this.durationIsCustom) return this.customDuration;
|
|
|
|
return this.selectedDurationQuickOption.value;
|
|
|
|
}
|
2019-11-12 01:05:47 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* (close) {
|
2019-11-12 01:05:47 +00:00
|
|
|
if (!this.client) return;
|
2019-11-19 06:36:07 +00:00
|
|
|
const isUpdating = this.client.isDraining;
|
2019-11-12 01:05:47 +00:00
|
|
|
|
|
|
|
let deadline;
|
|
|
|
try {
|
2019-12-13 20:24:13 +00:00
|
|
|
deadline = new Duration(this.deadline).nanoseconds();
|
2019-11-12 01:05:47 +00:00
|
|
|
} catch (err) {
|
2019-12-13 20:24:13 +00:00
|
|
|
this.set('parseError', err.message);
|
2019-11-12 01:05:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const spec = {
|
|
|
|
Deadline: deadline,
|
|
|
|
IgnoreSystemJobs: !this.drainSystemJobs,
|
|
|
|
};
|
|
|
|
|
2021-10-22 14:51:31 +00:00
|
|
|
this.drainOptions = {
|
|
|
|
deadlineEnabled: this.deadlineEnabled,
|
|
|
|
customDuration: this.deadline,
|
|
|
|
selectedDurationQuickOption: this.selectedDurationQuickOption,
|
|
|
|
drainSystemJobs: this.drainSystemJobs,
|
|
|
|
forceDrain: this.forceDrain,
|
|
|
|
};
|
|
|
|
|
2019-11-12 01:05:47 +00:00
|
|
|
close();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (this.forceDrain) {
|
|
|
|
yield this.client.forceDrain(spec);
|
|
|
|
} else {
|
|
|
|
yield this.client.drain(spec);
|
|
|
|
}
|
2019-11-19 06:36:07 +00:00
|
|
|
this.onDrain(isUpdating);
|
2019-11-12 01:05:47 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.onError(err);
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
drain;
|
2019-11-12 01:05:47 +00:00
|
|
|
|
|
|
|
preventDefault(e) {
|
|
|
|
e.preventDefault();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|