Files
Proxer-Downloader/views/pm2.ejs
T
2025-12-21 18:10:43 +01:00

449 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">PM2 Processes</h4>
<div class="subtle">Select a process to manage and view details.</div>
</div>
<div class="d-flex gap-2 align-items-center">
<button id="refresh-list" class="btn btn-sm btn-outline-light">⟳</button>
<!-- NEW: ONE-CLICK CREATE -->
<button id="btn-create" class="btn btn-sm btn-success">
<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 PM2 processes found.</div>
<% } %>
</div>
<!-- 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>
<button id="btn-save" class="btn btn-outline-light btn-sm">Save</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>
<pre id="control-output" class="mt-3 p-2" style="background:#0b0b0b; height:110px; overflow:auto; border-radius:6px;">Actions output will appear here.</pre>
</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>
/* ============================================================
WEBSOCKET LOG CONNECTION
============================================================ */
const ws = new WebSocket(
(location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host
);
const logsEl = document.getElementById("logs");
function appendLog(msg) {
const line = `[${new Date().toLocaleTimeString()}] ${msg}`;
logsEl.textContent += line + "\n";
logsEl.scrollTop = logsEl.scrollHeight; // auto-scroll always
}
ws.addEventListener("open", () => appendLog("[ws connected]"));
ws.addEventListener("close", () => appendLog("[ws closed]"));
ws.addEventListener("message", ev => {
let obj;
try { obj = JSON.parse(ev.data); } catch { appendLog(ev.data); return; }
if (obj.type === "stdout") appendLog(obj.data);
else if (obj.type === "stderr") appendLog(obj.data);
else if (obj.type === "info") appendLog("[info] " + obj.message);
else if (obj.type === "error") appendLog("[error] " + obj.error);
else if (obj.type === "close") appendLog("[log stream closed]");
});
/* ============================================================
PROCESS SELECTION & DETAILS
============================================================ */
let selectedEl = null;
let selectedData = null;
function clearSelection() {
document.querySelectorAll('.pm2-row')
.forEach(r => r.classList.remove('active', 'selected-outline'));
selectedEl = null;
selectedData = null;
document.getElementById("control-centre").style.display = "none";
}
function populateControlCentre(d) {
document.getElementById("cc-name").innerText = d.name;
document.getElementById("cc-sub").innerText =
`pm_id: ${d.pmid} • pid: ${d.pid} • status: ${d.status}`;
document.getElementById("detail-name").innerText = d.name;
document.getElementById("detail-pmid").innerText = d.pmid;
document.getElementById("detail-pid").innerText = d.pid;
document.getElementById("detail-status").innerText = d.status;
document.getElementById("detail-uptime").innerText = d.uptime;
document.getElementById("detail-cpu").innerText = d.cpu;
document.getElementById("detail-mem").innerText = d.memoryMB;
document.getElementById("detail-exec").innerText = d.execPath;
}
async function fetchFullProcess(name) {
try {
const res = await fetch('/pm2/list-json');
const json = await res.json();
if (!json.ok) return null;
return json.processes.find(p => p.name === name) || null;
} catch {
return null;
}
}
function bindRowEvents() {
document.querySelectorAll('.pm2-row').forEach(row => {
row.addEventListener('click', async ev => {
ev.preventDefault();
// toggle deselect
if (selectedEl === row) {
clearSelection();
return;
}
// highlight selected
document.querySelectorAll('.pm2-row')
.forEach(r => r.classList.remove('active', 'selected-outline'));
row.classList.add('active', 'selected-outline');
selectedEl = row;
// base data
selectedData = {
name: row.dataset.name,
pmid: row.dataset.pmid,
pid: row.dataset.pid,
status: row.dataset.status
};
// fetch detailed data
const full = await fetchFullProcess(selectedData.name);
if (full) {
selectedData = {
name: full.name,
pmid: full.pm_id,
pid: full.pid,
status: full.status,
uptime: full.uptime,
cpu: full.cpu,
memoryMB: full.memoryMB,
execPath: full.execPath
};
}
populateControlCentre(selectedData);
document.getElementById("control-centre").style.display = "block";
// AUTO-START LOGS
ws.send(JSON.stringify({
type: "logs",
name: selectedData.name
}));
appendLog("[auto logs for: " + selectedData.name + "]");
});
});
}
bindRowEvents();
/* ============================================================
REFRESH PROCESS LIST
============================================================ */
async function refreshList() {
try {
const res = await fetch('/pm2/list-json');
const json = await res.json();
if (!json.ok) return;
const list = document.getElementById("process-list");
list.innerHTML = '';
json.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('start') || status.includes('launch')) badge = 'bg-warning text-dark';
const item = document.createElement('a');
item.href = "#";
item.className = "list-group-item pm2-row d-flex justify-content-between";
item.dataset.name = p.name;
item.dataset.pid = p.pid;
item.dataset.pmid = p.pm_id;
item.dataset.status = p.status;
item.innerHTML = `
<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>
`;
list.appendChild(item);
});
bindRowEvents();
} catch (err) {
console.error(err);
}
}
document.getElementById("refresh-list").addEventListener("click", refreshList);
/* ============================================================
PM2 COMMAND EXECUTION
============================================================ */
async function execCommand(payload, outEl) {
outEl.textContent = "Running...";
try {
const res = await fetch('/pm2/command', {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
});
const json = await res.json();
outEl.textContent = JSON.stringify(json, null, 2);
if (json.ok) refreshList();
} catch (err) {
outEl.textContent = "Error: " + err.message;
}
}
/* ============================================================
ONE-CLICK CREATE WORKER
============================================================ */
async function findNextWorkerName() {
const res = await fetch('/pm2/list-json');
const json = await res.json();
if (!json.ok) return "proxerdl-worker1";
const names = json.processes
.map(p => p.name)
.filter(n => /^proxerdl-worker\d+$/.test(n));
const nums = names.map(n => parseInt(n.replace("proxerdl-worker", ""), 10));
if (nums.length === 0) return "proxerdl-worker1";
nums.sort((a, b) => a - b);
let next = 1;
for (let n of nums) {
if (n === next) next++;
else if (n > next) break;
}
return "proxerdl-worker" + next;
}
document.getElementById("btn-create").addEventListener("click", async () => {
const out = document.getElementById("control-output");
try {
const name = await findNextWorkerName();
const scriptPath = "/home/pm2/Proxer-Downloader/queue_worker.js";
out.textContent = "Creating " + name + "...";
const res = await fetch('/pm2/command', {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
cmd: "create",
name,
scriptPath
})
});
const json = await res.json();
out.textContent = JSON.stringify(json, null, 2);
if (json.ok) {
await refreshList();
const row = [...document.querySelectorAll(".pm2-row")]
.find(r => r.dataset.name === name);
if (row) row.click();
}
} catch (err) {
out.textContent = "Error: " + err.message;
}
});
/* ============================================================
QUICK ACTION BUTTONS
============================================================ */
document.getElementById("btn-start").addEventListener("click", () => {
if (!selectedData) return alert("Select a process.");
execCommand({ cmd: "start", name: selectedData.name }, document.getElementById("control-output"));
});
document.getElementById("btn-stop").addEventListener("click", () => {
if (!selectedData) return alert("Select a process.");
execCommand({ cmd: "stop", name: selectedData.name }, document.getElementById("control-output"));
});
document.getElementById("btn-restart").addEventListener("click", () => {
if (!selectedData) return alert("Select a process.");
execCommand({ cmd: "restart", name: selectedData.name }, document.getElementById("control-output"));
});
document.getElementById("btn-delete").addEventListener("click", () => {
if (!selectedData) return alert("Select a process.");
execCommand({ cmd: "delete", name: selectedData.name }, document.getElementById("control-output"));
});
document.getElementById("btn-save").addEventListener("click", () => {
execCommand({ cmd: "save" }, document.getElementById("control-output"));
});
/* ============================================================
LOG ACTIONS
============================================================ */
document.getElementById("clear-logs").addEventListener("click", () => {
logsEl.textContent = "";
});
</script>
<%- include('partials/footer') %>