Skip to content

Commit 4a5eb25

Browse files
author
Dmitry Ovsyanko
committed
Queue +interval
1 parent 17720a7 commit 4a5eb25

4 files changed

Lines changed: 77 additions & 6 deletions

File tree

__tests__/Queue.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ const app = new Application ({
1717
logger
1818
})
1919

20+
test ('bad', async () => {
21+
22+
expect (() => new Queue (app, {name: 'qx', cron: 1})).toThrow ('type')
23+
expect (() => new Queue (app, {name: 'qx', cron: ''})).toThrow ('nvalid')
24+
expect (() => new Queue (app, {name: 'qx', interval: '10'})).toThrow ('type')
25+
expect (() => new Queue (app, {name: 'qx', interval: -1})).toThrow ('nvalid')
26+
expect (() => new Queue (app, {name: 'qx', interval: 2147483648})).toThrow ('nvalid')
27+
expect (() => new Queue (app, {name: 'qx', cron: '* * * * * *', interval: 10})).toThrow ('exclusive')
28+
29+
})
30+
2031
test ('maxPending', async () => {
2132

2233
{
@@ -178,7 +189,7 @@ test ('cron', async () => {
178189

179190
process.on('exit', (code) => {
180191
console.log('Process beforeExit event with code: ', code);
181-
});
192+
});
182193

183194
const u = {id: 1}, a = [u], r = []
184195

@@ -210,4 +221,42 @@ test ('cron', async () => {
210221
expect (a).toHaveLength (0)
211222
expect (r).toStrictEqual ([u])
212223

224+
})
225+
226+
test ('interval', async () => {
227+
228+
process.on('exit', (code) => {
229+
console.log('Process beforeExit event with code: ', code);
230+
});
231+
232+
const u = {id: 1}, a = [u], r = []
233+
234+
class TestQueue extends Queue {
235+
async peek () {return a [0] || null}
236+
}
237+
238+
await new Promise ((ok, fail) => {
239+
240+
const q = new TestQueue (app, {
241+
name: 'q8',
242+
request: {type: 'users'},
243+
interval: 10,
244+
on: {
245+
end: function () {r.push (this.result)},
246+
error: function () {fail (this.error)},
247+
finish: function () {a.shift ()},
248+
}
249+
})
250+
251+
q.on ('job-next', () => {
252+
if (q.pending.size === 0) ok ()
253+
})
254+
255+
})
256+
257+
app.emit ('finish')
258+
259+
expect (a).toHaveLength (0)
260+
expect (r).toStrictEqual ([u])
261+
213262
})

lib/Queue.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,41 @@ class Queue extends JobSource {
77

88
if (!('maxPending' in o)) o.maxPending = 1
99

10+
const {cron, interval} = o
11+
12+
if (cron != null) {
13+
14+
const t = typeof cron; if (t !== 'string') throw new Error (`Invalid cron type: '${t}' (expected 'string')`)
15+
16+
if (!cron) throw new Error (`Invalid cron option`)
17+
18+
}
19+
20+
if (interval != null) {
21+
22+
const t = typeof interval; if (t !== 'number') throw new Error (`Invalid interval type: '${t}' (expected 'number')`)
23+
24+
if (!Number.isSafeInteger || interval < 1 || interval > 2147483647) throw new Error (`Invalid interval value: ${interval}`)
25+
26+
}
27+
28+
if (cron && interval) throw new Error (`'cron' and 'interval' options are mutually exclusive`)
29+
1030
super (app, o)
1131

1232
this.isStopped = false
1333

14-
if ('cron' in o) this.cron = new Cron (o.cron, {}, () => this.check ())
34+
if (cron) this.cron = new Cron (o.cron, {}, () => this.check ())
35+
36+
if (interval) this.interval = setInterval (() => this.check (), interval)
1537

1638
app.once ('finish', () => this.stop ())
1739

1840
}
1941

2042
stop () {
2143

22-
if ('cron' in this) this.cron.stop ()
44+
if ('interval' in this) clearInterval (this.interval); else if ('cron' in this) this.cron.stop ()
2345

2446
this.app.jobSources.delete (this.name)
2547

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "doix",
3-
"version": "1.0.62",
3+
"version": "1.0.63",
44
"description": "A general purpose sever side framework",
55
"main": "index.js",
66
"files": [

0 commit comments

Comments
 (0)