summaryrefslogtreecommitdiff
path: root/daemons/jobs.js
blob: 0ca8552a2b8caa36b00640624e2621046190f69c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const fs = require('fs');
const child_process = require('child_process');

let jobsList = [];
let jobs = [];
let history = require('../data/history.json');

fs.writeFileSync("../data/running.json", "null");

setInterval(() => {
    if (JSON.stringify(fs.readdirSync("../data/jobs")) !== JSON.stringify(jobsList)) {
        console.log("Updating the jobs list...");
        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);

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 + " [" + 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" });
                    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]);
                    }

                    fs.unlinkSync("../data/jobs/" + job._id);

                    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));
                    fs.writeFileSync("../data/running.json", "null");
                } 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]);
                    }

                    fs.unlinkSync("../data/jobs/" + job._id);

                    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));
                    fs.writeFileSync("../data/running.json", "null");
                }
            }

            console.log("\nCompleted");
        }
    }, 1000);
}, 500);