mirror of
https://github.com/kemko/nomad.git
synced 2026-01-03 00:45:43 +03:00
this is the CE side of an Enterprise-only feature.
a job trying to use this in CE will fail to validate.
to enable daily-scheduled execution entirely client-side,
a job may now contain:
task "name" {
schedule {
cron {
start = "0 12 * * * *" # may not include "," or "/"
end = "0 16" # partial cron, with only {minute} {hour}
timezone = "EST" # anything in your tzdb
}
}
...
and everything about the allocation will be placed as usual,
but if outside the specified schedule, the taskrunner will block
on the client, waiting on the schedule start, before proceeding
with the task driver execution, etc.
this includes a taksrunner hook, which watches for the end of
the schedule, at which point it will kill the task.
then, restarts-allowing, a new task will start and again block
waiting for start, and so on.
this also includes all the plumbing required to pipe API calls
through from command->api->agent->server->client, so that
tasks can be force-run, force-paused, or resume the schedule
on demand.
103 lines
2.9 KiB
JavaScript
103 lines
2.9 KiB
JavaScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
import Watchable from './watchable';
|
|
import addToPath from 'nomad-ui/utils/add-to-path';
|
|
import classic from 'ember-classic-decorator';
|
|
|
|
@classic
|
|
export default class AllocationAdapter extends Watchable {
|
|
stop = adapterAction('/stop');
|
|
|
|
restart(allocation, taskName) {
|
|
const prefix = `${this.host || '/'}${this.urlPrefix()}`;
|
|
const url = `${prefix}/client/allocation/${allocation.id}/restart`;
|
|
return this.ajax(url, 'PUT', {
|
|
data: taskName && { TaskName: taskName },
|
|
});
|
|
}
|
|
|
|
restartAll(allocation) {
|
|
const prefix = `${this.host || '/'}${this.urlPrefix()}`;
|
|
const url = `${prefix}/client/allocation/${allocation.id}/restart`;
|
|
return this.ajax(url, 'PUT', { data: { AllTasks: true } });
|
|
}
|
|
|
|
ls(model, path) {
|
|
return this.token
|
|
.authorizedRequest(
|
|
`/v1/client/fs/ls/${model.id}?path=${encodeURIComponent(path)}`
|
|
)
|
|
.then(handleFSResponse);
|
|
}
|
|
|
|
stat(model, path) {
|
|
return this.token
|
|
.authorizedRequest(
|
|
`/v1/client/fs/stat/${model.id}?path=${encodeURIComponent(path)}`
|
|
)
|
|
.then(handleFSResponse);
|
|
}
|
|
|
|
// note: TaskName vs Task as key for PUT data differs from restart above
|
|
forcePause(allocation, taskName) {
|
|
const prefix = `${this.host || '/'}${this.urlPrefix()}`;
|
|
const url = `${prefix}/client/allocation/${allocation.id}/pause`;
|
|
return this.ajax(url, 'PUT', {
|
|
data: taskName && { Task: taskName, ScheduleState: 'pause' },
|
|
});
|
|
}
|
|
forceRun(allocation, taskName) {
|
|
const prefix = `${this.host || '/'}${this.urlPrefix()}`;
|
|
const url = `${prefix}/client/allocation/${allocation.id}/pause`;
|
|
return this.ajax(url, 'PUT', {
|
|
data: taskName && { Task: taskName, ScheduleState: 'run' },
|
|
});
|
|
}
|
|
reEnableSchedule(allocation, taskName) {
|
|
const prefix = `${this.host || '/'}${this.urlPrefix()}`;
|
|
const url = `${prefix}/client/allocation/${allocation.id}/pause`;
|
|
return this.ajax(url, 'PUT', {
|
|
data: taskName && { Task: taskName, ScheduleState: 'scheduled' },
|
|
});
|
|
}
|
|
|
|
async check(model) {
|
|
const res = await this.token.authorizedRequest(
|
|
`/v1/client/allocation/${model.id}/checks`
|
|
);
|
|
const data = await res.json();
|
|
// Append allocation ID to each check
|
|
Object.values(data).forEach((check) => {
|
|
check.Alloc = model.id;
|
|
check.Timestamp = check.Timestamp * 1000; // Convert to milliseconds
|
|
});
|
|
return data;
|
|
}
|
|
}
|
|
|
|
async function handleFSResponse(response) {
|
|
if (response.ok) {
|
|
return response.json();
|
|
} else {
|
|
const body = await response.text();
|
|
|
|
throw {
|
|
code: response.status,
|
|
toString: () => body,
|
|
};
|
|
}
|
|
}
|
|
|
|
function adapterAction(path, verb = 'POST') {
|
|
return function (allocation) {
|
|
const url = addToPath(
|
|
this.urlForFindRecord(allocation.id, 'allocation'),
|
|
path
|
|
);
|
|
return this.ajax(url, verb);
|
|
};
|
|
}
|