diff options
Diffstat (limited to 'includes/external/jobs')
-rw-r--r-- | includes/external/jobs/index.js | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/includes/external/jobs/index.js b/includes/external/jobs/index.js index 39b7de5..0f44cc9 100644 --- a/includes/external/jobs/index.js +++ b/includes/external/jobs/index.js @@ -1,13 +1,21 @@ const fs = require('fs'); const child_process = require('child_process'); -let jobs = require('../../data/jobs.json'); +let jobsList = []; +let jobs = []; let history = require('../../data/history.json'); +fs.writeFileSync("../../data/running.json", "null"); + setInterval(() => { - if (JSON.stringify(JSON.parse(fs.readFileSync("../../data/jobs.json").toString())) !== JSON.stringify(jobs)) { + if (JSON.stringify(fs.readdirSync("../../data/jobs")) !== JSON.stringify(jobsList)) { console.log("Updating the jobs list..."); - jobs = JSON.parse(fs.readFileSync("../../data/jobs.json").toString()); + jobs = fs.readdirSync("../../data/jobs").map((i) => { + let obj = JSON.parse(fs.readFileSync("../../data/jobs/" + i).toString()); + obj["_id"] = i; + return obj; + }); + jobsList = fs.readdirSync("../../data/jobs"); } }, 1000); @@ -20,11 +28,13 @@ setTimeout(() => { console.log("\nRunning jobs:"); for (let job of jobs) { - console.log(" " + job.name); + console.log(" " + job.name + " [" + job._id + "]"); let output; let start; let end; + fs.writeFileSync("../../data/running.json", JSON.stringify(job._id)); + try { start = new Date(); output = child_process.execFileSync("php", [job.name + ".php", JSON.stringify(job.options)], { cwd: "../../jobs" }); @@ -37,8 +47,7 @@ setTimeout(() => { description = "," + name + "=" + JSON.stringify(job.options[name]); } - jobs.splice(jobs.indexOf(job), 1); - fs.writeFileSync("../../data/jobs.json", JSON.stringify(jobs)); + fs.unlinkSync("../../data/jobs/" + job._id); history.unshift({ completed: true, @@ -58,6 +67,7 @@ setTimeout(() => { history = history.slice(0, 200); fs.writeFileSync("../../data/history.json", JSON.stringify(history)); + fs.writeFileSync("../../data/running.json", "null"); } catch (e) { end = start ? new Date() : null; console.log(" Failed to process job"); @@ -70,8 +80,7 @@ setTimeout(() => { description = "," + name + "=" + JSON.stringify(job.options[name]); } - jobs.splice(jobs.indexOf(job), 1); - fs.writeFileSync("../../data/jobs.json", JSON.stringify(jobs)); + fs.unlinkSync("../../data/jobs/" + job._id); history.unshift({ completed: false, @@ -91,6 +100,7 @@ setTimeout(() => { history = history.slice(0, 200); fs.writeFileSync("../../data/history.json", JSON.stringify(history)); + fs.writeFileSync("../../data/running.json", "null"); } } |