UNPKG

ticketman

Version:

A simple pull-based job/ticket system contians a centeral ticket dispatcher and distributed workers. This system is written in NodeJS, runing on MongoDB

70 lines (56 loc) 2.14 kB
// Generated by CoffeeScript 1.12.5 (function() { var DEFAULT_BASIC_AUTH, PATH, SQARSH_CALLBCAK, TicketManager, assert, debuglog, env, request; assert = require("assert"); debuglog = require("debug")("ticketman:TicketManager"); env = process.env.NODE_ENV || 'development'; DEFAULT_BASIC_AUTH = require('./config/config')[env]['basicAuth']; request = require("request"); PATH = "/api/tickets/new"; SQARSH_CALLBCAK = function() {}; TicketManager = (function() { function TicketManager(name, host, basicAuth) { this.name = name; this.host = host; assert(this.name, "missing name"); assert(this.host, "missing host"); this.basicAuth = basicAuth || DEFAULT_BASIC_AUTH; debuglog("[TicketManager.constructor] @name:" + this.name + ", @host:" + this.host + ", @basicAuth:%j", this.basicAuth); } TicketManager.prototype.issue = function(title, category, content, callback) { var options; if (callback == null) { callback = SQARSH_CALLBCAK; } options = { method: 'POST', url: "" + this.host + PATH, auth: this.basicAuth, json: { title: title, owner_id: this.name, category: category, content: content } }; return request(options, function(err, res, body) { debuglog("err:" + err + ", res.statusCode:" + (res != null ? res.statusCode : "n/a") + ", body:%j", body); if (err != null) { return callback(err); } if (res.statusCode !== 200) { return callback(new Error("Network error, res.statusCode:" + res.statusCode)); } if (!((body != null) && body.success && body.ticket)) { return callback(new Error("Fail to create ticket:" + title + "#" + category + ", due to " + (body.error || "unknown error" + JSON.stringify(body)))); } if (body.ticket._id != null) { body.ticket.id = body.ticket._id; } return callback(null, body.ticket); }); }; return TicketManager; })(); module.exports = TicketManager; }).call(this);