UNPKG

5.03 kBJavaScriptView Raw
1"use strict";
2
3var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
8
9function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
10
11// Generated by CoffeeScript 2.5.1
12(function () {
13 var ENABLE_EVENTS,
14 EventEmitter,
15 NonRetryable,
16 Processor,
17 Promise,
18 _,
19 newrelic,
20 uuid,
21 boundMethodCheck = function boundMethodCheck(instance, Constructor) {
22 if (!(instance instanceof Constructor)) {
23 throw new Error('Bound instance method accessed before binding');
24 }
25 };
26
27 _ = require("lodash");
28
29 NonRetryable = require("./exceptions/non.retryable");
30
31 EventEmitter = require("events");
32
33 Promise = require("bluebird");
34
35 uuid = require("uuid/v4");
36
37 newrelic = _.once(function () {
38 return require("newrelic");
39 });
40
41 ENABLE_EVENTS = process.env.ENABLE_EVENTS !== "false";
42
43 module.exports = Processor = function (_EventEmitter) {
44 _inherits(Processor, _EventEmitter);
45
46 function Processor(_ref) {
47 var source = _ref.source,
48 runner = _ref.runner,
49 timeout = _ref.timeout,
50 apm = _ref.apm;
51
52 _classCallCheck(this, Processor);
53
54 var _this = _possibleConstructorReturn(this, (Processor.__proto__ || Object.getPrototypeOf(Processor)).call(this));
55
56 _this.process = _this.process.bind(_this);
57 _this._emitEvent = _this._emitEvent.bind(_this);
58 _this.source = source;
59 _this.runner = runner;
60 _this.timeout = timeout;
61 _this.apm = apm;
62 return _this;
63 }
64
65 _createClass(Processor, [{
66 key: "process",
67 value: function process(context, raw) {
68 var _this2 = this;
69
70 var $promise, execute, id, notification;
71 boundMethodCheck(this, Processor);
72 id = uuid();
73 notification = this.source.newNotification({
74 context: context,
75 id: id,
76 message: raw
77 });
78 this._emitEvent("started", { context: context, id: id, notification: notification });
79 $promise = Promise.method(this.runner)(notification, context);
80 if (this.timeout != null) {
81 $promise = $promise.timeout(this.timeout, "processor timeout");
82 }
83 execute = function execute() {
84 var ref;
85 if (!((ref = _this2.apm) != null ? ref.active : void 0)) {
86 return $promise;
87 }
88 return newrelic().startBackgroundTransaction(_this2.apm.transactionName, _this2.apm.group, function () {
89 return $promise.tapCatch(function (err) {
90 return newrelic().noticeError(new Error(JSON.stringify(_.omit(err.detail, "request.auth.pass"))));
91 });
92 });
93 };
94 execute().tap(function () {
95 return _this2._emitEvent("successful", { context: context, id: id, notification: notification });
96 }).catch(function (error) {
97 if (!(error instanceof NonRetryable)) {
98 throw error;
99 }
100 return _this2._emitEvent("unsuccessful_non_retryable", { context: context, id: id, notification: notification, error: error });
101 }).tapCatch(function (error) {
102 return _this2._emitEvent("unsuccessful", { context: context, id: id, notification: notification, error: error });
103 }).finally(function () {
104 return _this2._emitEvent("finished", { context: context, id: id, notification: notification });
105 }).asCallback(context.done);
106 }
107 }, {
108 key: "_emitEvent",
109 value: function _emitEvent(eventName, value) {
110 boundMethodCheck(this, Processor);
111 if (ENABLE_EVENTS) {
112 return this.emit(eventName, value);
113 }
114 }
115 }]);
116
117 return Processor;
118 }(EventEmitter);
119}).call(undefined);
\No newline at end of file