summaryrefslogtreecommitdiff
path: root/includes/external
diff options
context:
space:
mode:
Diffstat (limited to 'includes/external')
-rw-r--r--includes/external/jobs/index.js102
1 files changed, 102 insertions, 0 deletions
diff --git a/includes/external/jobs/index.js b/includes/external/jobs/index.js
new file mode 100644
index 0000000..aa84db9
--- /dev/null
+++ b/includes/external/jobs/index.js
@@ -0,0 +1,102 @@
+const fs = require('fs');
+const child_process = require('child_process');
+
+// TODO: Jobs history
+
+let jobs = require('../../data/jobs.json');
+let history = require('../../data/history.json');
+
+setInterval(() => {
+ if (JSON.stringify(JSON.parse(fs.readFileSync("../../data/jobs.json").toString())) !== JSON.stringify(jobs)) {
+ console.log("Updating the jobs list...");
+ jobs = JSON.parse(fs.readFileSync("../../data/jobs.json").toString());
+ }
+}, 1000);
+
+setTimeout(() => {
+ setInterval(() => {
+ if (jobs.length > 0) {
+ let pickup = new Date();
+
+ console.log(jobs.length + " job(s)");
+ console.log("\nRunning jobs:");
+
+ for (let job of jobs) {
+ console.log(" " + job.name);
+ let output;
+ let start;
+ let end;
+
+ try {
+ start = new Date();
+ output = child_process.execFileSync("php", [job.name + ".php", JSON.stringify(job.options)], { cwd: "../../jobs" });
+ end = new Date();
+
+ let runtime = end.getTime() - start.getTime();
+ let description = "";
+
+ for (let name of Object.keys(job.options)) {
+ description = "," + name + "=" + JSON.stringify(job.options[name]);
+ }
+
+ jobs.splice(jobs.indexOf(job), 1);
+ fs.writeFileSync("../../data/jobs.json", JSON.stringify(jobs));
+
+ history.unshift({
+ completed: true,
+ error: null,
+ options: job.options,
+ name: job.name + "(" + description.substring(1) + ")",
+ output: output.toString(),
+ time: runtime,
+ tracking: {
+ queue: new Date(job.date).toISOString(),
+ pickup: pickup.toISOString(),
+ start: start.toISOString(),
+ end: end.toISOString(),
+ logged: new Date().toISOString()
+ }
+ });
+
+ history = history.slice(0, 200);
+ fs.writeFileSync("../../data/history.json", JSON.stringify(history));
+ } catch (e) {
+ end = start ? new Date() : null;
+ console.log(" Failed to process job");
+ console.error(e);
+
+ let runtime = end ? (start ? end.getTime() - start.getTime() : null) : null;
+ let description = "";
+
+ for (let name of Object.keys(job.options)) {
+ description = "," + name + "=" + JSON.stringify(job.options[name]);
+ }
+
+ jobs.splice(jobs.indexOf(job), 1);
+ fs.writeFileSync("../../data/jobs.json", JSON.stringify(jobs));
+
+ history.unshift({
+ completed: false,
+ error: e.stack,
+ options: job.options,
+ name: job.name + "(" + description.substring(1) + ")",
+ output: (output ?? e.stdout ?? e.stderr).toString(),
+ time: runtime,
+ tracking: {
+ queue: new Date(job.date).toISOString(),
+ pickup: pickup.toISOString(),
+ start: start ? start.toISOString() : null,
+ end: end ? end.toISOString() : null,
+ logged: new Date().toISOString()
+ }
+ });
+
+ history = history.slice(0, 200);
+ fs.writeFileSync("../../data/history.json", JSON.stringify(history));
+ }
+ }
+
+ console.log("\nCompleted");
+ }
+ }, 1000);
+}, 500); \ No newline at end of file