forked from Arcana/node-dota2-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.js
173 lines (158 loc) · 4.06 KB
/
queue.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const winston = require('winston')
const logger = winston.createLogger({
level: 'debug',
format: winston.format.json(),
defaultMeta: {
service: 'queue'
},
transports: [
new winston.transports.File({
filename: 'qerror.log',
level: 'error'
}),
new winston.transports.File({
filename: 'qcombined.log'
})
]
})
// Default backoff is 10s
const DEFAULT_BACKOFF = 10000
const DEFAULT_RATELIMIT = 100
const STATE = {
IDLE: 'idle',
RUNNING: 'running',
BLOCKED: 'blocked',
}
/**
* Job queue with exponential backoff
**/
module.exports = class Queue {
/**
* Creates a queue with the given backoff parameter
* @param backoff number of milliseconds to be used as base backoff time
* @param debug whether or not debug info should be logged
**/
constructor(backoff, rate_limit, debug) {
this._backoff = backoff || DEFAULT_BACKOFF
this._rate_limit = rate_limit || DEFAULT_RATELIMIT
this._debug = debug
this._retries = 0
this._state = STATE.IDLE
this._queue = []
}
/**
* Get the current state of the queue
**/
get state() {
return this._state
}
/**
* Get the rate_limit
**/
get rate_limit() {
return this._rate_limit
}
/**
* Set the rate-limit
* @param rate_limit #millis to wait between requests
**/
set rate_limit(rate_limit) {
this._rate_limit = rate_limit
}
/**
* Get the backoff rate
**/
get backoff(){
return this._backoff
}
/**
* Set the backoff rate
* @param backoff #millis for exponential backoff
**/
set backoff(backoff) {
this._backoff = backoff
}
/**
* Schedule a job for execution
* @param job function that needs to be executed
**/
schedule (job) {
if (this._debug) {
logger.debug('Scheduling job')
}
this._queue.push(job)
if (this._state === STATE.IDLE) {
this._state = STATE.RUNNING
if (this._retries === 0) {
this._execute()
}
}
}
/**
* Block job execution
**/
block() {
if (this._debug) {
logger.debug('Blocking queue')
}
this._state = STATE.BLOCKED
}
/**
* Start job execution
**/
release() {
if (this._state === STATE.BLOCKED) {
if (this._debug) {
logger.debug('Activating queue')
}
this._state = STATE.IDLE
if (this._retries === 0) {
this._execute()
}
}
}
/**
* Deletes all the jobs from the queue
**/
clear() {
this._queue = []
this._retries = 0
this._state = STATE.IDLE
}
_execute() {
let job = this._queue[0]
if (job) {
switch(this._state) {
case STATE.BLOCKED: {
this._retries++
let r = Math.floor(Math.random() * (this._retries + 1))
let self = this
if (this._debug) {
logger.debug('Queue blocked, sleeping for ' + (r * this.backoff))
}
setTimeout(() => {
self._execute()
}, r * this.backoff)
break
}
default: {
if (this._debug) {
logger.debug('Executing job')
}
this._retries = 0
this._state = STATE.RUNNING;
(this._queue.shift())()
// Apply rate limiting
setTimeout(() => {
this._execute()
}, this.rate_limit)
}
}
} else {
if (this._debug) {
logger.debug('Queue is empty, going idle')
}
this._state = STATE.IDLE
}
}
}