-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
executable file
·67 lines (57 loc) · 1.75 KB
/
queue.js
File metadata and controls
executable file
·67 lines (57 loc) · 1.75 KB
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
var http = require('http')
// список очередей
// для каждого ip - одна очередь
exports.list = []
// строим очередь заново из config
exports.reset = function() {
config.list.forEach(function (device) {
exports.list[device.ip] = {
queries: [],
free: 1,
}
})
}
// запрос get
// если очередь пуста, сразу выполняем
// иначе помещаем в соотв. очередь
exports.push = function(query_str, device_ip) {
if (exports.list[device_ip]) {
if (exports.list[device_ip].free) {
exports.list[device_ip].free = 0
exports.get(query_str, device_ip)
} else {
exports.list[device_ip].queries.push(query_str)
}
}
}
// если очередь непуста, достаём запрос и выполняем
// иначе - помечаем очередь пустой
exports.shift = function(device_ip) {
if (device_ip) {
if (exports.list[device_ip]) {
if (exports.list[device_ip].queries.length) {
var query = exports.list[device_ip].queries.shift()
exports.get(query, device_ip)
} else {
exports.list[device_ip].free = 1
}
}
}
}
// упрощённый get (обёртка над http.get)
// выполняем запрос, после - сразу следующий из очереди
exports.get = function(query, device_ip) {
// простой get
var request = http.get(query, function(res) {
// доп. действия после запроса
exports.shift(device_ip)
// обработка ошибок
}).on('error', function(e) {
if (e.code === 'ETIMEDOUT') {
} else {
exports.shift(device_ip)
}
}).setTimeout( globals.get('socket_wait_time'), function( ) {
exports.shift(device_ip)
})
}