Created
September 10, 2015 20:02
-
-
Save cchen408/dece16968632fe327575 to your computer and use it in GitHub Desktop.
worker code
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
"use strict"; | |
var Promise = require("bluebird"); | |
var Handler = require("./handler.js")(); | |
var Domain = require('domain'); | |
var Helper = require("paystand/helpers"); | |
var app = Helper.getHelper("app"); | |
var queue = Helper.getHelper("queue"); | |
var Errors = Helper.getHelper("errors"); | |
var wsMerchant = Helper.getWebService("merchant"); | |
Handler.setType("process-ach"); | |
Handler.log("Created process-ach handler"); | |
/** | |
* Set work | |
*/ | |
var work = function (payload, cb) { | |
var domain = Domain.create(); | |
// Domain error | |
domain.on("error", function (err) { | |
console.log(err.stack); | |
}); | |
// Domain run | |
domain.run(function () { | |
return Promise.resolve(true) | |
.bind({}) | |
// Get Job | |
.then(function () { | |
Handler.log("process-ach"); | |
if (!payload) { | |
Handler.log("process-ach: no payload found"); | |
} | |
if (!payload.id) { | |
Handler.log("process-ach: no payload id found"); | |
} | |
Handler.log("process-ach find job id"); | |
return Handler.model.findById(payload.id) | |
.catch(function (error) { | |
throw Errors.PayStand("SystemError", "961a1fbb98e2485bbb87078763c8ac21", "Error finding job", error); | |
}); | |
}) | |
// Attempt to Verify | |
.then(function (job) { | |
if (!job) { | |
Handler.log("process-ach: no job found"); | |
} | |
Handler.log("attempt to process ach"); | |
this.job = job; | |
this.job.attempted = new Date(); | |
this.job.attempts = !this.job.attempts ? 1 : this.job.attempts + 1; | |
// call a method to do what we need it to do | |
return wsMerchant.processAch(); | |
}) | |
// Save job | |
.then(function () { | |
Handler.log("verify-drops: saving job"); | |
this.job.completed = new Date(); | |
this.job.result = "200"; | |
return this.job.save() | |
.catch(function (error) { | |
throw Errors.PayStand("SystemError", "23969650cfb543d79fdd57da1b625f60", "Error saving job", error); | |
}); | |
}) | |
// Schedule the next job | |
.then(function () { | |
Handler.log("Process-ach: scheduling next job"); | |
return Handler.nextJob(); | |
}) | |
// Success | |
.then(function () { | |
Handler.log("Process-ach: success"); | |
return "success"; | |
}) | |
// Handle errors | |
.catch(function (error) { | |
console.log("error:", error); | |
Handler.log("Process ach failed. Retrying in 60 seconds"); | |
if (!this.job) { | |
return "failed"; | |
} | |
return this.job.save() | |
.catch(function (error) { | |
Errors.SystemError("1080410ef42e47d4b90f810dd1610f48", "Error saving job", error); | |
}) | |
.then(function (job) { | |
return Handler.nextJob(60); | |
}) | |
.then(function () { | |
return "success"; | |
}) | |
.catch(function () { | |
return "success"; | |
}); | |
}) | |
// Nodeify | |
.nodeify(function (error, value) { | |
cb(value); | |
}); | |
}); | |
}; | |
Handler.setWork(work); | |
/** | |
* Add Next Job | |
*/ | |
Handler.nextJob = function (interval, q) { | |
return app.models.Job.create({"data": {}}) | |
.catch(function (error) { | |
throw Errors.PayStand("SystemError", "997c3a409f59429da00effb0e4dce6ae", "Error creating next job", error); | |
}) | |
.bind({}) | |
// Add to Queue | |
.then(function (job) { | |
var config = app.get("queue"); | |
this.settings = { | |
"priority": 0, | |
"delay": interval, | |
"timeToRun": 60, | |
"tube": "token", | |
"tubeType": "token", | |
"payload": {"id": job.id} | |
}; | |
var queue = Helper.getHelper("queue"); | |
var Queue = q || queue; | |
return Queue.putSpecial(this.settings.tube, this.settings.tubeType, this.settings.payload, | |
this.settings.priority, this.settings.delay, this.settings.timeToRun); | |
}) | |
// Write to log | |
.then(function (jobId) { | |
this.settings.jobId = jobId; | |
Handler.log("Job added to queue: Process-ach (process-ach) (jobId: " + jobId + ")"); | |
return true; | |
}) | |
// Errors | |
.catch(function (error) { | |
Errors.paystand("SystemError", "7463dd73d003449f9fd912718e7296e1", "Error creating next job", error); | |
}); | |
}; | |
module.exports = function () { | |
return Handler; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment