-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.js
executable file
·108 lines (91 loc) · 2.91 KB
/
worker.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
const express = require('express')
var bodyParser = require('body-parser');
var fileUpload = require('express-fileupload')
var request = require('request')
var fs = require('fs')
var Jimp = require('jimp')
const app = express()
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(fileUpload())
var masterEndpoint
var storageEndpoint
kvEndpoint = require('./common').getKeyvaluestoreEndpoint();
if (!kvEndpoint){
console.log("Usage: node worker.js <KEYVALUESTORE_ENDOPOINT>")
console.log("Alternatively, specify MICRO_KEYVALUESTORE_ENDPOINT env variable.")
process.exit()
}
getServiceEndpoints()
function doWork() {
request.get({uri: masterEndpoint + '/task/next'}, (err, res, body) => {
if (!err && res.statusCode == 200) {
task = JSON.parse(body)
console.log("Working on task " + task.id)
if (task && task.id != -1) {
var imagePath = __dirname + '/data/worker/' + task.id + '.png'
imagePath = imagePath.replace("//", "/"); // if _dirname is /, then we get //worker/1.png
request.get({uri: masterEndpoint + '/task/' + task.id + '/download'}).pipe(fs.createWriteStream(imagePath)).on('close', () => {
console.log("\tprocessing " + imagePath)
Jimp.read(imagePath,(jimpErr, image) => {
if (jimpErr) {
console.log("\terror: " + jimpErr)
setTimeout(doWork, 5000)
return
}
image.invert().write(imagePath, () => {
console.log('\tdone')
var formData = {
image: fs.createReadStream(imagePath),
}
var postImage = {
uri: storageEndpoint + '/upload/' + task.id,
formData: formData
}
request.post(postImage, (storageError, storageResponse, storageBody) => {
if (!storageError && storageResponse.statusCode == 200) {
request.post({uri: masterEndpoint + '/task/' + task.id + '/finish'})
setTimeout(doWork, 1000)
} else {
console.log("\tError storing processed image")
setTimeout(doWork, 5000)
}
})
})
})
})
} else {
setTimeout(doWork, 5000)
}
} else {
console.log("Nothing to do at the moment")
setTimeout(doWork, 5000)
}
})
}
function getServiceEndpoints() {
if (!masterEndpoint) {
request(kvEndpoint + '/masterendpoint', (error, response, body) => {
if (!error && response.statusCode == 200) {
masterEndpoint = 'http://' + body
} else {
console.log("Error getting Master service.")
}
})
}
if (!storageEndpoint) {
request(kvEndpoint + '/storageendpoint', (error, response, body) => {
if (!error && response.statusCode == 200) {
storageEndpoint = 'http://' + body
} else {
console.log("Error getting Storage service.")
}
})
}
if (masterEndpoint && storageEndpoint) {
doWork()
} else {
setTimeout(getServiceEndpoints, 1000)
}
}
console.log("Using keyvaluestore endpoint: ", kvEndpoint);