open-nomad/ui/mirage/factories/job.js

260 lines
7.1 KiB
JavaScript
Raw Normal View History

2018-02-16 02:55:59 +00:00
import { assign } from '@ember/polyfills';
2019-09-26 18:47:07 +00:00
import { Factory, trait } from 'ember-cli-mirage';
import faker from 'nomad-ui/mirage/faker';
import { provide, pickOne } from '../utils';
2017-09-19 14:47:10 +00:00
import { DATACENTERS } from '../common';
const JOB_PREFIXES = provide(5, faker.hacker.abbreviation);
const JOB_TYPES = ['service', 'batch', 'system'];
const JOB_STATUSES = ['pending', 'running', 'dead'];
export default Factory.extend({
id: i =>
2019-09-26 18:47:07 +00:00
`${faker.helpers.randomize(
JOB_PREFIXES
)}-${faker.hacker.noun().dasherize()}-${i}`.toLowerCase(),
name() {
return this.id;
},
2017-09-19 14:47:10 +00:00
version: 1,
// When provided, the resourceSpec will inform how many task groups to create
// and how much of each resource that task group reserves.
//
// One task group, 256 MiB memory and 500 Mhz cpu
// resourceSpec: ['M: 256, C: 500']
//
// Two task groups
// resourceSpec: ['M: 256, C: 500', 'M: 1024, C: 1200']
resourceSpec: null,
groupsCount() {
return this.resourceSpec ? this.resourceSpec.length : faker.random.number({ min: 1, max: 2 });
},
2017-09-19 14:47:10 +00:00
region: () => 'global',
type: () => faker.helpers.randomize(JOB_TYPES),
2017-11-29 23:36:34 +00:00
priority: () => faker.random.number(100),
allAtOnce: faker.random.boolean,
status: () => faker.helpers.randomize(JOB_STATUSES),
2019-09-26 18:47:07 +00:00
datacenters: () =>
faker.helpers.shuffle(DATACENTERS).slice(0, faker.random.number({ min: 1, max: 4 })),
2017-09-19 14:47:10 +00:00
childrenCount: () => faker.random.number({ min: 1, max: 2 }),
periodic: trait({
type: 'batch',
periodic: true,
// periodic details object
// serializer update for bool vs details object
periodicDetails: () => ({
Enabled: true,
ProhibitOverlap: true,
Spec: '*/5 * * * * *',
SpecType: 'cron',
TimeZone: 'UTC',
}),
}),
parameterized: trait({
type: 'batch',
parameterized: true,
// parameterized details object
// serializer update for bool vs details object
parameterizedDetails: () => ({
MetaOptional: null,
MetaRequired: null,
Payload: faker.random.boolean() ? 'required' : null,
}),
}),
periodicChild: trait({
// Periodic children need a parent job,
// It is the Periodic job's responsibility to create
// periodicChild jobs and provide a parent job.
type: 'batch',
}),
parameterizedChild: trait({
// Parameterized children need a parent job,
// It is the Parameterized job's responsibility to create
// parameterizedChild jobs and provide a parent job.
type: 'batch',
parameterized: true,
dispatched: true,
payload: window.btoa(faker.lorem.sentence()),
}),
2017-09-19 14:47:10 +00:00
createIndex: i => i,
modifyIndex: () => faker.random.number({ min: 10, max: 2000 }),
// Directive used to control sub-resources
// When false, no allocations are made
createAllocations: true,
// When true, deployments for the job will never have a 'running' status
noActiveDeployment: false,
// When true, deployments for the job will always have a 'running' status
activeDeployment: false,
// When true, the job will have no versions or deployments (and in turn no latest deployment)
noDeployments: false,
2017-11-29 23:36:34 +00:00
// When true, an evaluation with a high modify index and placement failures is created
failedPlacements: false,
2017-11-30 00:29:32 +00:00
// When true, no evaluations have failed placements
noFailedPlacements: false,
// When true, all task groups get the noHostVolumes trait
noHostVolumes: false,
2018-05-04 20:08:30 +00:00
// When true, allocations for this job will fail and reschedule, randomly succeeding or not
withRescheduling: false,
// When true, task groups will have services
withGroupServices: false,
// When true, only task groups and allocations are made
shallow: false,
2017-09-19 14:47:10 +00:00
afterCreate(job, server) {
if (!job.namespaceId) {
const namespace = server.db.namespaces.length ? pickOne(server.db.namespaces).id : null;
job.update({
namespace,
namespaceId: namespace,
});
} else {
job.update({
namespace: job.namespaceId,
});
}
const groupProps = {
2018-02-16 02:55:59 +00:00
job,
createAllocations: job.createAllocations,
2018-05-04 20:08:30 +00:00
withRescheduling: job.withRescheduling,
withServices: job.withGroupServices,
shallow: job.shallow,
};
if (job.groupTaskCount) {
groupProps.count = job.groupTaskCount;
}
let groups;
if (job.noHostVolumes) {
groups = provide(job.groupsCount, (_, idx) =>
server.create('task-group', 'noHostVolumes', {
...groupProps,
resourceSpec: job.resourceSpec && job.resourceSpec.length && job.resourceSpec[idx],
})
);
} else {
groups = provide(job.groupsCount, (_, idx) =>
server.create('task-group', {
...groupProps,
resourceSpec: job.resourceSpec && job.resourceSpec.length && job.resourceSpec[idx],
})
);
}
2018-02-16 02:55:59 +00:00
job.update({
taskGroupIds: groups.mapBy('id'),
});
const hasChildren = job.periodic || (job.parameterized && !job.parentId);
const jobSummary = server.create('job-summary', hasChildren ? 'withChildren' : 'withSummary', {
jobId: job.id,
2017-09-19 14:47:10 +00:00
groupNames: groups.mapBy('name'),
2018-02-16 02:55:59 +00:00
namespace: job.namespace,
2017-09-19 14:47:10 +00:00
});
job.update({
jobSummaryId: jobSummary.id,
});
2020-07-25 04:36:43 +00:00
const jobScale = server.create('job-scale', {
groupNames: groups.mapBy('name'),
jobId: job.id,
namespace: job.namespace,
shallow: job.shallow,
});
job.update({
jobScaleId: jobScale.id,
});
if (!job.noDeployments) {
Array(faker.random.number({ min: 1, max: 3 }))
.fill(null)
.map((_, index) => {
return server.create('job-version', {
job,
namespace: job.namespace,
version: index,
noActiveDeployment: job.noActiveDeployment,
activeDeployment: job.activeDeployment,
});
2017-09-19 14:47:10 +00:00
});
}
2017-11-29 23:36:34 +00:00
if (!job.shallow) {
const knownEvaluationProperties = {
job,
namespace: job.namespace,
};
2018-02-16 02:55:59 +00:00
server.createList(
'evaluation',
faker.random.number({ min: 1, max: 5 }),
2018-02-16 02:55:59 +00:00
knownEvaluationProperties
);
if (!job.noFailedPlacements) {
server.createList(
'evaluation',
faker.random.number(3),
'withPlacementFailures',
knownEvaluationProperties
);
}
if (job.failedPlacements) {
server.create(
'evaluation',
'withPlacementFailures',
assign(knownEvaluationProperties, {
modifyIndex: 4000,
})
);
}
2017-11-29 23:36:34 +00:00
}
if (job.periodic) {
// Create periodicChild jobs
server.createList('job', job.childrenCount, 'periodicChild', {
parentId: job.id,
namespaceId: job.namespaceId,
namespace: job.namespace,
2018-01-31 22:01:13 +00:00
createAllocations: job.createAllocations,
shallow: job.shallow,
});
}
if (job.parameterized && !job.parentId) {
// Create parameterizedChild jobs
server.createList('job', job.childrenCount, 'parameterizedChild', {
parentId: job.id,
namespaceId: job.namespaceId,
namespace: job.namespace,
2018-01-31 22:01:13 +00:00
createAllocations: job.createAllocations,
shallow: job.shallow,
});
}
2017-09-19 14:47:10 +00:00
},
});