node_task 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79735e5f4ab699858eef9104cf225f8e4e1a4f3e
4
- data.tar.gz: d7bd36c0567068bcce877b3a9eb684e553d2cefa
3
+ metadata.gz: 251a401a6d440f9a5fd7399519c12bef82ae32ff
4
+ data.tar.gz: a159f15a8e6ad3f490b9bfb3fcc3c334170105bd
5
5
  SHA512:
6
- metadata.gz: 27f366f9e2b2269b46eb86b6065e5cd6872924bc1299d4116b70713a04eb87f439e20025eb648cb63db301a422b9b93b5caffec0b73f1d2885010970ed117f90
7
- data.tar.gz: cd225c6b382359ad8b550bde59043affc81f4e566d6978440844c008b0e79c8a99bee4a6411856f452b3bebe98f41f906c525110c2a9f65d72ac52aceda544e9
6
+ metadata.gz: fc9a7866fd5eaddafbaec94ed2c7b892c8125a65d086705c18a8584069cbaaa9b6d4022dc221b7793dbac9ce44e68d7f4ba8fb6166cd3a86aef28d6aa7ba76d6
7
+ data.tar.gz: 8d0b4430317f4af05480160b6270ea48815efa43ff5a1d73759ac54c97f4eacd991a02cafc67898e301eae1c4482ae91e6b6a930ded18fa911da08285e5bb677
@@ -3,6 +3,9 @@ var path = require('path')
3
3
  var net = require('net')
4
4
  var ndjson = require('ndjson')
5
5
 
6
+ var parentCheckInterval = parseInt(process.env.NODE_TASK_PARENT_CHECK_INTERVAL, 10) || 1000;
7
+ var parentPid = parseInt(process.env.NODE_TASK_DAEMON_ID, 10);
8
+
6
9
  var workingDir = path.resolve(process.env.NODE_TASK_CWD || __dirname)
7
10
  var errorLogPath = path.join(workingDir, 'ruby_node_task-error.log')
8
11
  var debugLogPath = path.join(workingDir, 'ruby_node_task-debug.log')
@@ -24,6 +27,17 @@ var server = net.createServer(onClientConnect)
24
27
  process.on('exit', onExit)
25
28
  process.on('uncaughtException', onUncaughtException)
26
29
 
30
+ setInterval(function() {
31
+ try {
32
+ process.kill(parentPid, 0);
33
+ } catch (err) {
34
+ if (err.code === 'ESRCH') {
35
+ logger.debug('parent '+parentPid+' is no longer alive, exiting')
36
+ process.exit()
37
+ }
38
+ }
39
+ }, parentCheckInterval);
40
+
27
41
  // the important part
