migrated away from python
This commit is contained in:
1 parent
2dd68cb2b4
commit
bce6755a61
2 files changed
+99
-137
No files matched your search
@@ -0,0 +1,99 @@
|
||||
// proxer-queue-worker.js
|
||||
const { exec } = require("child_process");
|
||||
const { MongoClient } = require("mongodb");
|
||||
const path = require("path");
|
||||
require("dotenv").config({ path: path.join(__dirname, "..", ".env") });
|
||||
|
||||
// ========================
|
||||
// CONFIG
|
||||
// ========================
|
||||
const MONGO_URI = process.env.MONGO_URI;
|
||||
const DB_NAME = process.env.MONGO_DB || "downloads";
|
||||
const OUTPUT_DIR = process.env.OUTPUT_DIR || "/mnt/media/WIP/";
|
||||
|
||||
const client = new MongoClient(MONGO_URI);
|
||||
let queueCol, doneCol, failedCol;
|
||||
|
||||
// ========================
|
||||
// RUN A JOB
|
||||
// ========================
|
||||
async function runJob(job) {
|
||||
const url = job.url;
|
||||
const filename = job.filename;
|
||||
const token = job.token;
|
||||
|
||||
const cleanName = filename.replace(".mp4", "");
|
||||
|
||||
const scriptPath = path.join(__dirname, "proxer-dl-video.py");
|
||||
const venvPython = "/home/pm2/Proxer-Downloader/.venv/bin/python";
|
||||
|
||||
// Construct command string
|
||||
const command = `${venvPython} ${scriptPath} -u "${url}" -f "${cleanName}" -t "${token}" -o "${OUTPUT_DIR}"`;
|
||||
|
||||
console.log("Running:", command);
|
||||
|
||||
exec(command, async (error, stdout, stderr) => {
|
||||
const outputLog = `${stdout}\n${stderr}`.trim();
|
||||
|
||||
if (error) {
|
||||
console.error(`FAILED: ${cleanName}`);
|
||||
await failedCol.insertOne({
|
||||
filename,
|
||||
url,
|
||||
failed_at: new Date(),
|
||||
output_log: outputLog,
|
||||
return_code: error.code,
|
||||
});
|
||||
|
||||
// Remove from queue
|
||||
await queueCol.deleteOne({ _id: job._id });
|
||||
} else {
|
||||
console.log(`SUCCESS: ${cleanName}`);
|
||||
await doneCol.insertOne({
|
||||
filename,
|
||||
url,
|
||||
finished_at: new Date(),
|
||||
output_log: outputLog,
|
||||
result: "success",
|
||||
});
|
||||
|
||||
await queueCol.deleteOne({ _id: job._id });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ========================
|
||||
// MAIN LOOP
|
||||
// ========================
|
||||
async function main() {
|
||||
console.log("Worker started. Watching queue...");
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
const db = client.db(DB_NAME);
|
||||
|
||||
queueCol = db.collection("queue");
|
||||
doneCol = db.collection("done");
|
||||
failedCol = db.collection("failed");
|
||||
|
||||
// Polling loop
|
||||
while (true) {
|
||||
const job = await queueCol.findOneAndUpdate(
|
||||
{},
|
||||
{ $set: { processing: true } },
|
||||
{ sort: { created_at: 1 }, returnDocument: "after" }
|
||||
);
|
||||
|
||||
if (job.value) {
|
||||
await runJob(job.value);
|
||||
} else {
|
||||
// Wait 1 second before checking again
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Worker error:", err);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in new issue
Block a user