nodo 1.5.5 → 1.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -1
- data/lib/nodo/core.rb +15 -5
- data/lib/nodo/nodo.js +13 -10
- data/lib/nodo/railtie.rb +2 -0
- data/lib/nodo/version.rb +1 -1
- data/lib/nodo.rb +4 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 650d220075d68a34268a03f2e68587b1caab13f47707c8b44f7cb7d05f5a5588
|
4
|
+
data.tar.gz: 0b73f0cd827a1ab364975d3559f64f2d9c49afe265759715f92d23f133bab10c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c1049cd1e574c0a7ec503ef01ae735d7b3ca33d21695a1a76c4c8f5c92f0914db58b59136a7ea0bcf17b83f9c6776817b8b4890061e6c442e414c5476d80037
|
7
|
+
data.tar.gz: d5fd39f74f9ebfabaec0463dbf7234e6da34a73b3167f2580fb6ac276f52f6aef433156ef0917b4a5c9c344f1dfc6d0f28e7c1af1bfe329829c5c87068d932dd
|
data/README.md
CHANGED
@@ -185,6 +185,29 @@ class Foo < Nodo::Core
|
|
185
185
|
JS
|
186
186
|
end
|
187
187
|
|
188
|
-
|
188
|
+
Foo.new.sleep(2)
|
189
189
|
=> Nodo::TimeoutError raised
|
190
190
|
```
|
191
|
+
|
192
|
+
### Logging
|
193
|
+
|
194
|
+
By default, JS errors will be logged to `STDOUT`.
|
195
|
+
|
196
|
+
To set a custom logger:
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
Nodo.logger = Logger.new('nodo.log')
|
200
|
+
```
|
201
|
+
|
202
|
+
In Rails applications, `Rails.logger` will automatically be set.
|
203
|
+
|
204
|
+
### Debugging
|
205
|
+
|
206
|
+
To get verbose debug output, set
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
Nodo.debug = true
|
210
|
+
```
|
211
|
+
|
212
|
+
before instantiating any worker instances. The debug mode will be active during
|
213
|
+
the current process run.
|
data/lib/nodo/core.rb
CHANGED
@@ -63,10 +63,12 @@ module Nodo
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def function(name, _code = nil, timeout: 60, code: nil)
|
66
|
-
raise ArgumentError, "reserved method name #{name.inspect}" if Nodo::Core.method_defined?(name)
|
66
|
+
raise ArgumentError, "reserved method name #{name.inspect}" if Nodo::Core.method_defined?(name) || name.to_s == DEFINE_METHOD
|
67
67
|
code = (code ||= _code).strip
|
68
68
|
raise ArgumentError, 'function code is required' if '' == code
|
69
|
-
|
69
|
+
loc = caller_locations(1, 1)[0]
|
70
|
+
source_location = "#{loc.path}:#{loc.lineno}: in `#{name}'"
|
71
|
+
self.functions = functions.merge(name => Function.new(name, _code || code, source_location, timeout))
|
70
72
|
define_method(name) { |*args| call_js_method(name, args) }
|
71
73
|
end
|
72
74
|
|
@@ -94,7 +96,7 @@ module Nodo
|
|
94
96
|
nodo.core.close(() => { process.exit(0) });
|
95
97
|
};
|
96
98
|
|
97
|
-
process.on('SIGINT', shutdown);
|
99
|
+
// process.on('SIGINT', shutdown);
|
98
100
|
process.on('SIGTERM', shutdown);
|
99
101
|
|
100
102
|
nodo.core.run(socket);
|
@@ -146,6 +148,11 @@ module Nodo
|
|
146
148
|
self.class.clsid
|
147
149
|
end
|
148
150
|
|
151
|
+
def log_exception(e)
|
152
|
+
return unless logger = Nodo.logger
|
153
|
+
logger.error "\n#{e.class} (#{e.message}):\n\n#{e.backtrace.join("\n")}"
|
154
|
+
end
|
155
|
+
|
149
156
|
def ensure_process_is_spawned
|
150
157
|
return if node_pid
|
151
158
|
spawn_process
|
@@ -160,6 +167,7 @@ module Nodo
|
|
160
167
|
def spawn_process
|
161
168
|
@@tmpdir = Pathname.new(Dir.mktmpdir('nodo'))
|
162
169
|
env = Nodo.env.merge('NODE_PATH' => Nodo.modules_root.to_s)
|
170
|
+
env['NODO_DEBUG'] = '1' if Nodo.debug
|
163
171
|
@@node_pid = Process.spawn(env, Nodo.binary, '-e', self.class.generate_core_code, '--', socket_path.to_s, err: :out)
|
164
172
|
at_exit do
|
165
173
|
Process.kill(:SIGTERM, node_pid) rescue Errno::ECHILD
|
@@ -207,9 +215,11 @@ module Nodo
|
|
207
215
|
def handle_error(response, function)
|
208
216
|
if response.body
|
209
217
|
result = parse_response(response)
|
210
|
-
|
218
|
+
error = JavaScriptError.new(result['error'], function) if result.is_a?(Hash) && result.key?('error')
|
211
219
|
end
|
212
|
-
|
220
|
+
error ||= CallError.new("Node returned #{response.code}")
|
221
|
+
log_exception(error)
|
222
|
+
raise error
|
213
223
|
end
|
214
224
|
|
215
225
|
def parse_response(response)
|
data/lib/nodo/nodo.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module.exports = (function() {
|
2
2
|
const DEFINE_METHOD = '__nodo_define_class__';
|
3
|
+
const DEBUG = process.env.NODO_DEBUG;
|
3
4
|
|
4
5
|
const vm = require('vm');
|
5
6
|
const http = require('http');
|
@@ -24,7 +25,7 @@ module.exports = (function() {
|
|
24
25
|
function respond_with_error(res, code, name) {
|
25
26
|
res.statusCode = code;
|
26
27
|
const rendered = render_error(name);
|
27
|
-
|
28
|
+
debug(`Error ${code} ${rendered}`);
|
28
29
|
res.end(rendered, 'utf8');
|
29
30
|
}
|
30
31
|
|
@@ -35,22 +36,24 @@ module.exports = (function() {
|
|
35
36
|
if (start) {
|
36
37
|
timing = ` in ${(performance.now() - start).toFixed(2)}ms`;
|
37
38
|
}
|
38
|
-
|
39
|
+
debug(`Completed 200 OK${timing}\n`);
|
39
40
|
}
|
40
41
|
|
41
|
-
function
|
42
|
-
|
43
|
-
|
42
|
+
function debug(message) {
|
43
|
+
if (DEBUG) {
|
44
|
+
// fs.appendFileSync('log/nodo.log', `${message}\n`);
|
45
|
+
console.log(`[Nodo] ${message}`);
|
46
|
+
}
|
44
47
|
}
|
45
48
|
|
46
49
|
const core = {
|
47
50
|
run: (socket) => {
|
48
|
-
|
51
|
+
debug('Starting up...');
|
49
52
|
server = http.createServer((req, res) => {
|
50
53
|
const start = performance.now();
|
51
54
|
|
52
55
|
res.setHeader('Content-Type', 'application/json');
|
53
|
-
|
56
|
+
debug(`${req.method} ${req.url}`);
|
54
57
|
|
55
58
|
if (req.method !== 'POST' || !req.url.startsWith('/')) {
|
56
59
|
return respond_with_error(res, 405, 'Method Not Allowed');
|
@@ -103,12 +106,12 @@ module.exports = (function() {
|
|
103
106
|
|
104
107
|
//server.maxConnections = 64;
|
105
108
|
server.listen(socket, () => {
|
106
|
-
|
109
|
+
debug(`server ready, listening on ${socket} (max connections: ${server.maxConnections})`);
|
107
110
|
});
|
108
111
|
},
|
109
112
|
|
110
113
|
close: (finalizer) => {
|
111
|
-
|
114
|
+
debug("Shutting down");
|
112
115
|
if (!closing) {
|
113
116
|
closing = true;
|
114
117
|
server.close(finalizer);
|
@@ -116,5 +119,5 @@ module.exports = (function() {
|
|
116
119
|
}
|
117
120
|
};
|
118
121
|
|
119
|
-
return { core: core,
|
122
|
+
return { core: core, debug: debug };
|
120
123
|
})();
|
data/lib/nodo/railtie.rb
CHANGED
data/lib/nodo/version.rb
CHANGED
data/lib/nodo.rb
CHANGED
@@ -3,16 +3,19 @@ require 'json'
|
|
3
3
|
require 'fileutils'
|
4
4
|
require 'tmpdir'
|
5
5
|
require 'tempfile'
|
6
|
+
require 'logger'
|
6
7
|
require 'socket'
|
7
8
|
require 'forwardable'
|
8
9
|
|
9
10
|
module Nodo
|
10
11
|
class << self
|
11
|
-
attr_accessor :modules_root, :env, :binary
|
12
|
+
attr_accessor :modules_root, :env, :binary, :logger, :debug
|
12
13
|
end
|
13
14
|
self.modules_root = './node_modules'
|
14
15
|
self.env = {}
|
15
16
|
self.binary = 'node'
|
17
|
+
self.logger = Logger.new(STDOUT)
|
18
|
+
self.debug = false
|
16
19
|
end
|
17
20
|
|
18
21
|
require_relative 'nodo/version'
|