28
42
  function onClientConnect(socket) {
29
43
  logger.debug('client connected')
@@ -44,12 +58,6 @@ function onClientConnect(socket) {
44
58
  return sendError(new Error('only one task can be run per connection'))
45
59
  }
46
60
 
47
- if (msg.status) {
48
- return server.getConnections(function(err, count) {
49
- sendMsg({clients: count-1}) // minus this connection
50
- })
51
- }
52
-
53
61
  if (!msg.task) {
54
62
  return sendError(new Error('msg.task not defined'))
55
63
  }
@@ -9,13 +9,18 @@ class NodeTask
9
9
  START_MAX_RETRIES = 1
10
10
 
11
11
  class Error < StandardError
12
- def initialize(js_error)
13
- @js_error = js_error
12
+ attr_accessor :js_error
13
+
14
+ def initialize(original_js_error)
15
+ js_error = original_js_error
16
+
14
17
  super(js_error[:message])
15
18
  end
16
19
 
17
20
  def to_s
18
- @js_error[:stack] || @js_error[:message]
21
+ super unless js_error
22
+
23
+ js_error[:stack] || js_error[:message]
19
24
  end
20
25
  end
21
26
 
@@ -64,7 +69,7 @@ class NodeTask
64
69
  end
65
70
 
66
71
  def daemon_start_script
67
- File.join(gem_dir, 'index.js').to_s
72
+ File.join(gem_dir, 'nodeTask.js').to_s
68
73
  end
69
74
 
70
75
  # get configured daemon controller for daemon, and start it
@@ -155,36 +160,30 @@ class NodeTask
155
160
  result
156
161
  end
157
162
 
158
- # number of connections active to the daemon
159
- def clients_active
160
- socket = _make_connection # might fail
161
- message = {status: true} # special message type
162
- result = request(socket, message)
163
- return 0 if result.nil?
164
- result[:clients]
163
+ def alive?
164
+ current_pid = nil
165
+ alive = false
166
+ if @controller
167
+ begin
168
+ current_pid = @controller.pid
169
+ rescue Errno::ENOENT
170
+ end
171
+ end
172
+ if current_pid
173
+ begin
174
+ Process.getpgid(current_pid)
175
+ alive = true
176
+ rescue Errno::ESRCH
177
+ end
178
+ end
179
+ alive
165
180
  end
166
181
 
167
- # stop the daemon if no one else is using it
182
+ # stop the daemon
168
183
  def release
169
- begin
170
- # this doesn't really work as intended right now
171
- # as no connections are maintained when tasks aren't running
172
- return if clients_active > 0
173
- rescue Errno::ENOENT => e
174
- # socket file probably doesn't exist
175
- # maybe we should just return here?
176
- end
184
+ return unless alive?
177
185
 
178
- pid = nil
179
- begin
180
- pid = @controller.pid
181
- rescue Errno::ENOENT => e
182
- # presumably no pid file exists and the daemon is not running
183
- logger.debug "daemon already stopped"
184
- return
185
- end
186
-
187
- logger.debug "stopping daemon #{pid}"
186
+ logger.debug "stopping daemon #{@controller.pid}"
188
187
  @controller.stop
189
188
 
190
189
  begin
@@ -194,6 +193,21 @@ class NodeTask
194
193
  end
195
194
  end
196
195
 
196
+ def daemon_env
197
+ {
198
+ "NODE_TASK_SOCK_PATH" => socket_path,
199
+ "NODE_TASK_CWD" => working_dir,
200
+ "NODE_TASK_DAEMON_ID" => daemon_identifier,
201
+ "NODE_TASK_PARENT_PID" => Process.pid.to_s,
202
+ "NODE_TASK_PARENT_CHECK_INTERVAL" => parent_check_interval.to_s,
203
+ "NODE_ENV" => ENV["RACK_ENV"],
204
+ }
205
+ end
206
+
207
+ def parent_check_interval
208
+ 1000
209
+ end
210
+
197
211
  private
198
212
 
199
213
  def _make_connection
@@ -212,12 +226,11 @@ class NodeTask
212
226
  # - some server errors not reported
213
227
  def _make_daemon_controller
214
228
  logger.debug "socket_path #{socket_path}"
229
+ logger.debug "starting #{node_command} #{daemon_start_script}"
215
230
 
216
- puts "starting #{node_command} #{daemon_start_script}"
217
231
  controller = DaemonController.new(
218
232
  identifier: daemon_identifier,
219
233
  start_command: "#{node_command} #{daemon_start_script}",
220
- # ping_command: [:unix, socket_path],
221
234
  ping_command: Proc.new{
222
235
  begin
223
236
  _make_connection
@@ -228,19 +241,12 @@ class NodeTask
228
241
  },
229
242
  pid_file: pid_file,
230
243
  log_file: error_log_file,
231
- env: {
232
- "NODE_TASK_SOCK_PATH" => socket_path,
233
- "NODE_TASK_CWD" => working_dir,
234
- "NODE_TASK_DAEMON_ID" => daemon_identifier,
235
- "NODE_ENV" => ENV["RACK_ENV"],
236
- },
244
+ env: daemon_env,
237
245
  log_file_activity_timeout: RESPONSE_TIMEOUT,
238
246
  start_timeout: RESPONSE_TIMEOUT,
239
247
  daemonize_for_me: true,
240
248
  )
241
249
 
242
- at_exit { release }
243
-
244
250
  controller
245
251
  end
246
252
  end
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "ruby_node_task",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "nodeTask.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"
8
8
  },
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: node_task
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Friend
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-30 00:00:00.000000000 Z
11
+ date: 2015-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: daemon_controller
@@ -37,7 +37,7 @@ extensions: []
37
37
  extra_rdoc_files: []
38
38
  files:
39
39
  - lib/node_task.rb
40
- - lib/node_task/index.js
40
+ - lib/node_task/nodeTask.js
41
41
  - lib/node_task/node_modules/ndjson/cli.js
42
42
  - lib/node_task/node_modules/ndjson/collaborators.md
43
43
  - lib/node_task/node_modules/ndjson/index.js