-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
executable file
·122 lines (100 loc) · 2.88 KB
/
db.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
const express = require('express')
var bodyParser = require('body-parser');
var request = require('request')
var os = require('os');
var ip = require('ip');
const app = express()
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
const port = process.env.MICRO_DB_PORT || process.env.PORT || 9001;
var dataStore = {}
var oldestNotFinishedTask = 0
kvEndpoint = require('./common').getKeyvaluestoreEndpoint();
if (!kvEndpoint){
console.log("Usage: node db.js <KEYVALUESTORE_ENDOPOINT>")
console.log("Alternatively, specify MICRO_KEYVALUESTORE_ENDPOINT env variable.")
process.exit()
}
// Get DB IP and try to report it to the key-value registry
setTimeout(registerService, 0, 'dbendpoint', ip.address())
function registerService(service, address) {
// POST the address to the registry
var options = {
uri: kvEndpoint + "/" + service,
json: { "value": address + ":" + port }
}
request.post(options, function(error, response, body) {
// If post was successful, continue, otherwise inform the user and exit
if (!error && response.statusCode == 200) {
console.log("Database endpoint registered")
startService()
} else {
console.log("Key-value store is not available")
setTimeout(registerService, 1000, service, address)
}
})
}
app.post('/task', (req, res) => {
var taskId = dataStoreSize()
dataStore[taskId] = {
"id": taskId,
state: -1
}
res.send("" + taskId)
})
app.get('/task/next', (req, res) =>{
for (var i = 0; i < dataStoreSize(); i++) {
var state = dataStore[i]['state']
if (state != 0) {
continue
}
// Ensure tasks are processed on time. In case after 20s the status is not finished.
// reset it to "ready".
setTimeout(() => {
if (dataStore[i]['state'] != 2) {
dataStore[i]['state'] = 0
}
}, 20000)
dataStore[i]['state'] = 1
res.send(dataStore[i])
return
}
res.status(404).send({'id': -1, 'state': 0})
})
app.get('/task/:taskId', (req, res) => {
var taskId = req.params.taskId
if (taskId in dataStore) {
res.send(dataStore[taskId])
} else {
res.status(404).send("Task not found")
}
})
app.post('/task/:taskId/ready', (req, res) => {
var taskId = req.params.taskId
updateTaskState(taskId, 0, res)
})
app.post('/task/:taskId/finish', (req, res) => {
var taskId = req.params.taskId
updateTaskState(taskId, 2, res)
})
function updateTaskState(taskId, state, res) {
if (taskId in dataStore) {
dataStore[taskId]['state'] = state
res.send()
} else {
res.status(404).send("Task not found")
}
}
function dataStoreSize() {
return Object.keys(dataStore).length
}
function startService() {
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`Database is listening on ${port}`)
})
}
console.log("Running db on port: ", port);
console.log("Using keyvaluestore endpoint: ", kvEndpoint);