-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
194 lines (193 loc) · 4.65 KB
/
index.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const aws = require('aws-sdk')
const crypto = require('crypto')
const stringify = require('json-stable-stringify')
const debug = require('debug')('sqs:main')
const SQSmsg = require('./msg');
class SQS {
constructor (config) {
if (typeof config === 'string') {
config = {
name: config
}
}
var awsConfig = {}
if (config.access && config.secret) {
awsConfig.accessKeyId = config.access
awsConfig.secretAccessKey = config.secret
}
awsConfig.region = config.region || 'us-east-2'
this.sqs = new aws.SQS(awsConfig)
this.defaultVisabilityTimeout = config.visibilityTimeout || 60 * 5// 5 min
this.queueUrl = this.getQueueUrl(config.name)
this.stopped = false
this.running = false
}
getQueueUrl (name) {
if (!name.endsWith('.fifo')) {
name = name += '.fifo'
}
return this.checkQueue(name).catch(() => {
return this.createQueue(name)
})
}
checkQueue (name) {
return new Promise((resolve, reject) => {
this.sqs.getQueueUrl({
QueueName: name
}, (err, data) => {
if (err) {
debug('setQueueUrl err')
return reject(err)
}
if (data && data.QueueUrl) {
return resolve(data.QueueUrl)
}
return reject(new Error('nothing returned'))
})
})
}
createQueue (name) {
return new Promise((resolve, reject) => {
const config = {
QueueName: name,
Attributes: {
FifoQueue: 'true',
ContentBasedDeduplication: 'true',
VisibilityTimeout: String(this.defaultVisabilityTimeout)
}
}
this.sqs.createQueue(config, (err, resp) => {
if (err) {
debug('createQueue error')
return reject(err)
}
resolve(resp.QueueUrl)
})
})
}
push (data, groupID) {
groupID = groupID || crypto.randomBytes(64).toString('hex')
return this.queueUrl.then(url => {
const params = {
MessageBody: stringify(data),
QueueUrl: url,
MessageGroupId: groupID
}
return new Promise((resolve, reject) => {
this.sqs.sendMessage(params, err => {
if (err) {
debug('push error')
return reject(err)
}
resolve()
})
})
})
}
pull () {
if (this.inflight) {
return this.inflight
}
this.inflight = this.queueUrl.then(url => {
const params = {
QueueUrl: url,
WaitTimeSeconds: 20
}
return new Promise((resolve, reject) => {
this.sqs.receiveMessage(params, (err, data) => {
if (err) {
debug('pull err')
return reject(err)
}
const msgs = data.Messages
if (!msgs || !msgs.length) {
return reject(new Error('no messages'))
}
resolve(new SQSmsg(msgs[0], this))
})
})
}).then(data => {
this.inflight = null
return data
}, err => {
this.inflight = null
throw err
})
return this.inflight
}
awk (rcpt) {
debug('awk')
return this.queueUrl.then(url => {
const params = {
QueueUrl: url,
ReceiptHandle: rcpt,
VisibilityTimeout: this.defaultVisabilityTimeout
}
return new Promise((resolve, reject) => {
this.sqs.changeMessageVisibility(params, err => {
if (err) {
debug('changeMessageVisibility err')
return reject(err)
}
resolve()
})
})
})
}
stop () {
this.stopped = true
}
run (handler, recursive) {
if (this.running) {
if (!recursive) {
return
}
} else {
this.running = true
}
if (this.stopped) {
this.running = false
this.stopped = false
return
}
return this.pull().then(data => {
return Promise.resolve(handler(data)).then(resp => {
if (resp) {
return Promise.all([
this.push(resp),
data.done()
])
}
return data.done()
})
}).then(() => {
return this.run(handler, true)
},
e => {
if (e.message !== 'no messages') {
debug('run error')
debug(e)
}
return this.run(handler, true)
})
}
done (rcpt) {
debug('done')
return this.queueUrl.then(url => {
const params = {
QueueUrl: url,
ReceiptHandle: rcpt
}
return new Promise((resolve, reject) => {
this.sqs.deleteMessage(params, err => {
if (err) {
debug('delete msg err')
return reject(err)
}
resolve()
})
})
})
}
}
module.exports = SQS