REVAMP
This commit is contained in:
1 parent
566a8e5905
commit
cb61ad08c7
12 files changed
+145
-374
No files matched your search
+60
-40
@@ -17,45 +17,52 @@ let queueCol, doneCol, failedCol;
|
||||
// ========================
|
||||
// RUN A JOB
|
||||
// ========================
|
||||
async function runJob(job) {
|
||||
const venvPython = "/home/pm2/Proxer-Downloader/.venv/bin/python";
|
||||
function runJob(job) {
|
||||
return new Promise((resolve) => {
|
||||
const venvPython = "F:/Nextcloud/DeSq-Gaming/Documents/Coding/Proxer-Downloader-v2/Proxer-Downloader/.venv/Scripts/python";
|
||||
|
||||
const cleanName = job.filename.replace(".mp4", "");
|
||||
const safeToken = token.replace(/^"+|"+$/g, ''); // strip extra quotes
|
||||
const command = `${venvPython} ${scriptPath} -u "${url}" -f "${cleanName}" -t "${safeToken}" -o "${OUTPUT_DIR}"`;
|
||||
const cleanName = job.filename.replace(".mp4", "");
|
||||
const safeToken = job.token.replace(/^"+|"+$/g, '');
|
||||
const scriptPath = 'scripts/proxer-dl-video.py'
|
||||
const command = `${venvPython} ${scriptPath} -u "${job.url}" -f "${cleanName}" -t "${safeToken}" -o "${OUTPUT_DIR}"`;
|
||||
|
||||
console.log("Running:", command);
|
||||
|
||||
console.log("Running:", command);
|
||||
exec(command, async (error, stdout, stderr) => {
|
||||
const outputLog = `${stdout}\n${stderr}`.trim();
|
||||
|
||||
exec(command, async (error, stdout, stderr) => {
|
||||
const outputLog = `${stdout}\n${stderr}`.trim();
|
||||
|
||||
if (error) {
|
||||
console.error(`FAILED: ${cleanName}`);
|
||||
await failedCol.insertOne({
|
||||
filename: job.filename,
|
||||
url: job.url,
|
||||
failed_at: new Date(),
|
||||
output_log: outputLog,
|
||||
return_code: error.code,
|
||||
});
|
||||
|
||||
await queueCol.deleteOne({ _id: job._id });
|
||||
} else {
|
||||
console.log(`SUCCESS: ${cleanName}`);
|
||||
await doneCol.insertOne({
|
||||
filename: job.filename,
|
||||
url: job.url,
|
||||
finished_at: new Date(),
|
||||
output_log: outputLog,
|
||||
result: "success",
|
||||
});
|
||||
|
||||
await queueCol.deleteOne({ _id: job._id });
|
||||
}
|
||||
try {
|
||||
if (error) {
|
||||
console.error(`FAILED: ${cleanName}`);
|
||||
await failedCol.insertOne({
|
||||
filename: job.filename,
|
||||
url: job.url,
|
||||
failed_at: new Date(),
|
||||
output_log: outputLog,
|
||||
return_code: error.code,
|
||||
});
|
||||
} else {
|
||||
console.log(`SUCCESS: ${cleanName}`);
|
||||
await doneCol.insertOne({
|
||||
filename: job.filename,
|
||||
url: job.url,
|
||||
finished_at: new Date(),
|
||||
output_log: outputLog,
|
||||
result: "success",
|
||||
});
|
||||
}
|
||||
// Remove from queue in both cases
|
||||
await queueCol.deleteOne({ _id: job._id });
|
||||
} catch (dbErr) {
|
||||
console.error("DB error:", dbErr);
|
||||
} finally {
|
||||
resolve(); // resolve promise when done
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// ========================
|
||||
// MAIN LOOP
|
||||
// ========================
|
||||
@@ -64,24 +71,36 @@ async function main() {
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
const db = client.db(DB_NAME);
|
||||
|
||||
const db = client.db(DB_NAME);
|
||||
queueCol = db.collection("queue");
|
||||
doneCol = db.collection("done");
|
||||
failedCol = db.collection("failed");
|
||||
|
||||
while (true) {
|
||||
const job = await queueCol.findOneAndUpdate(
|
||||
{},
|
||||
{ $set: { processing: true } },
|
||||
{ sort: { created_at: 1 }, returnDocument: "after" }
|
||||
{
|
||||
// Only pick jobs NOT being processed
|
||||
$or: [
|
||||
{ processing: { $exists: false } },
|
||||
{ processing: false }
|
||||
]
|
||||
},
|
||||
{
|
||||
// Atomically claim job
|
||||
$set: { processing: true, started_at: new Date() }
|
||||
},
|
||||
{
|
||||
sort: { created_at: 1 },
|
||||
returnDocument: "after"
|
||||
}
|
||||
);
|
||||
|
||||
if (job && job.value) {
|
||||
await runJob(job.value);
|
||||
if (job && job.token) {
|
||||
await runJob(job);
|
||||
} else {
|
||||
// Queue empty, wait 1 second
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
// No available job, wait a bit
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -90,3 +109,4 @@ async function main() {
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
Reference in new issue
Block a user