-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (120 loc) · 4.06 KB
/
index.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var bodyParser = require('body-parser');
var config;
if(process.argv.length == 2){
config = require("cson").load("config.cson");
} else {
config = require("cson").load(process.argv[2]);
}
(require("./src/db")(config)).then(function(db){
var express = require('express');
var slave = require("./src/slave")(db,config);
var scheduler = require('./src/scheduler')(slave, db, config);
var app = express();
app.use(bodyParser.json());
app.get("/status", function(req, res){
res.json({status: "up"});
});
app.get("/pdfs/processAll", function(req, res){
var processing = slave.processSolutions();
res.json({processing: processing});
});
// id = solutionId
app.get("/pdfs/process/:id", function(req, res) {
slave.processSpecificSolution(req.params.id, function(result, err) {
res.json({result: result, error: err});
});
});
// id = solutionId
app.get("/pdfs/reset/:id", function(req, res) {
slave.resetPdf(req.params.id, function(error) {
res.json({reset: error === undefined, error: error});
});
});
app.get("/pdfs/resetAll", function(req, res) {
slave.resetAllPdf(function(error) {
res.json({reset: error === undefined, error: error});
})
});
// id = solutionId
app.get("/tests/:id", function (req, res) {
slave.runTest(req.params.id, function(err, testResults) {
var o = {testResults: testResults};
if (err) {
o.error = err;
}
res.json(o);
});
});
app.get("/solutions/store", function(req, res){
var started = slave.storeAllSolutions();
if(started){
res.send("starting sharejs store operation").end();
} else {
res.status(400).send("sharejs store operation still in progress").end();
}
});
// Warning: Could cause heavy load.
app.get("/solutions/storeFinal", function(req, res) {
slave.storeAllFinalSolutions();
res.sendStatus(204);
});
// id = solutionId
app.get("/solutions/store/:id", function(req, res) {
var cb = function(err, result) {
if (err)
res.json({error: err});
else
res.json({result: result});
};
if (!slave.storeSolution(req.params.id, cb))
res.status(400).send("cannot store solution while a bulk store operation is in progress");
});
/**
* A job consists of a function name (see jobs.js) which is to be executed
* and a job specification (called data) which specifies when to execute the job.
* Data accepts all formats that node-schedule accepts.
*
* A job (identified by database id) can only run once per slave.
* But a slave can run multiple jobs of different id.
*/
app.post("/jobs/add", function(req, res) {
info = req.body;
if (info.name === undefined || info.data === undefined) {
res.status(400).send("you must specify name and data for the job").end();
return;
}
if (info.type !== undefined && type != "recurring" && type != "cron" && type != "date") {
res.status(400).send("the specified type is unknown / not valid").end();
return;
}
if (!scheduler.isValidJob(info.name, info.data)) {
res.status(400).send("job is not known, or data is not a valid cron string or was not accepted by node-schedule").end();
return;
}
// ok everything good now!
scheduler.addJob(info.name, info.type, info.data, function(changes){
res.json({id: changes.generated_keys[0]});
});
});
// id = jobId
app.get("/jobs/run/:id", function(req, res) {
info = req.body;
scheduler.runJob(req.params.id, function(err, job) {
if (err === null || err === undefined)
res.json({job: job});
else
res.status(400).json({error: undefined});
});
});
// id = jobId
app.get("/jobs/cancel/:id", function(req, res) {
// cancel a job only if its running
scheduler.cancelJob(req.params.id);
});
console.log("starting slave server on " + config.developmentPort);
app.listen(config.developmentPort);
if (config.jobs.startAll === true) {
console.log("starting all jobs");
scheduler.startAll();
}
});