Files
Varaverk/Plugin/usr/local/emhttp/plugins/varaverk/pages/scheduler.php
T
Gmer4Lfe d0b842a489 Add Dry Run button; fix CSRF token on all POST API calls
All fetch() POSTs now send application/x-www-form-urlencoded with the
page-injected csrf_token, satisfying unRAID's auto_prepend CSRF check.
All PHP API handlers switched from php://input JSON to $_POST.

Also adds Dry Run button (orange, between Run and Log) that sets
DRY_RUN=1 in the script environment before executing.
2026-05-23 17:50:47 -04:00

213 lines
8.0 KiB
PHP

<?php
require_once dirname(__DIR__) . '/include/scheduler.php';
$tree = vv_job_tree();
?>
<div id="vv-scheduler">
<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 (saves immediately)">
<input type="checkbox" class="vv-enabled"
<?= $orch['enabled'] ? 'checked' : '' ?>
onchange="vvSaveJob(this)">
<span class="vv-slider"></span>
</label>
<span class="vv-job-label"><?= htmlspecialchars($orch['label']) ?></span>
<input type="text" class="vv-cron" value="<?= htmlspecialchars($orch['cron']) ?>"
placeholder="cron expression">
<button class="vv-btn-sm vv-run-btn" onclick="vvRunJob(this)">&#9654; Run</button>
<button class="vv-btn-sm vv-dry-btn" onclick="vvDryRun(this)">&#9654; Dry 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'])): ?>
<button class="vv-advanced-toggle" onclick="vvToggleAdvanced(this)">Advanced ▸</button>
<?php endif; ?>
</div>
<!-- Orchestrator log panel -->
<div class="vv-log-panel" style="display:none;">
<div class="vv-log-toolbar">
<span class="vv-log-ts"></span>
<button class="vv-btn-sm" onclick="vvClearLog(this)">Clear</button>
</div>
<pre class="vv-log-pre">(no log yet)</pre>
</div>
<!-- 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 (saves immediately)">
<input type="checkbox" class="vv-enabled"
<?= $child['enabled'] ? 'checked' : '' ?>
onchange="vvSaveJob(this)">
<span class="vv-slider"></span>
</label>
<span class="vv-job-label"><?= htmlspecialchars($child['label']) ?></span>
<input type="text" class="vv-cron" value="<?= htmlspecialchars($child['cron']) ?>"
placeholder="cron expression">
<button class="vv-btn-sm vv-run-btn" onclick="vvRunJob(this)">&#9654; Run</button>
<button class="vv-btn-sm vv-dry-btn" onclick="vvDryRun(this)">&#9654; Dry Run</button>
<button class="vv-log-btn vv-btn-sm" onclick="vvToggleLog(this)">Log</button>
<span class="vv-job-status"></span>
</div>
<div class="vv-log-panel" style="display:none;">
<div class="vv-log-toolbar">
<span class="vv-log-ts"></span>
<button class="vv-btn-sm" onclick="vvClearLog(this)">Clear</button>
</div>
<pre class="vv-log-pre">(no log yet)</pre>
</div>
</div>
<?php endforeach; ?>
</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>
<script>
const vvLogTimers = {};
function vvPost(url, data) {
const params = new URLSearchParams({csrf_token, ...data});
return fetch(url, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: params
}).then(r => r.json());
}
// Toggle auto-saves its single job immediately
function vvSaveJob(el) {
const job = el.closest('[data-id]');
const id = job.dataset.id;
const enabled = job.querySelector('.vv-enabled').checked ? '1' : '0';
const cron = job.querySelector('.vv-cron').value.trim();
const status = job.querySelector('.vv-job-status');
vvPost('/plugins/varaverk/api/scheduler.php', {id, enabled, cron})
.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 ? '1' : '0';
const cron = job.querySelector('.vv-cron').value.trim();
vvPost('/plugins/varaverk/api/scheduler.php', {id, enabled, cron})
.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);
vvPost('/plugins/varaverk/api/run.php', {id})
.then(d => {
if (!d.ok) {
const pre = job.querySelector('.vv-log-pre');
if (pre) pre.textContent = '✗ ' + (d.error ?? 'Failed to start');
}
});
}
function vvDryRun(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);
vvPost('/plugins/varaverk/api/dryrun.php', {id})
.then(d => {
if (!d.ok) {
const pre = job.querySelector('.vv-log-pre');
if (pre) pre.textContent = '✗ ' + (d.error ?? 'Failed to start dry run');
}
});
}
function vvToggleAdvanced(btn) {
const card = btn.closest('.vv-sched-card');
const children = card.querySelector('.vv-children');
const visible = children.style.display !== 'none';
children.style.display = visible ? 'none' : 'block';
btn.textContent = visible ? 'Advanced ▸' : 'Advanced ▾';
}
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';
if (open) {
panel.style.display = 'none';
btn.textContent = 'Log';
btn.classList.remove('active');
clearInterval(vvLogTimers[id]);
delete vvLogTimers[id];
} else {
panel.style.display = 'block';
btn.textContent = 'Log ▾';
btn.classList.add('active');
vvFetchLog(panel, id);
vvLogTimers[id] = setInterval(() => vvFetchLog(panel, id), 3000);
}
}
function vvFetchLog(panel, id) {
fetch('/plugins/varaverk/api/log.php?id=' + encodeURIComponent(id))
.then(r => r.json())
.then(d => {
const pre = panel.querySelector('.vv-log-pre');
const ts = panel.querySelector('.vv-log-ts');
if (!d.ok) { pre.textContent = '✗ ' + (d.error ?? 'Error'); return; }
pre.textContent = d.content || '(no log yet)';
pre.scrollTop = pre.scrollHeight;
ts.textContent = d.ts ? 'Last run: ' + new Date(d.ts * 1000).toLocaleString() : '';
})
.catch(() => {});
}
function vvClearLog(btn) {
const panel = btn.closest('.vv-log-panel');
const job = btn.closest('[data-id]');
vvPost('/plugins/varaverk/api/log.php', {id: job.dataset.id, clear: '1'})
.then(() => vvFetchLog(panel, job.dataset.id))
.catch(() => {});
}
</script>