Last active
August 29, 2015 14:15
-
-
Save crueber/a728d2124c2af5846850 to your computer and use it in GitHub Desktop.
Using SQS with CoffeeScript in Express. Ugly, but works.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Note on globals: _, app, logger, and Data are global. Everything else is standard node. | |
# npm libs: aws-sdk, mongoose, express, coffeescript, lo-dash... At least, where this came frome. | |
# Lo-dash could easily be replaced with a for-each, but I use it all over the place in my apps. | |
aws = require 'aws-sdk' | |
aws.config.update accessKeyId: app.get('s3_access_key'), secretAccessKey: app.get('s3_secret_key') | |
sqs = new aws.SQS(region: app.get('aws_region')) # aka 'us-east-1' | |
queue_url = "http://sqs.us-east-1.amazonaws.com/#{app.get('aws_user_id')}/#{app.get('s3_bucket')}" | |
poll_every = 1000*5 # 5 seconds, express in ms. | |
long_poll = 20 # 20 seconds, expressed in seconds. | |
recvMessage = -> | |
sqs.receiveMessage {QueueUrl: queue_url, WaitTimeSeconds: long_poll}, (err, data) -> | |
if data and data.Messages | |
_.each data.Messages, (message) -> | |
details = JSON.parse(message.Body) | |
return sqs.deleteMessage({ QueueUrl: queue_url, ReceiptHandle: message.ReceiptHandle }, logger.logErr) if details.Event is "s3:TestEvent" | |
if details.Records | |
data_id = details.Records[0].s3.object.key.split('/')[2] | |
Data.verify_location data_id, (err) -> | |
sqs.deleteMessage({ QueueUrl: queue_url, ReceiptHandle: message.ReceiptHandle }, logger.logErr) | |
setTimeout recvMessage, poll_every | |
# Start receiving messages. | |
recvMessage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was specifically built only to receive messages. I used it for the events that S3 pushes to SQS when a file finishes uploading. It works like a charm.