403 lines
15 KiB
Plaintext
Executable File
403 lines
15 KiB
Plaintext
Executable File
<%- include('partials/header') %>
|
|
|
|
<style>
|
|
/* Dark theme PM2 CSS (integrates with your style.css) */
|
|
.pm2-row { cursor: pointer; border: 1px solid #4A4A4A; }
|
|
.pm2-row.active, .pm2-row.selected-outline {
|
|
background-color: #555 !important;
|
|
border-color: #8A190B !important;
|
|
box-shadow: 0 0 0 2px rgba(138,25,11,0.4);
|
|
}
|
|
|
|
#logs {
|
|
height: 60vh;
|
|
background: #0b1220;
|
|
color: #e6eef8;
|
|
padding: 10px;
|
|
font-family: monospace;
|
|
font-size: 0.85rem;
|
|
overflow-y: auto;
|
|
border-radius: 8px;
|
|
border: 1px solid #444;
|
|
|
|
white-space: pre-wrap;
|
|
word-break: break-word;
|
|
overflow-wrap: break-word;
|
|
}
|
|
|
|
.kv-label { width: 110px; display: inline-block; color: #C0C0C0; }
|
|
.kv-value { font-weight: bold; color: #ffffff; }
|
|
.subtle { color: #BEBEBE; font-size: 0.85rem; }
|
|
|
|
.badge-small { font-size: .75rem; padding: .25rem .5rem; border-radius: .4rem; }
|
|
</style>
|
|
|
|
<div class="container py-3" style="max-width:1300px;">
|
|
|
|
<div class="row g-3">
|
|
|
|
<!-- LEFT COLUMN — PROCESS LIST & CONTROL CENTRE -->
|
|
<div class="col-md-5 d-flex">
|
|
<div class="card-dark w-100 d-flex flex-column">
|
|
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<div>
|
|
<h4 class="mb-0">Downloader Workers</h4>
|
|
<div class="subtle">Select a worker to manage it and view its logs.</div>
|
|
</div>
|
|
|
|
<div class="d-flex gap-2 align-items-center">
|
|
<button id="refresh-list" class="btn btn-sm btn-outline-light" aria-label="Refresh worker list">⟳</button>
|
|
|
|
<!-- NEW: ONE-CLICK CREATE -->
|
|
<button id="btn-create" class="btn btn-sm btn-success" aria-label="Create downloader worker">
|
|
<strong>+</strong>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- PROCESS LIST -->
|
|
<div id="process-list" class="list-group" style="max-height:40vh; overflow-y:auto;">
|
|
<% if (processes && processes.length) { %>
|
|
<% processes.forEach(p => {
|
|
let status = (p.status || '').toLowerCase();
|
|
let badge = 'bg-secondary';
|
|
if (status === 'online' || status === 'running') badge = 'bg-success';
|
|
else if (status.includes('stop') || status.includes('exit')) badge = 'bg-danger';
|
|
else if (status.includes('launch') || status.includes('start')) badge = 'bg-warning text-dark';
|
|
%>
|
|
<a href="#"
|
|
class="list-group-item pm2-row d-flex justify-content-between"
|
|
data-name="<%= p.name %>"
|
|
data-pid="<%= p.pid %>"
|
|
data-pmid="<%= p.pm_id %>"
|
|
data-status="<%= p.status %>"
|
|
>
|
|
<div>
|
|
<strong><%= p.name %></strong>
|
|
<span class="badge badge-small <%= badge %>"><%= p.status %></span>
|
|
<div class="subtle"><%= p.execPath %></div>
|
|
</div>
|
|
|
|
<div class="text-end">
|
|
<div>pid: <span><%= p.pid %></span></div>
|
|
<div class="subtle"><%= p.uptime %></div>
|
|
</div>
|
|
</a>
|
|
<% }) %>
|
|
<% } else { %>
|
|
<div class="alert alert-warning">No downloader workers found.</div>
|
|
<% } %>
|
|
</div>
|
|
|
|
<pre id="control-output" class="mt-3 p-2" role="status" aria-live="polite" style="background:#0b0b0b; max-height:110px; overflow:auto; border-radius:6px; white-space:pre-wrap;">Worker action results will appear here.</pre>
|
|
|
|
<!-- CONTROL CENTRE -->
|
|
<div id="control-centre" class="mt-3 p-3" style="display:none; background:#3A3A3A; border-radius:8px; border:1px solid #555;">
|
|
<h5 id="cc-name">Process Name</h5>
|
|
<div id="cc-sub" class="subtle mb-2">pm_id / pid / status</div>
|
|
|
|
<!-- Quick actions -->
|
|
<div class="d-flex flex-wrap gap-2 mb-3">
|
|
<button id="btn-start" class="btn btn-success btn-sm">Start</button>
|
|
<button id="btn-stop" class="btn btn-danger btn-sm">Stop</button>
|
|
<button id="btn-restart" class="btn btn-warning btn-sm">Restart</button>
|
|
<button id="btn-delete" class="btn btn-outline-light btn-sm">Delete</button>
|
|
</div>
|
|
|
|
<!-- Details -->
|
|
<div class="small">
|
|
<div><span class="kv-label">PM2 name</span> <span class="kv-value" id="detail-name">—</span></div>
|
|
<div><span class="kv-label">PM2 id</span> <span class="kv-value" id="detail-pmid">—</span></div>
|
|
<div><span class="kv-label">PID</span> <span class="kv-value" id="detail-pid">—</span></div>
|
|
<div><span class="kv-label">Status</span> <span class="kv-value" id="detail-status">—</span></div>
|
|
<div><span class="kv-label">Uptime</span> <span class="kv-value" id="detail-uptime">—</span></div>
|
|
<div><span class="kv-label">CPU</span> <span class="kv-value" id="detail-cpu">—</span></div>
|
|
<div><span class="kv-label">Memory</span> <span class="kv-value" id="detail-mem">—</span></div>
|
|
<div><span class="kv-label">Exec Path</span> <span class="kv-value" id="detail-exec">—</span></div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<!-- RIGHT COLUMN — LOGS -->
|
|
<div class="col-md-7 d-flex">
|
|
<div class="card-dark w-100 d-flex flex-column">
|
|
|
|
<div class="d-flex justify-content-between mb-2">
|
|
<h4>Logs</h4>
|
|
<div class="d-flex gap-2">
|
|
<button id="clear-logs" class="btn btn-outline-light btn-sm">Clear</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="logs">[no logs yet]</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const ws = new WebSocket((location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host);
|
|
const logsEl = document.getElementById('logs');
|
|
const actionOutput = document.getElementById('control-output');
|
|
const MAX_VISIBLE_LOG = 64 * 1024;
|
|
let selectedEl = null;
|
|
let selectedData = null;
|
|
let selectionGeneration = 0;
|
|
let logWorkerName = null;
|
|
let commandInFlight = false;
|
|
|
|
function appendLog(message) {
|
|
const following = logsEl.scrollHeight - logsEl.scrollTop - logsEl.clientHeight < 40;
|
|
const combined = logsEl.textContent + `[${new Date().toLocaleTimeString()}] ${String(message)}\n`;
|
|
logsEl.textContent = combined.length > MAX_VISIBLE_LOG
|
|
? '[earlier log output omitted]\n' + combined.slice(-MAX_VISIBLE_LOG)
|
|
: combined;
|
|
if (following) logsEl.scrollTop = logsEl.scrollHeight;
|
|
}
|
|
|
|
function sendLogRequest(payload, showUnavailable = true) {
|
|
if (ws.readyState !== WebSocket.OPEN) {
|
|
if (showUnavailable) appendLog('[log connection is not ready]');
|
|
return false;
|
|
}
|
|
try {
|
|
ws.send(JSON.stringify(payload));
|
|
return true;
|
|
} catch {
|
|
if (showUnavailable) appendLog('[log request could not be sent]');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function startSelectedLogs() {
|
|
if (!selectedData || logWorkerName === selectedData.name) return;
|
|
if (sendLogRequest({ type: 'logs', name: selectedData.name })) logWorkerName = selectedData.name;
|
|
}
|
|
|
|
ws.addEventListener('open', () => {
|
|
appendLog('[log connection ready]');
|
|
startSelectedLogs();
|
|
});
|
|
ws.addEventListener('close', () => {
|
|
logWorkerName = null;
|
|
appendLog('[log connection closed; reload this page to reconnect]');
|
|
});
|
|
ws.addEventListener('error', () => appendLog('[log connection unavailable]'));
|
|
ws.addEventListener('message', event => {
|
|
let payload;
|
|
try { payload = JSON.parse(event.data); } catch { return; }
|
|
if (!payload || typeof payload !== 'object') return;
|
|
if (payload.type === 'stdout' || payload.type === 'stderr') appendLog(payload.data || '');
|
|
else if (payload.type === 'info') appendLog('[info] ' + (payload.message || ''));
|
|
else if (payload.type === 'error') {
|
|
logWorkerName = null;
|
|
appendLog('[error] ' + (payload.error || 'The log stream failed.'));
|
|
} else if (payload.type === 'close') {
|
|
logWorkerName = null;
|
|
appendLog('[log stream closed]');
|
|
}
|
|
});
|
|
|
|
function clearSelection() {
|
|
selectionGeneration++;
|
|
document.querySelectorAll('.pm2-row').forEach(row => row.classList.remove('active', 'selected-outline'));
|
|
selectedEl = null;
|
|
selectedData = null;
|
|
logWorkerName = null;
|
|
document.getElementById('control-centre').style.display = 'none';
|
|
sendLogRequest({ type: 'stop-logs' }, false);
|
|
}
|
|
|
|
function controlData(process) {
|
|
return { ...process, pmid: process.pm_id ?? process.pmid ?? '—' };
|
|
}
|
|
|
|
function populateControlCentre(data) {
|
|
const values = {
|
|
'cc-name': data.name,
|
|
'cc-sub': `pm_id: ${data.pmid} • pid: ${data.pid} • status: ${data.status}`,
|
|
'detail-name': data.name,
|
|
'detail-pmid': data.pmid,
|
|
'detail-pid': data.pid,
|
|
'detail-status': data.status,
|
|
'detail-uptime': data.uptime,
|
|
'detail-cpu': data.cpu,
|
|
'detail-mem': data.memoryMB,
|
|
'detail-exec': data.execPath,
|
|
};
|
|
for (const [id, value] of Object.entries(values)) document.getElementById(id).textContent = value ?? '—';
|
|
}
|
|
|
|
async function readProcesses() {
|
|
const response = await fetch('/pm2/list-json', { cache: 'no-store' });
|
|
const result = await response.json();
|
|
if (!response.ok || !result.ok || !Array.isArray(result.processes)) throw new Error('The worker list is unavailable.');
|
|
return result.processes;
|
|
}
|
|
|
|
function bindRowEvents() {
|
|
document.querySelectorAll('.pm2-row').forEach(row => {
|
|
row.addEventListener('click', async event => {
|
|
event.preventDefault();
|
|
if (selectedEl === row) {
|
|
clearSelection();
|
|
return;
|
|
}
|
|
clearSelection();
|
|
const requestGeneration = selectionGeneration;
|
|
row.classList.add('active', 'selected-outline');
|
|
selectedEl = row;
|
|
selectedData = controlData({
|
|
name: row.dataset.name,
|
|
pmid: row.dataset.pmid,
|
|
pid: row.dataset.pid,
|
|
status: row.dataset.status,
|
|
});
|
|
populateControlCentre(selectedData);
|
|
document.getElementById('control-centre').style.display = 'block';
|
|
const name = selectedData.name;
|
|
try {
|
|
const full = (await readProcesses()).find(process => process.name === name);
|
|
if (requestGeneration !== selectionGeneration || selectedData?.name !== name) return;
|
|
if (!full) {
|
|
clearSelection();
|
|
actionOutput.textContent = 'The selected worker is no longer available.';
|
|
return;
|
|
}
|
|
selectedData = controlData(full);
|
|
populateControlCentre(selectedData);
|
|
} catch {
|
|
if (requestGeneration !== selectionGeneration || selectedData?.name !== name) return;
|
|
actionOutput.textContent = 'Could not refresh the selected worker details.';
|
|
}
|
|
if (requestGeneration === selectionGeneration && selectedData?.name === name) startSelectedLogs();
|
|
});
|
|
});
|
|
}
|
|
|
|
function element(tag, className, text) {
|
|
const node = document.createElement(tag);
|
|
if (className) node.className = className;
|
|
if (text !== undefined) node.textContent = String(text ?? '—');
|
|
return node;
|
|
}
|
|
|
|
function processRow(process) {
|
|
const row = element('a', 'list-group-item pm2-row d-flex justify-content-between');
|
|
row.href = '#';
|
|
row.dataset.name = String(process.name);
|
|
row.dataset.pid = String(process.pid);
|
|
row.dataset.pmid = String(process.pm_id);
|
|
row.dataset.status = String(process.status);
|
|
const status = String(process.status || '').toLowerCase();
|
|
let badge = 'bg-secondary';
|
|
if (status === 'online' || status === 'running') badge = 'bg-success';
|
|
else if (status.includes('stop') || status.includes('exit')) badge = 'bg-danger';
|
|
else if (status.includes('start') || status.includes('launch')) badge = 'bg-warning text-dark';
|
|
const description = element('div');
|
|
description.append(element('strong', '', process.name), ' ', element('span', 'badge badge-small ' + badge, process.status), element('div', 'subtle', process.execPath));
|
|
const detail = element('div', 'text-end');
|
|
detail.append(element('div', '', 'pid: ' + process.pid), element('div', 'subtle', process.uptime));
|
|
row.append(description, detail);
|
|
return row;
|
|
}
|
|
|
|
async function refreshList() {
|
|
try {
|
|
const processes = await readProcesses();
|
|
const list = document.getElementById('process-list');
|
|
const selectedName = selectedData?.name;
|
|
list.replaceChildren();
|
|
for (const process of processes) {
|
|
const row = processRow(process);
|
|
list.appendChild(row);
|
|
if (process.name === selectedName) {
|
|
selectedEl = row;
|
|
selectedData = controlData(process);
|
|
row.classList.add('active', 'selected-outline');
|
|
populateControlCentre(selectedData);
|
|
}
|
|
}
|
|
if (!processes.length) list.appendChild(element('div', 'alert alert-warning', 'No downloader workers found.'));
|
|
if (selectedName && !processes.some(process => process.name === selectedName)) clearSelection();
|
|
bindRowEvents();
|
|
return true;
|
|
} catch {
|
|
actionOutput.textContent = 'Could not refresh the worker list. Please try again.';
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function execCommand(payload) {
|
|
if (commandInFlight) return false;
|
|
commandInFlight = true;
|
|
actionOutput.textContent = 'Running worker command…';
|
|
try {
|
|
const response = await fetch('/pm2/command', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
const result = await response.json();
|
|
actionOutput.textContent = result.message || result.error || 'The worker command could not be completed.';
|
|
if (response.ok && result.ok) {
|
|
await refreshList();
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch {
|
|
actionOutput.textContent = 'Could not contact the worker supervisor. Please try again.';
|
|
return false;
|
|
} finally {
|
|
commandInFlight = false;
|
|
}
|
|
}
|
|
|
|
async function findNextWorkerName() {
|
|
const names = new Set((await readProcesses()).map(process => process.name));
|
|
let next = 1;
|
|
while (names.has('proxerdl-worker' + next)) next++;
|
|
return 'proxerdl-worker' + next;
|
|
}
|
|
|
|
document.getElementById('btn-create').addEventListener('click', async event => {
|
|
const button = event.currentTarget;
|
|
if (button.disabled || commandInFlight) return;
|
|
button.disabled = true;
|
|
actionOutput.textContent = 'Preparing a downloader worker…';
|
|
try {
|
|
const name = await findNextWorkerName();
|
|
if (await execCommand({ cmd: 'create', name })) {
|
|
const row = [...document.querySelectorAll('.pm2-row')].find(item => item.dataset.name === name);
|
|
if (row) row.click();
|
|
}
|
|
} catch {
|
|
actionOutput.textContent = 'Could not prepare the worker. Refresh the list and try again.';
|
|
} finally {
|
|
button.disabled = false;
|
|
}
|
|
});
|
|
|
|
for (const command of ['start', 'stop', 'restart', 'delete']) {
|
|
document.getElementById('btn-' + command).addEventListener('click', () => {
|
|
if (!selectedData) {
|
|
actionOutput.textContent = 'Select a downloader worker first.';
|
|
return;
|
|
}
|
|
execCommand({ cmd: command, name: selectedData.name });
|
|
});
|
|
}
|
|
document.getElementById('refresh-list').addEventListener('click', refreshList);
|
|
document.getElementById('clear-logs').addEventListener('click', () => { logsEl.textContent = ''; });
|
|
window.addEventListener('beforeunload', () => sendLogRequest({ type: 'stop-logs' }, false));
|
|
bindRowEvents();
|
|
</script>
|
|
|
|
<%- include('partials/footer') %>
|