CI / Lint (eslint) (pull_request) Successful in 27s
CI / Test (jest) (pull_request) Successful in 26s
CI / Security (npm audit) (pull_request) Successful in 10s
CI / Scan (secrets, workflows, shell) (pull_request) Failing after 2m45s
CI / Integration (api) (pull_request) Successful in 3m37s
CI / UI (playwright) (pull_request) Successful in 2m51s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI screenshots (before/after) (pull_request) Successful in 2m35s
- Edit: PUT /api/scheduled-commands/:id changes a schedule in place (keeping its history) and works out its next run again. The create dialog doubles as the edit dialog. - Run now: POST /api/scheduled-commands/:id/run dispatches at once, records triggered_by, leaves next_run alone, and answers 409 while a previous run is still going. The dispatch body moved out of the scheduler loop into dispatchSchedule() so both share it. - Cron is offered in the dialog, and a preview shows the next three runs and the server time zone they are worked out in (GET /api/scheduled-commands/preview). Errors stay in the dialog. - Runs links to /executions?view=automated&search=scheduler:<name>; Executions now honours ?view= and ?search=. - Create and edit share scheduleInput(), which also checks daily HH:MM. - The dialog's worker list had no styles on this page (it borrows Quick Command's markup); they are now included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
7.4 KiB
JavaScript
153 lines
7.4 KiB
JavaScript
'use strict';
|
|
|
|
// The scheduler (processScheduledCommands in server.js). The test stack ticks every
|
|
// second (PULSE_SCHEDULER_TICK_MS); a schedule is made due at once by setting its
|
|
// next_run in the database, the one place these tests reach past the API.
|
|
const mysql = require('mysql2/promise');
|
|
const { api, sso, SSO, waitFor, waitForExecution, workerByName, commandResult, startWorker, sleep } = require('./helpers');
|
|
|
|
const unique = () => `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
|
|
let db;
|
|
|
|
beforeAll(async () => {
|
|
db = await mysql.createConnection({
|
|
host: process.env.DB_HOST || '127.0.0.1',
|
|
port: Number(process.env.DB_PORT || 3306),
|
|
user: process.env.DB_USER || 'pulse',
|
|
password: process.env.DB_PASSWORD || 'ci-only',
|
|
database: process.env.DB_NAME || 'pulse',
|
|
});
|
|
});
|
|
afterAll(() => db && db.end());
|
|
|
|
async function createSchedule(fields) {
|
|
const r = await sso('POST', '/api/scheduled-commands', {
|
|
name: `int ${unique()}`, schedule_type: 'interval', schedule_value: '60', ...fields,
|
|
});
|
|
return r;
|
|
}
|
|
const makeDue = id => db.query('UPDATE scheduled_commands SET next_run = NOW() - INTERVAL 1 SECOND WHERE id = ?', [id]);
|
|
async function executionsOf(name) {
|
|
const [rows] = await db.query('SELECT id, status FROM executions WHERE started_by = ? ORDER BY started_at', [`scheduler:${name}`]);
|
|
return rows;
|
|
}
|
|
|
|
test('a due schedule runs its command on its worker and moves next_run forward', async () => {
|
|
const worker = await workerByName('node-02');
|
|
const name = `int ${unique()}`;
|
|
const created = await createSchedule({ name, command: 'echo scheduled-ok', worker_ids: [worker.id] });
|
|
expect(created.status).toBe(200);
|
|
await makeDue(created.json.id);
|
|
|
|
const [run] = await waitFor(async () => { const r = await executionsOf(name); return r.length ? r : null; },
|
|
{ what: 'the scheduled execution' });
|
|
const ex = await waitForExecution(run.id);
|
|
expect(ex.status).toBe('completed');
|
|
expect(commandResult(ex).stdout.trim()).toBe('scheduled-ok');
|
|
|
|
const [[row]] = await db.query('SELECT next_run, last_run FROM scheduled_commands WHERE id = ?', [created.json.id]);
|
|
expect(row.last_run).not.toBeNull();
|
|
expect(new Date(row.next_run).getTime()).toBeGreaterThan(Date.now() + 50 * 60_000); // about 60 min ahead
|
|
});
|
|
|
|
test('a disabled schedule does not run', async () => {
|
|
const worker = await workerByName('node-02');
|
|
const name = `int ${unique()}`;
|
|
const created = await createSchedule({ name, command: 'echo nope', worker_ids: [worker.id] });
|
|
const toggled = await sso('PUT', `/api/scheduled-commands/${created.json.id}/toggle`);
|
|
expect(toggled.json.enabled).toBe(false);
|
|
await makeDue(created.json.id);
|
|
await sleep(3500); // several scheduler ticks
|
|
expect(await executionsOf(name)).toEqual([]);
|
|
});
|
|
|
|
test('only admins can create schedules, and bad input is refused', async () => {
|
|
const worker = await workerByName('node-01');
|
|
const asEmployee = await api('POST', '/api/scheduled-commands', {
|
|
headers: { ...SSO, 'Remote-Groups': 'employee' },
|
|
body: { name: 'x', command: 'true', worker_ids: [worker.id], schedule_type: 'interval', schedule_value: '5' },
|
|
});
|
|
expect(asEmployee.status).toBe(403);
|
|
expect((await createSchedule({ command: 'true' })).status).toBe(400); // no workers
|
|
expect((await createSchedule({ command: 'true', worker_ids: [worker.id], schedule_value: '-5' })).status).toBe(400);
|
|
});
|
|
|
|
test('an invalid cron expression is a 400, not a server error', async () => {
|
|
const worker = await workerByName('node-01');
|
|
const r = await createSchedule({ command: 'true', worker_ids: [worker.id], schedule_type: 'cron', schedule_value: 'not a cron' });
|
|
expect(r.status).toBe(400);
|
|
});
|
|
|
|
test('a schedule whose worker is offline records a failed run (#9)', async () => {
|
|
const workerName = `int-sched-${unique()}`;
|
|
const w = await startWorker(workerName);
|
|
const name = `int ${unique()}`;
|
|
try {
|
|
const created = await createSchedule({ name, command: 'echo unreachable', worker_ids: [w.id] });
|
|
w.child.kill('SIGKILL');
|
|
await waitFor(async () => (await workerByName(workerName))?.status === 'offline', { what: 'the worker to go offline' });
|
|
await makeDue(created.json.id);
|
|
await sleep(3500);
|
|
const runs = await executionsOf(name);
|
|
expect(runs).toHaveLength(1);
|
|
expect(runs[0].status).toBe('failed');
|
|
const ex = await waitForExecution(runs[0].id);
|
|
expect(commandResult(ex).error).toMatch(/offline/i);
|
|
} finally { await w.stop(); }
|
|
});
|
|
|
|
test('a schedule can be edited in place, and its next run is worked out again (#19)', async () => {
|
|
const worker = await workerByName('node-01');
|
|
const created = await createSchedule({ command: 'echo before', worker_ids: [worker.id] });
|
|
const id = created.json.id;
|
|
const r = await sso('PUT', `/api/scheduled-commands/${id}`, {
|
|
name: `int edited ${unique()}`, command: 'echo after', worker_ids: [worker.id], schedule_type: 'cron', schedule_value: '0 3 * * *',
|
|
});
|
|
expect(r.status).toBe(200);
|
|
const [[row]] = await db.query('SELECT command, schedule_type, schedule_value, next_run FROM scheduled_commands WHERE id = ?', [id]);
|
|
expect(row).toMatchObject({ command: 'echo after', schedule_type: 'cron', schedule_value: '0 3 * * *' });
|
|
const next = new Date(row.next_run);
|
|
expect(next.getHours()).toBe(3);
|
|
expect(next.getMinutes()).toBe(0);
|
|
|
|
const bad = await sso('PUT', `/api/scheduled-commands/${id}`, {
|
|
name: 'x', command: 'true', worker_ids: [worker.id], schedule_type: 'daily', schedule_value: '25:00',
|
|
});
|
|
expect(bad.status).toBe(400);
|
|
expect(bad.json.error).toBe('The time must be HH:MM (24-hour)');
|
|
expect((await sso('PUT', '/api/scheduled-commands/no-such-id', {
|
|
name: 'x', command: 'true', worker_ids: [worker.id], schedule_type: 'interval', schedule_value: '5',
|
|
})).status).toBe(404);
|
|
});
|
|
|
|
test('Run now runs the schedule at once, records who asked, and leaves its next run alone (#19)', async () => {
|
|
const worker = await workerByName('node-02');
|
|
const name = `int ${unique()}`;
|
|
const created = await createSchedule({ name, command: 'sleep 2; echo ran-now', worker_ids: [worker.id] });
|
|
const [[before]] = await db.query('SELECT next_run FROM scheduled_commands WHERE id = ?', [created.json.id]);
|
|
|
|
const r = await sso('POST', `/api/scheduled-commands/${created.json.id}/run`, {});
|
|
expect(r.status).toBe(200);
|
|
expect(r.json).toMatchObject({ sent: 1, offline: 0 });
|
|
// A second Run now while the first is still going is refused.
|
|
expect((await sso('POST', `/api/scheduled-commands/${created.json.id}/run`, {})).status).toBe(409);
|
|
|
|
const [run] = await executionsOf(name);
|
|
const ex = await waitForExecution(run.id);
|
|
expect(commandResult(ex).stdout.trim()).toBe('ran-now');
|
|
expect(ex.logs[0].triggered_by).toBeTruthy();
|
|
const [[after]] = await db.query('SELECT next_run, last_run FROM scheduled_commands WHERE id = ?', [created.json.id]);
|
|
expect(new Date(after.next_run).getTime()).toBe(new Date(before.next_run).getTime());
|
|
expect(after.last_run).not.toBeNull();
|
|
});
|
|
|
|
test('the preview gives the next three runs and the server time zone (#19)', async () => {
|
|
const r = await sso('GET', '/api/scheduled-commands/preview?type=cron&value=' + encodeURIComponent('30 4 * * *'));
|
|
expect(r.status).toBe(200);
|
|
expect(r.json.next).toHaveLength(3);
|
|
expect(r.json.timezone).toBeTruthy();
|
|
const days = r.json.next.map(t => new Date(t));
|
|
expect(days.every(d => d.getHours() === 4 && d.getMinutes() === 30)).toBe(true);
|
|
expect((await sso('GET', '/api/scheduled-commands/preview?type=cron&value=nope')).status).toBe(400);
|
|
});
|