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
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:
1 parent
777ab728c0
commit
966c2be5cc
10 files changed
+453
-97
No files matched your search
@@ -868,6 +868,14 @@
|
||||
async init() {
|
||||
const stored = Pulse.util.storage.get(VIEW_KEY, 'manual');
|
||||
state.view = (stored === 'automated') ? 'automated' : 'manual';
|
||||
// Deep link: /executions?view=automated&search=scheduler:<name> (#19, a schedule's runs).
|
||||
try {
|
||||
const q = new URLSearchParams(window.location.search);
|
||||
const v = q.get('view');
|
||||
if (v === 'manual' || v === 'automated') state.view = v;
|
||||
const term = q.get('search');
|
||||
if (term && $('ex-search')) $('ex-search').value = term;
|
||||
} catch (e) { /* ignore a malformed query */ }
|
||||
if (lt.tabs) lt.tabs.switch(PANEL[state.view]);
|
||||
|
||||
Pulse.events.on('tick', tickElapsed);
|
||||
|
||||
@@ -8,3 +8,37 @@
|
||||
color: var(--text-muted);
|
||||
font-size: 0.85em;
|
||||
}
|
||||
|
||||
/* Next-runs preview in the schedule dialog (#19). */
|
||||
.sc-preview {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-secondary);
|
||||
margin: -0.25rem 0 0.75rem;
|
||||
}
|
||||
.sc-preview:empty { display: none; }
|
||||
.sc-preview ul { margin: 0.2rem 0; padding-left: 1.1rem; }
|
||||
.sc-preview-title { color: var(--text-muted); letter-spacing: 0.05em; text-transform: uppercase; font-size: 0.65rem; }
|
||||
.sc-preview-tz { color: var(--text-muted); font-size: 0.7rem; }
|
||||
.sc-preview--bad { color: var(--accent-red); }
|
||||
|
||||
/* The schedule dialog's worker picker reuses Quick Command's markup, but quick.css
|
||||
is not loaded here, so the rows ran together on one line. */
|
||||
.qc-worker-list {
|
||||
background: var(--bg-primary);
|
||||
border: 1px solid var(--border-color);
|
||||
padding: 0.75rem;
|
||||
max-height: 220px;
|
||||
overflow-y: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.4rem;
|
||||
}
|
||||
.qc-worker-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.4rem 0.5rem;
|
||||
cursor: pointer;
|
||||
border: 1px solid transparent;
|
||||
}
|
||||
.qc-worker-row--online { background: rgba(0, 255, 136, 0.05); }
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
let _schedules = [];
|
||||
let _workers = [];
|
||||
let _editingId = null; // set while the dialog edits an existing schedule (#19)
|
||||
let _previewTimer = null;
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Helpers
|
||||
@@ -68,14 +70,18 @@
|
||||
const statusBadge = s.enabled
|
||||
? '<span class="lt-badge lt-badge-green">ENABLED</span>'
|
||||
: '<span class="lt-badge lt-badge-red">DISABLED</span>';
|
||||
const runsHref = '/executions?view=automated&search=' + encodeURIComponent('scheduler:' + (s.name || ''));
|
||||
const adminActions = Pulse.isAdmin
|
||||
? '<div class="lt-btn-group">' +
|
||||
'<button type="button" class="lt-btn lt-btn-primary lt-btn-sm" data-action="sc:run" data-id="' + esc(s.id) + '" data-name="' + esc(s.name || '') + '">▶ Run now</button>' +
|
||||
'<button type="button" class="lt-btn lt-btn-secondary lt-btn-sm" data-action="sc:edit" data-id="' + esc(s.id) + '">✎ Edit</button>' +
|
||||
'<button type="button" class="lt-btn lt-btn-secondary lt-btn-sm" data-action="sc:toggle" data-id="' + esc(s.id) + '" data-enabled="' + (s.enabled ? '1' : '0') + '">' +
|
||||
(s.enabled ? '⏸ Disable' : '▶ Enable') +
|
||||
'</button>' +
|
||||
'<button type="button" class="lt-btn lt-btn-danger lt-btn-sm" data-action="sc:delete" data-id="' + esc(s.id) + '" data-name="' + esc(s.name || '') + '">🗑 Delete</button>' +
|
||||
'<a class="lt-btn lt-btn-ghost lt-btn-sm" href="' + esc(runsHref) + '">Runs</a>' +
|
||||
'</div>'
|
||||
: '';
|
||||
: '<a class="lt-btn lt-btn-ghost lt-btn-sm" href="' + esc(runsHref) + '">Runs</a>';
|
||||
return (
|
||||
'<tr class="' + (s.enabled ? '' : 'sc-row-disabled') + '">' +
|
||||
'<td data-label="Name">' + esc(s.name || '') + '</td>' +
|
||||
@@ -161,12 +167,64 @@
|
||||
'<input type="number" id="sc-value" class="lt-input" placeholder="e.g. 2" min="1" max="24">';
|
||||
} else if (type === 'daily') {
|
||||
container.innerHTML =
|
||||
'<label class="lt-label" for="sc-value">Time (HH:MM)</label>' +
|
||||
'<label class="lt-label" for="sc-value">Time (HH:MM, server time)</label>' +
|
||||
'<input type="time" id="sc-value" class="lt-input">';
|
||||
} else if (type === 'cron') {
|
||||
container.innerHTML =
|
||||
'<label class="lt-label" for="sc-value">Cron expression</label>' +
|
||||
'<input type="text" id="sc-value" class="lt-input" placeholder="*/15 * * * *" autocomplete="off" spellcheck="false">' +
|
||||
'<div class="lt-form-hint">minute hour day-of-month month day-of-week, e.g. <code>0 2 * * 1-5</code> is 02:00 on weekdays</div>';
|
||||
}
|
||||
const valueEl = document.getElementById('sc-value');
|
||||
if (valueEl) valueEl.addEventListener('input', previewSoon);
|
||||
previewSoon();
|
||||
}
|
||||
|
||||
/* The next three runs, worked out by the server in its own time zone (#19). */
|
||||
function previewSoon() {
|
||||
clearTimeout(_previewTimer);
|
||||
_previewTimer = setTimeout(updatePreview, 300);
|
||||
}
|
||||
|
||||
async function updatePreview() {
|
||||
const el = document.getElementById('sc-preview');
|
||||
const typeEl = document.getElementById('sc-type');
|
||||
const valueEl = document.getElementById('sc-value');
|
||||
if (!el || !typeEl) return;
|
||||
const value = valueEl ? valueEl.value.trim() : '';
|
||||
if (!value) { el.textContent = ''; return; }
|
||||
try {
|
||||
const data = await Pulse.api.get('/api/scheduled-commands/preview?type=' + encodeURIComponent(typeEl.value) +
|
||||
'&value=' + encodeURIComponent(value));
|
||||
const runs = (data && data.next) || [];
|
||||
el.className = 'sc-preview';
|
||||
el.innerHTML = '<div class="sc-preview-title">Next runs</div><ul>' +
|
||||
runs.map(r => '<li>' + esc(fmt.dateTime(r)) + '</li>').join('') + '</ul>' +
|
||||
'<div class="sc-preview-tz">Worked out in the server\'s time zone: ' + esc((data && data.timezone) || 'unknown') + '</div>';
|
||||
} catch (e) {
|
||||
el.className = 'sc-preview sc-preview--bad';
|
||||
el.textContent = (e && e.message) || 'Invalid schedule';
|
||||
}
|
||||
}
|
||||
|
||||
function showFormError(message) {
|
||||
const el = document.getElementById('sc-create-error');
|
||||
if (!el) return;
|
||||
el.textContent = message || '';
|
||||
el.hidden = !message;
|
||||
}
|
||||
|
||||
function setDialogMode(editing) {
|
||||
const title = document.getElementById('sc-create-title');
|
||||
const submit = document.getElementById('sc-submit');
|
||||
if (title) title.textContent = editing ? 'Edit Scheduled Command' : 'Create Scheduled Command';
|
||||
if (submit) submit.textContent = editing ? 'Save Changes' : 'Create Schedule';
|
||||
}
|
||||
|
||||
function resetCreateForm() {
|
||||
_editingId = null;
|
||||
setDialogMode(false);
|
||||
showFormError('');
|
||||
const form = document.getElementById('sc-create-form');
|
||||
if (form) form.reset();
|
||||
const type = document.getElementById('sc-type');
|
||||
@@ -175,6 +233,37 @@
|
||||
renderCreateWorkerList();
|
||||
}
|
||||
|
||||
// Edit in place (#19): the same dialog, filled in, saving with PUT.
|
||||
function openEdit(id) {
|
||||
const s = _schedules.find(x => x.id === id);
|
||||
if (!s) return;
|
||||
resetCreateForm();
|
||||
_editingId = id;
|
||||
setDialogMode(true);
|
||||
document.getElementById('sc-name').value = s.name || '';
|
||||
document.getElementById('sc-command').value = s.command || '';
|
||||
document.getElementById('sc-type').value = s.schedule_type;
|
||||
updateValueInput();
|
||||
const valueEl = document.getElementById('sc-value');
|
||||
if (valueEl) valueEl.value = s.schedule_value || '';
|
||||
const ids = new Set(parseWorkerIds(s.worker_ids));
|
||||
document.querySelectorAll('input[name="sc-worker-cb"]').forEach(cb => { cb.checked = ids.has(cb.value); });
|
||||
previewSoon();
|
||||
if (window.lt && lt.modal) lt.modal.open('sc-create-modal');
|
||||
}
|
||||
|
||||
async function runNow(id, name) {
|
||||
try {
|
||||
const r = await Pulse.api.post('/api/scheduled-commands/' + encodeURIComponent(id) + '/run', {});
|
||||
const msg = 'Ran "' + name + '" on ' + r.sent + ' worker' + (r.sent === 1 ? '' : 's') +
|
||||
(r.offline ? ' (' + r.offline + ' offline)' : '');
|
||||
if (r.offline) Pulse.toast.warning(msg); else Pulse.toast.success(msg);
|
||||
await load();
|
||||
} catch (e) {
|
||||
Pulse.toast.error('Could not run "' + name + '": ' + (e.message || 'unknown error'));
|
||||
}
|
||||
}
|
||||
|
||||
async function submitCreate() {
|
||||
const name = document.getElementById('sc-name').value.trim();
|
||||
const command = document.getElementById('sc-command').value.trim();
|
||||
@@ -184,24 +273,22 @@
|
||||
const workerIds = Array.from(document.querySelectorAll('input[name="sc-worker-cb"]:checked')).map(cb => cb.value);
|
||||
|
||||
if (!name || !command || !value || workerIds.length === 0) {
|
||||
Pulse.toast.error('Please fill in all fields and select at least one worker');
|
||||
showFormError('Fill in every field and pick at least one worker');
|
||||
return;
|
||||
}
|
||||
|
||||
const body = { name: name, command: command, worker_ids: workerIds, schedule_type: type, schedule_value: value };
|
||||
const editing = _editingId;
|
||||
try {
|
||||
await Pulse.api.post('/api/scheduled-commands', {
|
||||
name: name,
|
||||
command: command,
|
||||
worker_ids: workerIds,
|
||||
schedule_type: type,
|
||||
schedule_value: value,
|
||||
});
|
||||
if (editing) await Pulse.api.put('/api/scheduled-commands/' + encodeURIComponent(editing), body);
|
||||
else await Pulse.api.post('/api/scheduled-commands', body);
|
||||
if (window.lt && lt.modal) lt.modal.close('sc-create-modal');
|
||||
Pulse.toast.success('Schedule created successfully');
|
||||
Pulse.toast.success(editing ? 'Schedule saved' : 'Schedule created');
|
||||
resetCreateForm();
|
||||
await load();
|
||||
} catch (e) {
|
||||
Pulse.toast.error('Failed to create schedule: ' + (e.message || 'unknown error'));
|
||||
// The server says what is wrong (e.g. a bad cron expression): keep the dialog open.
|
||||
showFormError((e && e.message) || 'Could not save the schedule');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,6 +362,8 @@
|
||||
'sc:create-submit': submitCreate,
|
||||
'sc:type': updateValueInput,
|
||||
'sc:toggle': (el) => toggleSchedule(el.getAttribute('data-id')),
|
||||
'sc:edit': (el) => openEdit(el.getAttribute('data-id')),
|
||||
'sc:run': (el) => runNow(el.getAttribute('data-id'), el.getAttribute('data-name')),
|
||||
'sc:delete': (el) => deleteSchedule(el.getAttribute('data-id'), el.getAttribute('data-name')),
|
||||
});
|
||||
Pulse.events.on('tick', onTick);
|
||||
|
||||
Reference in new issue
Block a user