2020-06-09 21:03:28 +00:00
|
|
|
import { alias, equal, or, and, mapBy } from '@ember/object/computed';
|
2017-12-15 21:39:18 +00:00
|
|
|
import { computed } from '@ember/object';
|
2021-02-17 21:01:44 +00:00
|
|
|
import Model from '@ember-data/model';
|
|
|
|
import { attr, belongsTo, hasMany } from '@ember-data/model';
|
2021-10-12 20:36:10 +00:00
|
|
|
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
|
2018-08-15 00:29:51 +00:00
|
|
|
import RSVP from 'rsvp';
|
|
|
|
import { assert } from '@ember/debug';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2021-10-07 21:11:38 +00:00
|
|
|
const JOB_TYPES = ['service', 'batch', 'system', 'sysbatch'];
|
2018-01-25 17:29:47 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
export default class Job extends Model {
|
|
|
|
@attr('string') region;
|
|
|
|
@attr('string') name;
|
|
|
|
@attr('string') plainId;
|
|
|
|
@attr('string') type;
|
|
|
|
@attr('number') priority;
|
|
|
|
@attr('boolean') allAtOnce;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@attr('string') status;
|
|
|
|
@attr('string') statusDescription;
|
|
|
|
@attr('number') createIndex;
|
|
|
|
@attr('number') modifyIndex;
|
2020-09-19 00:33:43 +00:00
|
|
|
@attr('date') submitTime;
|
2021-10-12 20:36:10 +00:00
|
|
|
|
|
|
|
@fragment('structured-attributes') meta;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2018-01-23 21:24:13 +00:00
|
|
|
// True when the job is the parent periodic or parameterized jobs
|
|
|
|
// Instances of periodic or parameterized jobs are false for both properties
|
2020-06-10 13:49:16 +00:00
|
|
|
@attr('boolean') periodic;
|
|
|
|
@attr('boolean') parameterized;
|
|
|
|
@attr('boolean') dispatched;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@attr() periodicDetails;
|
|
|
|
@attr() parameterizedDetails;
|
2018-01-25 17:29:47 +00:00
|
|
|
|
2022-02-16 14:01:25 +00:00
|
|
|
@computed('plainId')
|
|
|
|
get idWithNamespace() {
|
2022-05-13 21:01:27 +00:00
|
|
|
return `${this.plainId}@${this.belongsTo('namespace').id() ?? 'default'}`;
|
2022-02-16 14:01:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('periodic', 'parameterized', 'dispatched')
|
|
|
|
get hasChildren() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.periodic || (this.parameterized && !this.dispatched);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-01-23 21:24:13 +00:00
|
|
|
|
2021-10-07 21:11:38 +00:00
|
|
|
@computed('type')
|
|
|
|
get hasClientStatus() {
|
|
|
|
return this.type === 'system' || this.type === 'sysbatch';
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@belongsTo('job', { inverse: 'children' }) parent;
|
|
|
|
@hasMany('job', { inverse: 'parent' }) children;
|
2018-01-23 21:24:13 +00:00
|
|
|
|
2018-01-27 00:27:04 +00:00
|
|
|
// The parent job name is prepended to child launch job names
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('name', 'parent.content')
|
2020-06-10 13:49:16 +00:00
|
|
|
get trimmedName() {
|
2021-12-28 16:08:12 +00:00
|
|
|
return this.get('parent.content')
|
|
|
|
? this.name.replace(/.+?\//, '')
|
|
|
|
: this.name;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-01-27 00:27:04 +00:00
|
|
|
|
2018-01-26 23:01:10 +00:00
|
|
|
// A composite of type and other job attributes to determine
|
|
|
|
// a better type descriptor for human interpretation rather
|
|
|
|
// than for scheduling.
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('type', 'periodic', 'parameterized')
|
|
|
|
get displayType() {
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.periodic) {
|
2018-01-26 23:01:10 +00:00
|
|
|
return 'periodic';
|
2019-03-26 07:46:44 +00:00
|
|
|
} else if (this.parameterized) {
|
2018-01-26 23:01:10 +00:00
|
|
|
return 'parameterized';
|
|
|
|
}
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.type;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-01-26 23:01:10 +00:00
|
|
|
|
2018-01-25 17:29:47 +00:00
|
|
|
// A composite of type and other job attributes to determine
|
|
|
|
// type for templating rather than scheduling
|
2021-12-28 16:08:12 +00:00
|
|
|
@computed(
|
|
|
|
'type',
|
|
|
|
'periodic',
|
|
|
|
'parameterized',
|
|
|
|
'parent.{periodic,parameterized}'
|
|
|
|
)
|
2020-06-10 13:49:16 +00:00
|
|
|
get templateType() {
|
|
|
|
const type = this.type;
|
|
|
|
|
|
|
|
if (this.get('parent.periodic')) {
|
|
|
|
return 'periodic-child';
|
|
|
|
} else if (this.get('parent.parameterized')) {
|
|
|
|
return 'parameterized-child';
|
|
|
|
} else if (this.periodic) {
|
|
|
|
return 'periodic';
|
|
|
|
} else if (this.parameterized) {
|
|
|
|
return 'parameterized';
|
|
|
|
} else if (JOB_TYPES.includes(type)) {
|
|
|
|
// Guard against the API introducing a new type before the UI
|
|
|
|
// is prepared to handle it.
|
|
|
|
return this.type;
|
2018-01-25 17:29:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
// A fail-safe in the event the API introduces a new type.
|
|
|
|
return 'service';
|
|
|
|
}
|
|
|
|
|
|
|
|
@attr() datacenters;
|
|
|
|
@fragmentArray('task-group', { defaultValue: () => [] }) taskGroups;
|
|
|
|
@belongsTo('job-summary') summary;
|
2018-02-08 23:02:48 +00:00
|
|
|
|
2018-03-15 21:03:14 +00:00
|
|
|
// A job model created from the jobs list response will be lacking
|
|
|
|
// task groups. This is an indicator that it needs to be reloaded
|
|
|
|
// if task group information is important.
|
2020-06-10 13:49:16 +00:00
|
|
|
@equal('taskGroups.length', 0) isPartial;
|
2018-03-15 21:03:14 +00:00
|
|
|
|
2018-03-15 17:27:01 +00:00
|
|
|
// If a job has only been loaded through the list request, the task groups
|
|
|
|
// are still unknown. However, the count of task groups is available through
|
|
|
|
// the job-summary model which is embedded in the jobs list response.
|
2020-06-10 13:49:16 +00:00
|
|
|
@or('taskGroups.length', 'taskGroupSummaries.length') taskGroupCount;
|
2018-03-15 17:27:01 +00:00
|
|
|
|
2018-02-08 23:02:48 +00:00
|
|
|
// Alias through to the summary, as if there was no relationship
|
2020-06-10 13:49:16 +00:00
|
|
|
@alias('summary.taskGroupSummaries') taskGroupSummaries;
|
|
|
|
@alias('summary.queuedAllocs') queuedAllocs;
|
|
|
|
@alias('summary.startingAllocs') startingAllocs;
|
|
|
|
@alias('summary.runningAllocs') runningAllocs;
|
|
|
|
@alias('summary.completeAllocs') completeAllocs;
|
|
|
|
@alias('summary.failedAllocs') failedAllocs;
|
|
|
|
@alias('summary.lostAllocs') lostAllocs;
|
2022-04-22 15:25:02 +00:00
|
|
|
@alias('summary.unknownAllocs') unknownAllocs;
|
2020-06-10 13:49:16 +00:00
|
|
|
@alias('summary.totalAllocs') totalAllocs;
|
|
|
|
@alias('summary.pendingChildren') pendingChildren;
|
|
|
|
@alias('summary.runningChildren') runningChildren;
|
|
|
|
@alias('summary.deadChildren') deadChildren;
|
|
|
|
@alias('summary.totalChildren') totalChildren;
|
|
|
|
|
|
|
|
@attr('number') version;
|
|
|
|
|
|
|
|
@hasMany('job-versions') versions;
|
|
|
|
@hasMany('allocations') allocations;
|
|
|
|
@hasMany('deployments') deployments;
|
|
|
|
@hasMany('evaluations') evaluations;
|
2022-07-13 15:40:13 +00:00
|
|
|
@hasMany('variables') variables;
|
2020-06-10 13:49:16 +00:00
|
|
|
@belongsTo('namespace') namespace;
|
2020-07-24 04:39:50 +00:00
|
|
|
@belongsTo('job-scale') scaleState;
|
2020-06-10 13:49:16 +00:00
|
|
|
|
2020-10-29 12:46:42 +00:00
|
|
|
@hasMany('recommendation-summary') recommendationSummaries;
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('taskGroups.@each.drivers')
|
|
|
|
get drivers() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.taskGroups
|
2018-05-10 00:04:45 +00:00
|
|
|
.mapBy('drivers')
|
|
|
|
.reduce((all, drivers) => {
|
|
|
|
all.push(...drivers);
|
|
|
|
return all;
|
|
|
|
}, [])
|
|
|
|
.uniq();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-05-10 00:04:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@mapBy('allocations', 'unhealthyDrivers') allocationsUnhealthyDrivers;
|
2020-06-09 21:03:28 +00:00
|
|
|
|
2018-05-10 00:04:45 +00:00
|
|
|
// Getting all unhealthy drivers for a job can be incredibly expensive if the job
|
|
|
|
// has many allocations. This can lead to making an API request for many nodes.
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('allocations', 'allocationsUnhealthyDrivers.[]')
|
2020-06-10 13:49:16 +00:00
|
|
|
get unhealthyDrivers() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.allocations
|
2018-05-10 00:04:45 +00:00
|
|
|
.mapBy('unhealthyDrivers')
|
|
|
|
.reduce((all, drivers) => {
|
|
|
|
all.push(...drivers);
|
|
|
|
return all;
|
|
|
|
}, [])
|
|
|
|
.uniq();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-05-10 00:04:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('evaluations.@each.isBlocked')
|
|
|
|
get hasBlockedEvaluation() {
|
2021-12-28 16:08:12 +00:00
|
|
|
return this.evaluations
|
|
|
|
.toArray()
|
|
|
|
.some((evaluation) => evaluation.get('isBlocked'));
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-03-08 22:14:55 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@and('latestFailureEvaluation', 'hasBlockedEvaluation') hasPlacementFailures;
|
2017-11-29 01:23:30 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('evaluations.{@each.modifyIndex,isPending}')
|
|
|
|
get latestEvaluation() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const evaluations = this.evaluations;
|
2017-11-29 01:23:30 +00:00
|
|
|
if (!evaluations || evaluations.get('isPending')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return evaluations.sortBy('modifyIndex').get('lastObject');
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-11-29 01:23:30 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('evaluations.{@each.modifyIndex,isPending}')
|
|
|
|
get latestFailureEvaluation() {
|
2020-06-09 21:03:28 +00:00
|
|
|
const evaluations = this.evaluations;
|
|
|
|
if (!evaluations || evaluations.get('isPending')) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-11-29 01:23:30 +00:00
|
|
|
|
2020-06-09 21:03:28 +00:00
|
|
|
const failureEvaluations = evaluations.filterBy('hasPlacementFailures');
|
|
|
|
if (failureEvaluations) {
|
|
|
|
return failureEvaluations.sortBy('modifyIndex').get('lastObject');
|
2017-11-29 01:23:30 +00:00
|
|
|
}
|
2020-06-09 21:03:28 +00:00
|
|
|
|
2020-06-10 14:07:16 +00:00
|
|
|
return undefined;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-11-29 01:23:30 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@equal('type', 'service') supportsDeployments;
|
2017-10-24 19:35:29 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@belongsTo('deployment', { inverse: 'jobForLatest' }) latestDeployment;
|
2018-07-28 00:40:09 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('latestDeployment', 'latestDeployment.isRunning')
|
|
|
|
get runningDeployment() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const latest = this.latestDeployment;
|
2018-07-28 00:40:09 +00:00
|
|
|
if (latest.get('isRunning')) return latest;
|
2020-06-10 14:07:16 +00:00
|
|
|
return undefined;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
fetchRawDefinition() {
|
|
|
|
return this.store.adapterFor('job').fetchRawDefinition(this);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2018-01-26 22:32:11 +00:00
|
|
|
forcePeriodic() {
|
|
|
|
return this.store.adapterFor('job').forcePeriodic(this);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-01-26 22:32:11 +00:00
|
|
|
|
2018-04-18 19:17:55 +00:00
|
|
|
stop() {
|
|
|
|
return this.store.adapterFor('job').stop(this);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-04-18 19:17:55 +00:00
|
|
|
|
2018-08-15 00:29:51 +00:00
|
|
|
plan() {
|
2019-03-26 07:46:44 +00:00
|
|
|
assert('A job must be parsed before planned', this._newDefinitionJSON);
|
2018-08-15 00:29:51 +00:00
|
|
|
return this.store.adapterFor('job').plan(this);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-15 00:29:51 +00:00
|
|
|
|
2018-08-15 01:26:26 +00:00
|
|
|
run() {
|
2019-03-26 07:46:44 +00:00
|
|
|
assert('A job must be parsed before ran', this._newDefinitionJSON);
|
2018-08-15 01:26:26 +00:00
|
|
|
return this.store.adapterFor('job').run(this);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-15 01:26:26 +00:00
|
|
|
|
2018-08-21 23:44:31 +00:00
|
|
|
update() {
|
2019-03-26 07:46:44 +00:00
|
|
|
assert('A job must be parsed before updated', this._newDefinitionJSON);
|
2018-08-21 23:44:31 +00:00
|
|
|
return this.store.adapterFor('job').update(this);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-21 23:44:31 +00:00
|
|
|
|
2018-08-15 00:29:51 +00:00
|
|
|
parse() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const definition = this._newDefinition;
|
2018-08-15 00:29:51 +00:00
|
|
|
let promise;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// If the definition is already JSON then it doesn't need to be parsed.
|
|
|
|
const json = JSON.parse(definition);
|
2018-08-20 22:04:33 +00:00
|
|
|
this.set('_newDefinitionJSON', json);
|
2018-08-21 23:44:31 +00:00
|
|
|
|
|
|
|
// You can't set the ID of a record that already exists
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.isNew) {
|
2018-08-21 23:44:31 +00:00
|
|
|
this.setIdByPayload(json);
|
|
|
|
}
|
|
|
|
|
2018-08-17 00:22:58 +00:00
|
|
|
promise = RSVP.resolve(definition);
|
2018-08-15 00:29:51 +00:00
|
|
|
} catch (err) {
|
|
|
|
// If the definition is invalid JSON, assume it is HCL. If it is invalid
|
|
|
|
// in anyway, the parse endpoint will throw an error.
|
|
|
|
promise = this.store
|
|
|
|
.adapterFor('job')
|
2019-03-26 07:46:44 +00:00
|
|
|
.parse(this._newDefinition)
|
2021-12-28 14:45:20 +00:00
|
|
|
.then((response) => {
|
2018-08-15 00:29:51 +00:00
|
|
|
this.set('_newDefinitionJSON', response);
|
2018-08-21 23:44:31 +00:00
|
|
|
this.setIdByPayload(response);
|
2018-08-15 00:29:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-15 00:29:51 +00:00
|
|
|
|
2020-07-30 15:43:15 +00:00
|
|
|
scale(group, count, message) {
|
2021-12-28 16:08:12 +00:00
|
|
|
if (message == null)
|
|
|
|
message = `Manually scaled to ${count} from the Nomad UI`;
|
2020-07-30 15:43:15 +00:00
|
|
|
return this.store.adapterFor('job').scale(this, group, count, message);
|
2020-06-17 08:48:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 22:27:41 +00:00
|
|
|
dispatch(meta, payload) {
|
|
|
|
return this.store.adapterFor('job').dispatch(this, meta, payload);
|
|
|
|
}
|
|
|
|
|
2018-08-21 23:44:31 +00:00
|
|
|
setIdByPayload(payload) {
|
2018-08-15 01:26:26 +00:00
|
|
|
const namespace = payload.Namespace || 'default';
|
|
|
|
const id = payload.Name;
|
|
|
|
|
|
|
|
this.set('plainId', id);
|
2019-10-08 18:44:19 +00:00
|
|
|
this.set('_idBeforeSaving', JSON.stringify([id, namespace]));
|
2018-08-15 01:26:26 +00:00
|
|
|
|
|
|
|
const namespaceRecord = this.store.peekRecord('namespace', namespace);
|
|
|
|
if (namespaceRecord) {
|
|
|
|
this.set('namespace', namespaceRecord);
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-15 00:29:51 +00:00
|
|
|
|
2018-08-21 23:44:31 +00:00
|
|
|
resetId() {
|
2021-12-28 16:08:12 +00:00
|
|
|
this.set(
|
|
|
|
'id',
|
|
|
|
JSON.stringify([this.plainId, this.get('namespace.name') || 'default'])
|
|
|
|
);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-21 23:44:31 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('status')
|
|
|
|
get statusClass() {
|
2017-09-19 14:47:10 +00:00
|
|
|
const classMap = {
|
|
|
|
pending: 'is-pending',
|
|
|
|
running: 'is-primary',
|
|
|
|
dead: 'is-light',
|
|
|
|
};
|
|
|
|
|
2019-03-26 07:46:44 +00:00
|
|
|
return classMap[this.status] || 'is-dark';
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@attr('string') payload;
|
2018-01-26 00:50:31 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('payload')
|
|
|
|
get decodedPayload() {
|
2018-01-26 00:50:31 +00:00
|
|
|
// Lazily decode the base64 encoded payload
|
2019-03-26 07:46:44 +00:00
|
|
|
return window.atob(this.payload || '');
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-15 00:29:51 +00:00
|
|
|
|
|
|
|
// An arbitrary HCL or JSON string that is used by the serializer to plan
|
|
|
|
// and run this job. Used for both new job models and saved job models.
|
2020-06-10 13:49:16 +00:00
|
|
|
@attr('string') _newDefinition;
|
2018-08-15 00:29:51 +00:00
|
|
|
|
|
|
|
// The new definition may be HCL, in which case the API will need to parse the
|
|
|
|
// spec first. In order to preserve both the original HCL and the parsed response
|
|
|
|
// that will be submitted to the create job endpoint, another prop is necessary.
|
2020-06-10 13:49:16 +00:00
|
|
|
@attr('string') _newDefinitionJSON;
|
2022-07-13 15:40:13 +00:00
|
|
|
|
|
|
|
@computed('variables', 'parent', 'plainId')
|
|
|
|
get pathLinkedVariable() {
|
|
|
|
if (this.parent.get('id')) {
|
|
|
|
return this.variables?.findBy(
|
|
|
|
'path',
|
2022-07-20 16:19:01 +00:00
|
|
|
`nomad/jobs/${JSON.parse(this.parent.get('id'))[0]}`
|
2022-07-13 15:40:13 +00:00
|
|
|
);
|
|
|
|
} else {
|
2022-07-20 16:19:01 +00:00
|
|
|
return this.variables?.findBy('path', `nomad/jobs/${this.plainId}`);
|
2022-07-13 15:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|