Skip to content

Commit 885ec0d

Browse files
committed
Merge pull request #44 from AnyFetch/opbeat-in-childs
use opbeat in childs
2 parents 359ab7a + 96bff8d commit 885ec0d

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

lib/helpers/child-process.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,31 @@
99
*/
1010

1111
var crypto = require('crypto');
12-
var async = require("async");
12+
var async = require('async');
1313
var fs = require('fs');
1414
var request = require('supertest');
1515
var url = require('url');
1616
var rarity = require('rarity');
1717

18+
var logError = require('../utils').logError;
19+
1820
var filePath;
1921
var path;
2022

23+
2124
process.on('message', function(task) {
2225
filePath = task.file_path;
2326
path = '/tmp/AFH-' + crypto.randomBytes(20).toString('hex');
2427
var hydrate = require(task.functionPath);
28+
29+
logError.config = task.opbeatConfig;
30+
31+
/* istanbul ignore next */
32+
if(task.opbeatConfig && task.opbeatConfig.secretToken) {
33+
var opbeat = require('opbeat');
34+
logError.opbeat = opbeat(task.opbeatConfig);
35+
}
36+
2537
async.waterfall([
2638
/**
2739
* Download the file from task.file_path, store it in a temporary file if there is file_path
@@ -70,10 +82,24 @@ process.on('message', function(task) {
7082
}
7183
],
7284
function(err, changes) {
73-
process.send({
74-
err: err,
75-
changes: changes
76-
});
85+
if(err) {
86+
var extra = JSON.parse(JSON.stringify(task));
87+
extra.fromChild = true;
88+
extra.changes = changes;
89+
logError(err, extra);
90+
process.send({
91+
err: {
92+
message: err.toString(),
93+
_hydrationError: err._hydrationError
94+
},
95+
changes: changes
96+
});
97+
}
98+
else {
99+
process.send({
100+
changes: changes
101+
});
102+
}
77103
});
78104
});
79105

lib/helpers/hydrater.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var HydrationError = lib.HydrationError;
1919
var logError = require('../utils').logError;
2020

2121

22-
module.exports = function(hydraterFunction, childs) {
22+
module.exports = function(hydraterFunction, childs, opbeatConfig) {
2323
/**
2424
* Handle a hydration task:
2525
* - Download the file
@@ -75,8 +75,8 @@ module.exports = function(hydraterFunction, childs) {
7575
};
7676
cleaner.called = false;
7777

78-
child.process.on('error', function(exitCode) {
79-
cleaner(new HydrationError("Wild error appeared while spawning child. Exit code:" + exitCode));
78+
child.process.on('error', function(error) {
79+
cleaner(new HydrationError("Wild error appeared while spawning child. error:" + error));
8080
});
8181

8282
child.process.stderr.on('readable', function() {
@@ -119,17 +119,23 @@ module.exports = function(hydraterFunction, childs) {
119119
document: task.document,
120120
changes: lib.defaultChanges(),
121121
options: options,
122+
opbeatConfig: opbeatConfig
122123
});
123124

124125
child.process.on('message', function(res) {
125126
var err = res.err;
126127
// If the function replied with an "HydrationError", we'll wrap this in a nicely formatted document
127128
// and stop the error from bubbling up.
128-
if(err && err._hydrationError) {
129-
res.changes = {};
130-
res.changes.hydration_errored = true;
131-
res.changes.hydration_error = err.message;
132-
err = null;
129+
if(err) {
130+
if(err._hydrationError) {
131+
res.changes = {};
132+
res.changes.hydration_errored = true;
133+
res.changes.hydration_error = err.message;
134+
err = null;
135+
}
136+
else {
137+
err = err.message ? err.message : err;
138+
}
133139
}
134140
cleaner(err, res.changes);
135141
});

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports.createServer = function(config) {
5050

5151
var childs = new Childs(concurrency, tasksPerProcess);
5252

53-
var hydraterHelper = require('./helpers/hydrater.js')(config.hydrater_function, childs);
53+
var hydraterHelper = require('./helpers/hydrater.js')(config.hydrater_function, childs, config.opbeat);
5454

5555
var server = restify.createServer({
5656
log: log

test/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('Errors', function() {
114114
fakeApi.patch('/result', function(req, res, next) {
115115
res.send(204);
116116
next();
117-
if(req.params.hydration_errored && req.params.hydration_error === "hydrater errored") {
117+
if(req.params.hydration_errored && req.params.hydration_error === "HydrationError: hydrater errored") {
118118
done();
119119
}
120120
else {

test/hydraters/errored-hydrater.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
var anyfetchFileHydrater = require('../../lib/');
3+
var HydrationError = require('../../lib/').HydrationError;
44

55
module.exports = function erroredHydrater(path, document, changes, cb) {
6-
cb(new anyfetchFileHydrater.HydrationError("hydrater errored"));
6+
cb(new HydrationError("hydrater errored"));
77
};

0 commit comments

Comments
 (0)