-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo-queue.ts
More file actions
156 lines (142 loc) · 3.49 KB
/
mongo-queue.ts
File metadata and controls
156 lines (142 loc) · 3.49 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
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
import { Collection } from "mongodb";
import { v4 as uuidv4 } from 'uuid';
interface Payload {
key?: string;
[x: string]: any
}
interface QueueOptions {
visibility: Number,
delay: Number|Date,
maxRetries?: Number,
deadQueue?: Collection
}
export class MongoQueue {
private collection: Collection;
private visibility: Number;
private delay: Number | Date;
private maxRetries: Number;
private deadQueue: Collection;
constructor(collection: Collection, opts: QueueOptions) {
this.collection = collection
this.visibility = opts.visibility
this.delay = opts.delay
this.maxRetries = opts.maxRetries
this.deadQueue = opts.deadQueue
}
private nowPlusSecs(secs) {
return new Date(Date.now() + secs * 1000)
}
private getVisibilityDate(delay?: Number|Date) {
delay = delay || this.delay
if(delay instanceof Date) {
return delay
} else {
return this.nowPlusSecs(delay)
}
}
async get(quantity: number) {
let writes = []
let acks = []
for(let i = 0; i < quantity; i++) {
let ack = uuidv4()
let updateOne = {
updateOne: {
filter: {
visible: {
$lte: new Date()
},
deleted: null
},
update: {
$inc : { tries : 1 },
$set: {
ack,
visible: this.nowPlusSecs(this.visibility)
}
}
}
}
acks.push(ack)
writes.push(updateOne)
}
await this.collection.bulkWrite(writes)
let msgs = await this.collection.find({ack: {$in: acks}, deleted: null}).toArray()
return msgs
}
async add(payload: Payload|Array<Payload>, delay?: Number | Date) {
let payloads = new Array<Payload>().concat(payload)
let writes = []
if (payloads.length === 0) {
throw new Error('Queue.add(): Array payload length must be greater than 0')
}
for(let p of payloads) {
let pKey = p.key || uuidv4()
writes.push({
updateOne: {
filter: {
key: pKey
},
update: {
$setOnInsert: {
visible : this.getVisibilityDate(delay),
payload : p,
key: pKey
}
},
upsert: true
}
})
}
return await this.collection.bulkWrite(writes)
}
async ack(ack: string) {
var query = {
ack,
visible : { $gt : new Date() },
deleted : null
}
var update = {
$set : {
deleted : new Date(),
}
}
return await this.collection.findOneAndUpdate(query, update, { returnOriginal : false })
}
async ping(ack) {
var query = {
ack : ack,
visible : { $gt : new Date() },
deleted : null,
}
var update = {
$set : {
visible : this.nowPlusSecs(this.visibility)
}
}
return await this.collection.findOneAndUpdate(query, update, { returnOriginal : false })
}
async total() {
return await this.collection.count()
}
async size() {
let query = {
deleted : null,
visible : { $lte : new Date() },
}
return await this.collection.count(query)
}
async inFlight() {
let query = {
ack : { $exists : true },
visible : { $gt : new Date() },
deleted : null,
}
return await this.collection.count(query)
}
async done() {
let query = {
deleted : { $exists : true },
}
return await this.collection.count(query)
}
}