UNPKG

1.83 kBtext/coffeescriptView Raw
1utils = require './utils'
2
3class SQSConnector
4 constructor: (options) ->
5 @options = options
6 AWS = require 'aws-sdk'
7 credentialsPath = options.credentials
8 AWS.config.loadFromPath(credentialsPath)
9 @sqs = new AWS.SQS()
10 receive: (cb) ->
11 @sqs.receiveMessage
12 QueueUrl: @options.queueUrl
13 MaxNumberOfMessages: 1
14 VisibilityTimeout: 60
15 WaitTimeSeconds: 1
16 MessageAttributeNames: ['All']
17 , (err, data) =>
18 if err then throw err
19 if data.Messages and data.Messages.length > 0
20 message = data.Messages[0]
21 done = (err2) =>
22 if !err2 or String(err2).indexOf(404) > -1
23 console.info 'deleting message'
24 if err2
25 console.error err2
26 @sqs.deleteMessage
27 QueueUrl: @options.queueUrl
28 ReceiptHandle: message.ReceiptHandle
29 , (err) =>
30 console.log 'message deleted'
31 @receive(cb)
32 else
33 console.error err2
34 @receive(cb)
35 returnMessage =
36 Id: message.MessageId
37 Body: message.Body
38 Attributes: utils.getAllAttributes(message)
39 ReceiptHandle: message.ReceiptHandle
40 return cb returnMessage, done
41 @receive(cb)
42 send: (body, attributes, cb) ->
43
44 # if no attributes passed, the second
45 # parameter is the callback
46 if !cb
47 cb = attributes
48 attributes = null
49
50 # set params
51 params =
52 QueueUrl: @options.queueUrl
53 MessageBody: body
54
55 # generate and set messageattributes if attributes passed
56 if attributes
57 attributes = utils.setAllAttributes(attributes)
58 params.MessageAttributes = attributes
59
60 # sendMessage to queue
61 @sqs.sendMessage params, (err, data) ->
62 cb(err, data)
63
64module.exports = SQSConnector
\No newline at end of file