Scheduler: edit, Run now, cron with a preview, and a schedule's runs (#19)
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>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-23 19:17:48 -05:00
1 parent 777ab728c0
commit 966c2be5cc
10 files changed
+453 -97

No files matched your search

+55
View File
@@ -95,3 +95,58 @@ test('a schedule whose worker is offline records a failed run (#9)', async () =>
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);
});
+42
View File
@@ -95,3 +95,45 @@ test('empty states say what to do next (#33)', async ({ page }) => {
await page.goto('/workflows');
await expect(page.getByRole('link', { name: 'Step types and examples' })).toHaveAttribute('href', /#workflows$/);
});
test('a schedule can be created with cron, edited, run now, and its runs found (#19)', async ({ page, problems }) => {
const name = `UI sched ${Date.now()}`;
await page.goto('/scheduler');
await page.locator('[data-action="sc:create-open"]').first().click(); // header button or empty state
const modal = page.locator('#sc-create-modal');
await modal.locator('#sc-name').fill(name);
await modal.locator('#sc-command').fill('echo first');
await modal.locator('input[name="sc-worker-cb"]').first().check();
await modal.locator('#sc-type').selectOption('cron');
await modal.locator('#sc-value').fill('0 9 * * 1-5');
// The preview lists the next three runs and the server's time zone.
await expect(modal.locator('#sc-preview li')).toHaveCount(3);
await expect(modal.locator('#sc-preview')).toContainText("server's time zone");
// A bad expression says so, without leaving the dialog.
await modal.locator('#sc-value').fill('0 99 * * *');
await expect(modal.locator('#sc-preview')).toContainText('Invalid schedule');
expect(problems.every(p => /400/.test(p))).toBe(true); // that preview's 400 is expected
problems.length = 0;
await modal.locator('#sc-value').fill('0 9 * * 1-5');
await modal.getByRole('button', { name: 'Create Schedule' }).click();
const row = page.locator('#sc-wrap tr', { hasText: name });
await expect(row).toContainText('Cron: 0 9 * * 1-5');
// Edit in place.
await row.getByRole('button', { name: /Edit/ }).click();
await expect(modal.locator('#sc-create-title')).toHaveText('Edit Scheduled Command');
await expect(modal.locator('#sc-value')).toHaveValue('0 9 * * 1-5');
await modal.locator('#sc-command').fill('echo edited-run');
await modal.getByRole('button', { name: 'Save Changes' }).click();
await expect(row).toContainText('echo edited-run');
// Run now, then follow Runs to the Automated tab, filtered to this schedule.
await row.getByRole('button', { name: /Run now/ }).click();
await page.waitForTimeout(1500);
await row.getByRole('link', { name: 'Runs' }).click();
await expect(page).toHaveURL(/view=automated/);
await expect(page.locator('#ex-search')).toHaveValue(`scheduler:${name}`);
const runs = page.locator('#ex-list-automated .ex-row');
await expect(runs).toHaveCount(1);
await expect(runs.first()).toContainText('echo edited-run');
});
+30
View File
@@ -268,3 +268,33 @@ describe('definitionErrors (#11)', () => {
]);
});
});
describe('scheduleInput and nextRuns (#19)', () => {
const { scheduleInput, nextRuns } = require('../lib/utils');
const base = { name: 'n', command: 'true', worker_ids: ['w1'] };
test('accepts each schedule type and works out the next run', () => {
for (const [t, v] of [['interval', '15'], ['hourly', '2'], ['daily', '02:30'], ['cron', '*/5 * * * *']]) {
const r = scheduleInput({ ...base, schedule_type: t, schedule_value: v });
expect(r.error).toBeUndefined();
expect(r.value.next_run.getTime()).toBeGreaterThan(Date.now());
}
});
test('says what is wrong', () => {
const err = b => scheduleInput({ ...base, ...b }).error;
expect(err({ name: ' ' })).toBe('A name is required');
expect(err({ worker_ids: [] })).toBe('Pick at least one worker');
expect(err({ schedule_type: 'weekly', schedule_value: '1' })).toMatch(/^schedule_type must be one of/);
expect(err({ schedule_type: 'interval', schedule_value: '0' })).toBe('The interval must be a whole number above 0');
expect(err({ schedule_type: 'daily', schedule_value: '7pm' })).toBe('The time must be HH:MM (24-hour)');
expect(err({ schedule_type: 'cron', schedule_value: '99 * * * *' })).toMatch(/^Invalid schedule:/);
});
test('nextRuns steps forward from each run', () => {
const from = new Date(2026, 10, 23, 12, 0, 0);
expect(nextRuns('interval', '30', 3, from).map(d => d.getTime() - from.getTime())).toEqual([30, 60, 90].map(m => m * 60000));
const daily = nextRuns('daily', '02:00', 2, from);
expect(daily.map(d => [d.getDate(), d.getHours()])).toEqual([[24, 2], [25, 2]]);
});
});