Scheduler: Run button, single card-level Save, remove per-row Save
- Run button (blue) fires script immediately, opens log panel; stamps log with manual run timestamp before exec - Save moved to card footer — one button saves all cron expressions for the orchestrator and all its children at once - Toggle still auto-saves immediately on flip (enable/disable is instant) - Removed onblur from cron inputs — cron only saves on explicit Save - api/run.php: validates id, appends timestamp, execs script in background
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
require_once dirname(__DIR__) . '/include/scheduler.php';
|
||||
|
||||
$body = json_decode(file_get_contents('php://input'), true);
|
||||
$id = trim($body['id'] ?? '');
|
||||
|
||||
if (!$id || !preg_match('/^[a-zA-Z0-9_.\/\-]+\.sh$/', $id) || str_contains($id, '..')) {
|
||||
echo json_encode(['ok' => false, 'error' => 'Invalid id']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$script = SCRIPTS_DIR . '/' . $id;
|
||||
if (!file_exists($script)) {
|
||||
echo json_encode(['ok' => false, 'error' => 'Script not found: ' . $id]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$logFile = vv_job_log_path($id);
|
||||
$logDir = dirname($logFile);
|
||||
if (!is_dir($logDir)) mkdir($logDir, 0755, true);
|
||||
|
||||
// Stamp the log so the panel shows when the run started
|
||||
file_put_contents($logFile, date('[Y-m-d H:i:s]') . " Manual run started\n", FILE_APPEND);
|
||||
|
||||
exec('bash ' . escapeshellarg($script) . ' >> ' . escapeshellarg($logFile) . ' 2>&1 &');
|
||||
|
||||
echo json_encode(['ok' => true]);
|
||||
@@ -51,6 +51,17 @@
|
||||
.vv-btn-sm:hover { border-color: #888; color: #fff; }
|
||||
.vv-btn-sm.active { border-color: #4caf50; color: #4caf50; }
|
||||
|
||||
/* Card footer */
|
||||
.vv-card-footer { display: flex; align-items: center; gap: 10px;
|
||||
margin-top: 12px; padding-top: 10px; border-top: 1px solid #333; }
|
||||
.vv-save-btn { padding: 5px 18px; background: #4caf50; border: none; color: #fff;
|
||||
border-radius: 4px; cursor: pointer; font-size: 13px; }
|
||||
.vv-save-btn:hover { background: #388e3c; }
|
||||
.vv-save-status { font-size: 12px; color: #aaa; }
|
||||
.vv-run-btn { border-color: #2196f3 !important; color: #2196f3 !important; }
|
||||
.vv-run-btn:hover { background: #0d47a1 !important; color: #fff !important;
|
||||
border-color: #2196f3 !important; }
|
||||
|
||||
/* Log panel */
|
||||
.vv-log-panel { margin-top: 10px; border-top: 1px solid #333; padding-top: 8px; }
|
||||
.vv-log-toolbar { display: flex; justify-content: space-between; align-items: center;
|
||||
|
||||
@@ -4,16 +4,16 @@ $tree = vv_job_tree();
|
||||
?>
|
||||
|
||||
<div id="vv-scheduler">
|
||||
<p class="vv-hint">Orchestrators run their child scripts in the correct order.
|
||||
Disable an orchestrator and enable individual scripts below it to run them
|
||||
in isolation for debugging or testing.</p>
|
||||
<p class="vv-hint">Toggle enables/saves immediately. Edit cron expressions then hit
|
||||
<strong>Save</strong> at the bottom of the card. <strong>Run</strong> fires the script
|
||||
now and opens its log.</p>
|
||||
|
||||
<?php foreach ($tree as $orch): $oid = htmlspecialchars($orch['id']); ?>
|
||||
<div class="vv-card vv-wide vv-sched-card" data-id="<?= $oid ?>">
|
||||
|
||||
<!-- Orchestrator row -->
|
||||
<div class="vv-job-row">
|
||||
<label class="vv-toggle" title="Enable/disable">
|
||||
<label class="vv-toggle" title="Enable/disable (saves immediately)">
|
||||
<input type="checkbox" class="vv-enabled"
|
||||
<?= $orch['enabled'] ? 'checked' : '' ?>
|
||||
onchange="vvSaveJob(this)">
|
||||
@@ -21,8 +21,8 @@ $tree = vv_job_tree();
|
||||
</label>
|
||||
<span class="vv-job-label"><?= htmlspecialchars($orch['label']) ?></span>
|
||||
<input type="text" class="vv-cron" value="<?= htmlspecialchars($orch['cron']) ?>"
|
||||
placeholder="cron expression" onblur="vvSaveJob(this)">
|
||||
<button class="vv-btn-sm" onclick="vvSaveJob(this)">Save</button>
|
||||
placeholder="cron expression">
|
||||
<button class="vv-btn-sm vv-run-btn" onclick="vvRunJob(this)">▶ Run</button>
|
||||
<button class="vv-log-btn vv-btn-sm" onclick="vvToggleLog(this)">Log</button>
|
||||
<span class="vv-job-status"></span>
|
||||
<?php if (!empty($orch['children'])): ?>
|
||||
@@ -30,7 +30,7 @@ $tree = vv_job_tree();
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<!-- Log panel -->
|
||||
<!-- Orchestrator log panel -->
|
||||
<div class="vv-log-panel" style="display:none;">
|
||||
<div class="vv-log-toolbar">
|
||||
<span class="vv-log-ts"></span>
|
||||
@@ -39,13 +39,13 @@ $tree = vv_job_tree();
|
||||
<pre class="vv-log-pre">(no log yet)</pre>
|
||||
</div>
|
||||
|
||||
<!-- Children -->
|
||||
<!-- Children (Advanced) -->
|
||||
<?php if (!empty($orch['children'])): ?>
|
||||
<div class="vv-children" style="display:none;">
|
||||
<?php foreach ($orch['children'] as $child): $cid = htmlspecialchars($child['id']); ?>
|
||||
<div class="vv-script" data-id="<?= $cid ?>">
|
||||
<div class="vv-job-row">
|
||||
<label class="vv-toggle" title="Enable/disable">
|
||||
<label class="vv-toggle" title="Enable/disable (saves immediately)">
|
||||
<input type="checkbox" class="vv-enabled"
|
||||
<?= $child['enabled'] ? 'checked' : '' ?>
|
||||
onchange="vvSaveJob(this)">
|
||||
@@ -53,8 +53,8 @@ $tree = vv_job_tree();
|
||||
</label>
|
||||
<span class="vv-job-label"><?= htmlspecialchars($child['label']) ?></span>
|
||||
<input type="text" class="vv-cron" value="<?= htmlspecialchars($child['cron']) ?>"
|
||||
placeholder="cron expression" onblur="vvSaveJob(this)">
|
||||
<button class="vv-btn-sm" onclick="vvSaveJob(this)">Save</button>
|
||||
placeholder="cron expression">
|
||||
<button class="vv-btn-sm vv-run-btn" onclick="vvRunJob(this)">▶ Run</button>
|
||||
<button class="vv-log-btn vv-btn-sm" onclick="vvToggleLog(this)">Log</button>
|
||||
<span class="vv-job-status"></span>
|
||||
</div>
|
||||
@@ -70,6 +70,12 @@ $tree = vv_job_tree();
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<!-- Card footer: single Save for all crons in this card -->
|
||||
<div class="vv-card-footer">
|
||||
<button class="vv-save-btn" onclick="vvSaveCard(this)">Save Schedule</button>
|
||||
<span class="vv-save-status"></span>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
@@ -77,22 +83,67 @@ $tree = vv_job_tree();
|
||||
<script>
|
||||
const vvLogTimers = {};
|
||||
|
||||
// Toggle auto-saves its single job immediately
|
||||
function vvSaveJob(el) {
|
||||
const row = el.closest('.vv-job-row');
|
||||
const job = el.closest('[data-id]');
|
||||
const id = job.dataset.id;
|
||||
const job = el.closest('[data-id]');
|
||||
const id = job.dataset.id;
|
||||
const enabled = job.querySelector('.vv-enabled').checked;
|
||||
const cron = job.querySelector('.vv-cron').value.trim();
|
||||
const status = row.querySelector('.vv-job-status');
|
||||
|
||||
const status = job.querySelector('.vv-job-status');
|
||||
fetch('/plugins/varaverk/api/scheduler.php', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({id, enabled, cron})
|
||||
})
|
||||
.then(r => r.json())
|
||||
.then(d => { if (status) vvFlashStatus(status, d.ok ? '✓' : '✗ ' + (d.error ?? ''), d.ok); });
|
||||
}
|
||||
|
||||
// Save button at card bottom: saves all jobs (orch + children) in this card
|
||||
function vvSaveCard(btn) {
|
||||
const card = btn.closest('.vv-sched-card');
|
||||
const status = card.querySelector('.vv-save-status');
|
||||
const jobs = card.querySelectorAll('[data-id]');
|
||||
let pending = jobs.length;
|
||||
let allOk = true;
|
||||
|
||||
status.textContent = 'Saving…';
|
||||
jobs.forEach(job => {
|
||||
const id = job.dataset.id;
|
||||
const enabled = job.querySelector('.vv-enabled').checked;
|
||||
const cron = job.querySelector('.vv-cron').value.trim();
|
||||
fetch('/plugins/varaverk/api/scheduler.php', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({id, enabled, cron})
|
||||
})
|
||||
.then(r => r.json())
|
||||
.then(d => {
|
||||
if (!d.ok) allOk = false;
|
||||
if (--pending === 0) vvFlashStatus(status, allOk ? '✓ Saved' : '✗ Some failed', allOk);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Run: fires script immediately, opens log panel
|
||||
function vvRunJob(btn) {
|
||||
const job = btn.closest('[data-id]');
|
||||
const id = job.dataset.id;
|
||||
const logBtn = job.querySelector('.vv-log-btn');
|
||||
|
||||
if (logBtn && !logBtn.classList.contains('active')) vvToggleLog(logBtn);
|
||||
|
||||
fetch('/plugins/varaverk/api/run.php', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({id})
|
||||
})
|
||||
.then(r => r.json())
|
||||
.then(d => {
|
||||
if (status) vvFlashStatus(status, d.ok ? '✓' : '✗ ' + (d.error ?? ''), d.ok);
|
||||
if (!d.ok) {
|
||||
const pre = job.querySelector('.vv-log-pre');
|
||||
if (pre) pre.textContent = '✗ ' + (d.error ?? 'Failed to start');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -105,10 +156,10 @@ function vvToggleAdvanced(btn) {
|
||||
}
|
||||
|
||||
function vvToggleLog(btn) {
|
||||
const job = btn.closest('[data-id]');
|
||||
const id = job.dataset.id;
|
||||
const panel = job.querySelector('.vv-log-panel');
|
||||
const open = panel.style.display !== 'none';
|
||||
const job = btn.closest('[data-id]');
|
||||
const id = job.dataset.id;
|
||||
const panel = job.querySelector('.vv-log-panel');
|
||||
const open = panel.style.display !== 'none';
|
||||
|
||||
if (open) {
|
||||
panel.style.display = 'none';
|
||||
@@ -142,9 +193,9 @@ function vvFetchLog(panel, id) {
|
||||
function vvClearLog(btn) {
|
||||
const panel = btn.closest('.vv-log-panel');
|
||||
const job = btn.closest('[data-id]');
|
||||
const id = job.dataset.id;
|
||||
fetch('/plugins/varaverk/api/log.php?id=' + encodeURIComponent(id) + '&clear=1', {method:'POST'})
|
||||
.then(() => vvFetchLog(panel, id))
|
||||
fetch('/plugins/varaverk/api/log.php?id=' + encodeURIComponent(job.dataset.id) + '&clear=1',
|
||||
{method: 'POST'})
|
||||
.then(() => vvFetchLog(panel, job.dataset.id))
|
||||
.catch(() => {});
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user