This repository has been archived by the owner on Nov 30, 2018. It is now read-only.
/
wpp.js
151 lines (122 loc) · 3.97 KB
/
wpp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";
var express = require('express'),
app = express(),
port = 8283,
server = app.listen(port),
BodyParser = require('body-parser'),
assert = require('assert'),
v8 = require('v8'),
Logger = require('./components/loggers/Logger'),
WebpageProcessor = require('./components/WebpageProcessor'),
PhantomJSBalancer = require('./components/phantom/Balancer'),
PhantomJSSettings = require('./components/phantom/Settings'),
phbalancer = new PhantomJSBalancer(),
jobs_done = 0,
jobs_total = 0;
require('epipebomb')();
process.env.UV_THREADPOOL_SIZE = 1024;
process.on('exit', function() {
phbalancer.free();
});
process.on('SIGINT', function(code) {
process.exit();
});
process.on('SIGTERM', function(code) {
process.exit();
});
console.log('Welcome to Webpage Processor Utility.\n'
+ 'WPP server listening on port', port);
app.use(BodyParser.json());
app.post('/', function(req, res) {
let logger = new Logger(),
logger_conf;
jobs_total++;
logger.info('New job received at ' + new Date());
let heap_stats = v8.getHeapStatistics();
Object.keys(heap_stats).map(function(key) {
heap_stats[key] = heap_stats[key] / 1000 + ' Mb';
});
logger.info(heap_stats);
let handle_exception = function (description, exc, req, res) {
jobs_done++;
res.json({}).status(500);
logger.error(description + ':', exc);
logger.info('Job ' + jobs_done + '/' + jobs_total + ' done: FAILURE');
};
let actions;
try {
actions = req.body.actions;
assert(actions);
} catch (exc) {
handle_exception('Cannot get actions', exc, req, res);
return;
}
if (!logger.url) {
try {
logger.url = actions[0].data.url;
assert(logger.url);
} catch (exc) {
logger.info('Cannot get task URL');
}
}
let proxy;
try {
proxy = req.body.proxy;
assert(proxy);
} catch (exc) {
logger.info('Proxy disabled');
}
let phantom_settings = new PhantomJSSettings();
try {
if (proxy) {
process.env[proxy.type.toUpperCase() + '_PROXY'] = proxy[proxy.type];
phantom_settings.set('--proxy-type', proxy.type);
phantom_settings.set('--proxy', proxy[proxy.type]);
}
} catch (exc) {
handle_exception('Cannot initialize phantomjs settings', exc, req, res);
return;
}
let little_horse_ready;
try {
little_horse_ready = phbalancer.acquire_phantom_instance(phantom_settings);
} catch (exc) {
handle_exception('Cannot get phantom instance', exc, req, res);
return;
}
little_horse_ready.then(function(little_horse) {
let phantom = little_horse.phantom,
wpp;
try {
wpp = new WebpageProcessor(phantom, logger);
} catch (exc) {
little_horse.release();
handle_exception('Cannot create webpage processor', exc, req, res);
return;
}
wpp.defaults = req.body.defaults || {};
try {
wpp.run(actions, function(exc, data) {
if (exc) {
handle_exception('Webpage processor exception', exc, req, res);
little_horse.release();
return;
}
try {
res.json(data);
jobs_done++;
logger.info('Job ' + jobs_done + '/' + jobs_total + ' done: SUCCESS');
} catch (exc) {
handle_exception('Failed to build response', exc, req, res);
} finally {
little_horse.release();
}
});
} catch (exc) {
handle_exception('Task failed', exc, req, res);
little_horse.release();
}
}, function(exc) {
handle_exception('Cannot get phantom instance', exc, req, res);
});
});