Created
December 15, 2013 17:10
-
-
Save mwawrusch/7975444 to your computer and use it in GitHub Desktop.
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
### | |
Primary task scheduler for fanignite. | |
This queries the site api for tasks to schedule, then schedules them. | |
### | |
_ = require 'underscore' | |
async = require 'async' | |
request = require 'request' | |
logSetup = require './log-setup' | |
iron_mq = require 'iron_mq' | |
logger = logSetup 'worker-task-scheduler',"",0 | |
ironMq = new iron_mq.Client | |
token: "" | |
project_id: "" | |
ironMqQueue = ironMq.queue "scheduler" | |
pushData = | |
push_type: "unicast" | |
retries: 5 | |
retries_delay: 60 | |
subscribers: [ | |
url: "http://fanignite.com/iapi/scheduler/iron-io-inbound/process-one-task" | |
] | |
ironMqQueue.update pushData, (err,body) -> | |
logger.error err if err | |
return if err | |
logger.info "Fetching scheduled tasks at #{new Date()}" | |
request | |
uri: "http://fanignite.com/iapi/scheduler/accounts-to-process" | |
headers: | |
'Content-Type': 'application/json' | |
'Accept' : 'application/json' | |
body: JSON.stringify({}) | |
method: "POST" | |
, (err, res, bodyBeforeJson) -> | |
logger.error err if err | |
return if err | |
result = JSON.parse(bodyBeforeJson) | |
items = result.items || [] | |
logger.info "Queuing #{items.length} scheduled tasks" | |
### | |
Loading one item from the backend, then add it to the queue. | |
### | |
fbOneItem = (item,cb) -> | |
msg = | |
body: JSON.stringify({schedulerId : item._id}) | |
delay: 1 | |
expires_in: 60 * 15 # Expires after 15 minutes | |
ironMqQueue.post msg, (err,item) -> | |
logger.error {error: JSON.stringify(err), message: err.message} if err | |
cb null | |
async.eachSeries items, fbOneItem, (err) -> | |
return logger.error err if err | |
logger.info "Queuing #{items.length} scheduled tasks COMPLETE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment