-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
147 lines (141 loc) · 5 KB
/
server.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
144
145
146
147
'use-strict';
const express = require('express');
const enable_ws = require('express-ws');
const md5 = require('md5');
const http = require('http');
const fs = require('fs');
const ws_handler = require('./ws-job-handler');
const port = process.env.PORT || 8080;
const bind_ip = process.env.IP || '127.0.0.1';
/**
* [{
* jobid: "hash",
*
* },...]
*/
/*
// test data for reference:
var jobs = [{
jobid: "89b4138517796b88585a05a3bb76d6b2",
description: "Protein folding",
workers: 0,
cores: 0,
finished: false,
threads_required: 5,
// threads_finished: {1:{finished:false,result:'result'}, 2:{finished:true,result:'result'}},
threads_finished: {},
unfinished_threads: [1,2,3,4,5],
webhook_completion_url: 'http://localhost:1111',
workerjs: 'onmessage=function(event){setTimeout(()=>postMessage(event.data), 10000)}',
workerjs_hash: 'f5f4d1722152e8f0e3bec54704432dcf',
password: '5f4dcc3b5aa765d61d8327deb882cf99' // md5 hash for 'password'
}];
*/
var jobs = [];
const workerjs_template = fs.readFileSync('public/worker.js', 'utf8');
var app = express();
enable_ws(app);
ws_handler(app, jobs);
app.use(express.json());
app.get('/worker.js', function(req, res){
res.setHeader('Content-Type', 'application/javascript');
if(req.query.jobid){
res.send(workerjs_template.replace('__JOBID_HASH__', req.query.jobid));
}else{
res.send(workerjs_template);
}
});
app.get('/workerjs/:jobid', function(req, res){
res.setHeader('Content-Type', 'application/javascript');
if(req.params.jobid){
for(let each_job of jobs){
if(each_job.jobid === req.params.jobid){
res.send(each_job.workerjs);
return;
}
}
}
res.send('');
});
app.get('/workerresults/:jobid', function(req, res){
if(req.params.jobid){
for(let each_job of jobs){
if(each_job.jobid === req.params.jobid){
res.json(each_job.threads_finished);
return;
}
}
}
res.json({});
});
app.post('/createjob', function(req, res){
if(Object.keys(req.body).length){
if(req.body.description && req.body.workerjs &&
req.body.threads_required > 0 && req.body.password){
if(req.body.jobid){
for(let each_job of jobs){
if(req.body.jobid === each_job.jobid){
if(md5(req.body.password) !== each_job.password){
res.status(400).send('Bad password');
return;
}
each_job.description = req.body.description;
each_job.finished = false;
each_job.threads_required = req.body.threads_required;
each_job.threads_finished = {};
let temp = [];
for(let c=1;c<=each_job.threads_required;c++) temp.push(c);
each_job.unfinished_threads = temp;
each_job.workerjs = req.body.workerjs;
each_job.workerjs_hash = md5(req.body.workerjs);
each_job.webhook_completion_url = req.body.webhook_completion;
res.send('Updated');
return;
}
}
}
let temp = [];
for(let c=1;c<=req.body.threads_required;c++) temp.push(c);
jobs.push({
jobid: md5(''+Date.now()+Math.random()),
description: req.body.description,
workers: 0,
cores: 0,
finished: false,
threads_required: req.body.threads_required,
threads_finished: {},
unfinished_threads: temp,
webhook_completion_url: req.body.webhook_completion,
workerjs: req.body.workerjs,
workerjs_hash: md5(req.body.workerjs),
password: md5(req.body.password)
});
res.send(jobs[jobs.length-1].jobid);
}else{
res.status(400).send('Missing parameters');
}
}else{
res.status(400).send('Bad JSON');
}
});
app.get('/listjobs', function(req, res){
let result = [];
for(let each_job of jobs){
let finished_thread_count = 0;
for(let thread_number in each_job.threads_finished){
if(each_job.threads_finished[thread_number].finished) finished_thread_count++;
}
result.push({
jobid: each_job.jobid,
description: each_job.description,
workers: each_job.workers,
cores: each_job.cores,
finished: each_job.finished,
threads_required: each_job.threads_required,
threads_finished: finished_thread_count
});
}
res.json(result);
});
app.use(express.static(__dirname+'/public'));
app.listen(port, bind_ip, null, ()=>console.log('Colossal Tortoise server listening on port '+port));