Add jobs service with script execution, configuration support, and control interface.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1f6d41ab06
commit
0ad5b5f44f
27 files changed
+1108
-45
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import { request, type ControlRequest } from './control.ts';
|
||||
import type { JobState, RunRecord } from './service.ts';
|
||||
|
||||
const usage = 'usage: jobctl list | run <id> | cancel <id>';
|
||||
|
||||
function ago(time: number): string
|
||||
{
|
||||
const seconds = Math.round((Date.now() - time) / 1000);
|
||||
if (seconds < 120) return `${seconds}s ago`;
|
||||
if (seconds < 7200) return `${Math.round(seconds / 60)}m ago`;
|
||||
return `${Math.round(seconds / 3600)}h ago`;
|
||||
}
|
||||
|
||||
function describeRun(run: RunRecord | undefined): string
|
||||
{
|
||||
if (!run) return '';
|
||||
if (run.outcome === 'running') return `since ${ago(run.startedAt)}`;
|
||||
|
||||
const took = `${((run.finishedAt! - run.startedAt) / 1000).toFixed(1)}s`;
|
||||
return `${run.outcome} ${ago(run.finishedAt!)}, took ${took}${run.error ? `: ${run.error}` : ''}`;
|
||||
}
|
||||
|
||||
function state(job: JobState): string
|
||||
{
|
||||
if (!job.enabled) return 'disabled';
|
||||
if (job.kind === 'builtin') return 'backend';
|
||||
return job.running ? 'running' : 'idle';
|
||||
}
|
||||
|
||||
function table(jobs: JobState[]): string
|
||||
{
|
||||
const rows = [['ID', 'KIND', 'STATE', 'SCHEDULE', 'LAST RUN'], ...jobs.map((job) => [job.id, job.kind, state(job), job.schedule, describeRun(job.last)])];
|
||||
const widths = rows[0]!.map((_, column) => Math.max(...rows.map((row) => row[column]!.length)));
|
||||
return rows.map((row) => row.map((cell, column) => cell.padEnd(widths[column]!)).join(' ').trimEnd()).join('\n');
|
||||
}
|
||||
|
||||
const socket = process.env.CONTROL_SOCKET ?? '/run/jobs/control.sock';
|
||||
const [command, id] = process.argv.slice(2);
|
||||
|
||||
let body: ControlRequest;
|
||||
if (command === 'list') body = { op: 'list' };
|
||||
else if ((command === 'run' || command === 'cancel') && id) body = { op: command, id };
|
||||
else
|
||||
{
|
||||
console.error(usage);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const response = await request(socket, body).catch((error: Error) =>
|
||||
{
|
||||
console.error(`cannot reach the jobs service at ${socket}: ${error.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
if (!response.ok)
|
||||
{
|
||||
console.error(response.error);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(response.jobs ? table(response.jobs) : response.message);
|
||||
Reference in new issue
Block a user