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';
|
2017-09-19 14:47:10 +00:00
|
|
|
import Model from 'ember-data/model';
|
|
|
|
import attr from 'ember-data/attr';
|
2017-10-10 16:36:15 +00:00
|
|
|
import { belongsTo, hasMany } from 'ember-data/relationships';
|
2017-09-19 14:47:10 +00:00
|
|
|
import { fragmentArray } from 'ember-data-model-fragments/attributes';
|
2018-08-15 00:29:51 +00:00
|
|
|
import RSVP from 'rsvp';
|
|
|
|
import { assert } from '@ember/debug';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2018-01-25 17:29:47 +00:00
|
|
|
const JOB_TYPES = ['service', 'batch', 'system'];
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
export default Model.extend({
|
|
|
|
region: attr('string'),
|
|
|
|
name: attr('string'),
|
2017-10-23 17:22:58 +00:00
|
|
|
plainId: attr('string'),
|
2017-09-19 14:47:10 +00:00
|
|
|
type: attr('string'),
|
|
|
|
priority: attr('number'),
|
|
|
|
allAtOnce: attr('boolean'),
|
|
|
|
|
|
|
|
status: attr('string'),
|
|
|
|
statusDescription: attr('string'),
|
|
|
|
createIndex: attr('number'),
|
|
|
|
modifyIndex: attr('number'),
|
|
|
|
|
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
|
2017-09-19 14:47:10 +00:00
|
|
|
periodic: attr('boolean'),
|
|
|
|
parameterized: attr('boolean'),
|
2018-11-08 05:04:27 +00:00
|
|
|
dispatched: attr('boolean'),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2018-01-25 17:29:47 +00:00
|
|
|
periodicDetails: attr(),
|
|
|
|
parameterizedDetails: attr(),
|
|
|
|
|
2018-11-08 05:04:27 +00:00
|
|
|
hasChildren: computed('periodic', 'parameterized', 'dispatched', function() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.periodic || (this.parameterized && !this.dispatched);
|
2018-11-08 05:04:27 +00:00
|
|
|
}),
|
2018-01-23 21:24:13 +00:00
|
|
|
|
|
|
|
parent: belongsTo('job', { inverse: 'children' }),
|
|
|
|
children: hasMany('job', { inverse: 'parent' }),
|
|
|
|
|
2018-01-27 00:27:04 +00:00
|
|
|
// The parent job name is prepended to child launch job names
|
|
|
|
trimmedName: computed('name', 'parent', function() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.get('parent.content') ? this.name.replace(/.+?\//, '') : this.name;
|
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.
|
|
|
|
displayType: computed('type', 'periodic', 'parameterized', function() {
|
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;
|
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
|
|
|
|
templateType: computed(
|
|
|
|
'type',
|
|
|
|
'periodic',
|
|
|
|
'parameterized',
|
2020-06-09 21:03:28 +00:00
|
|
|
'parent.{periodic,parameterized}',
|
2018-01-25 17:29:47 +00:00
|
|
|
function() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const type = this.type;
|
2018-01-25 17:29:47 +00:00
|
|
|
|
2018-11-08 05:04:27 +00:00
|
|
|
if (this.get('parent.periodic')) {
|
2018-01-25 17:29:47 +00:00
|
|
|
return 'periodic-child';
|
|
|
|
} else if (this.get('parent.parameterized')) {
|
|
|
|
return 'parameterized-child';
|
2019-03-26 07:46:44 +00:00
|
|
|
} else if (this.periodic) {
|
2018-11-08 05:04:27 +00:00
|
|
|
return 'periodic';
|
2019-03-26 07:46:44 +00:00
|
|
|
} else if (this.parameterized) {
|
2018-11-08 05:04:27 +00:00
|
|
|
return 'parameterized';
|
2018-01-25 17:29:47 +00:00
|
|
|
} else if (JOB_TYPES.includes(type)) {
|
|
|
|
// Guard against the API introducing a new type before the UI
|
|
|
|
// is prepared to handle it.
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.type;
|
2018-01-25 17:29:47 +00:00
|
|
|
}
|
2018-01-26 23:01:10 +00:00
|
|
|
|
|
|
|
// A fail-safe in the event the API introduces a new type.
|
|
|
|
return 'service';
|
2018-01-25 17:29:47 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
datacenters: attr(),
|
|
|
|
taskGroups: fragmentArray('task-group', { defaultValue: () => [] }),
|
2018-02-08 23:02:48 +00:00
|
|
|
summary: belongsTo('job-summary'),
|
|
|
|
|
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.
|
|
|
|
isPartial: equal('taskGroups.length', 0),
|
|
|
|
|
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.
|
|
|
|
taskGroupCount: or('taskGroups.length', 'taskGroupSummaries.length'),
|
|
|
|
|
2018-02-08 23:02:48 +00:00
|
|
|
// Alias through to the summary, as if there was no relationship
|
|
|
|
taskGroupSummaries: alias('summary.taskGroupSummaries'),
|
|
|
|
queuedAllocs: alias('summary.queuedAllocs'),
|
|
|
|
startingAllocs: alias('summary.startingAllocs'),
|
|
|
|
runningAllocs: alias('summary.runningAllocs'),
|
|
|
|
completeAllocs: alias('summary.completeAllocs'),
|
|
|
|
failedAllocs: alias('summary.failedAllocs'),
|
|
|
|
lostAllocs: alias('summary.lostAllocs'),
|
|
|
|
totalAllocs: alias('summary.totalAllocs'),
|
|
|
|
pendingChildren: alias('summary.pendingChildren'),
|
|
|
|
runningChildren: alias('summary.runningChildren'),
|
|
|
|
deadChildren: alias('summary.deadChildren'),
|
2018-03-20 19:23:12 +00:00
|
|
|
totalChildren: alias('summary.totalChildren'),
|
2018-01-25 17:29:47 +00:00
|
|
|
|
|
|
|
version: attr('number'),
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
versions: hasMany('job-versions'),
|
|
|
|
allocations: hasMany('allocations'),
|
|
|
|
deployments: hasMany('deployments'),
|
2017-11-29 01:23:30 +00:00
|
|
|
evaluations: hasMany('evaluations'),
|
2017-10-10 16:36:15 +00:00
|
|
|
namespace: belongsTo('namespace'),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2018-05-10 00:04:45 +00:00
|
|
|
drivers: computed('taskGroups.@each.drivers', function() {
|
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-09 21:03:28 +00:00
|
|
|
allocationsUnhealthyDrivers: mapBy('allocations', 'unhealthyDrivers'),
|
|
|
|
|
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.
|
2020-06-09 21:03:28 +00:00
|
|
|
unhealthyDrivers: computed('allocationsUnhealthyDrivers.[]', function() {
|
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();
|
|
|
|
}),
|
|
|
|
|
2018-03-08 22:14:55 +00:00
|
|
|
hasBlockedEvaluation: computed('evaluations.@each.isBlocked', function() {
|
2019-10-08 18:44:19 +00:00
|
|
|
return this.evaluations.toArray().some(evaluation => evaluation.get('isBlocked'));
|
2018-03-08 22:14:55 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
hasPlacementFailures: and('latestFailureEvaluation', 'hasBlockedEvaluation'),
|
2017-11-29 01:23:30 +00:00
|
|
|
|
2020-06-09 21:03:28 +00:00
|
|
|
latestEvaluation: computed('evaluations.{@each.modifyIndex,isPending}', function() {
|
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-09 21:03:28 +00:00
|
|
|
latestFailureEvaluation: computed('evaluations.{@each.modifyIndex,isPending}', function() {
|
|
|
|
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
|
|
|
|
|
|
|
return;
|
|
|
|
}),
|
2017-11-29 01:23:30 +00:00
|
|
|
|
2017-12-15 21:39:18 +00:00
|
|
|
supportsDeployments: equal('type', 'service'),
|
2017-10-24 19:35:29 +00:00
|
|
|
|
2018-07-28 02:28:05 +00:00
|
|
|
latestDeployment: belongsTo('deployment', { inverse: 'jobForLatest' }),
|
2018-07-28 00:40:09 +00:00
|
|
|
|
2018-07-28 02:28:05 +00:00
|
|
|
runningDeployment: computed('latestDeployment', 'latestDeployment.isRunning', function() {
|
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-09 21:03:28 +00:00
|
|
|
return;
|
2017-09-19 14:47:10 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
fetchRawDefinition() {
|
|
|
|
return this.store.adapterFor('job').fetchRawDefinition(this);
|
|
|
|
},
|
|
|
|
|
2018-01-26 22:32:11 +00:00
|
|
|
forcePeriodic() {
|
|
|
|
return this.store.adapterFor('job').forcePeriodic(this);
|
|
|
|
},
|
|
|
|
|
2018-04-18 19:17:55 +00:00
|
|
|
stop() {
|
|
|
|
return this.store.adapterFor('job').stop(this);
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
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)
|
2018-08-15 00:29:51 +00:00
|
|
|
.then(response => {
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
}
|
2018-08-15 00:29:51 +00:00
|
|
|
},
|
|
|
|
|
2018-08-21 23:44:31 +00:00
|
|
|
resetId() {
|
2019-03-26 07:46:44 +00:00
|
|
|
this.set('id', JSON.stringify([this.plainId, this.get('namespace.name') || 'default']));
|
2018-08-21 23:44:31 +00:00
|
|
|
},
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
statusClass: computed('status', function() {
|
|
|
|
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';
|
2017-09-19 14:47:10 +00:00
|
|
|
}),
|
2018-01-26 00:50:31 +00:00
|
|
|
|
|
|
|
payload: attr('string'),
|
|
|
|
decodedPayload: computed('payload', function() {
|
|
|
|
// Lazily decode the base64 encoded payload
|
2019-03-26 07:46:44 +00:00
|
|
|
return window.atob(this.payload || '');
|
2018-01-26 00:50:31 +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.
|
|
|
|
_newDefinition: attr('string'),
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
_newDefinitionJSON: attr('string'),
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|