run_in_background 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env ruby
2
+ # Wrapper script, that can receive commands from Windows Service Control and run user script,
3
+ # provided as it's argument.
4
+ # NOTE This wrapper script doesn't intended to be run from command line,
5
+ # rather be started and controlled by Windows Service Control.
6
+ # For more information see documentations and examples of win32-daemon library.
7
+ # usage: $0 <abs path to Ruby> <abs path to script> [blank-separated list of script's arguments]
8
+ # example: C:\ruby.exe c:\dev\daemon_wrapper c:\ruby.exe c:\dev\test_app 1000 -dbg=true
9
+
10
+ require 'win32/daemon'
11
+ #require 'win32/process'
12
+
13
+ include Win32
14
+
15
+ begin
16
+ require 'params'
17
+ require 'log'
18
+ rescue LoadError
19
+ $:.unshift(File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', 'lib'))
20
+ $:.unshift(File.join(File.dirname(File.expand_path(__FILE__)), '..', '..'))
21
+ require 'params'
22
+ require 'log'
23
+ end
24
+
25
+ BBFS::Params['log_write_to_console'] = false
26
+ # On WindowsXP log can be found under:
27
+ # C:/Documents and Settings/NetworkService/.bbfs/daemon_wrapper_<pid>.log
28
+ BBFS::Params['log_file_name'] = File.join(Dir.home, '.bbfs', "#{File.basename(__FILE__)}_#{Process.pid}.log")
29
+ BBFS::Log.init
30
+
31
+ class WrapperDaemon < Daemon
32
+ def service_main
33
+ BBFS::Log.info "Wrapper starts: #{ARGV.join(' ')}"
34
+ @pid = Process.spawn ARGV.join(' ')
35
+ BBFS::Log.debug1 "Wrapper inner app pid: #{@pid}"
36
+
37
+ while running?
38
+ begin
39
+ # checking whether inner application is alive
40
+ Process.kill 0, @pid
41
+ rescue Errno::ESRCH
42
+ # if inner application exited then stop the service
43
+ BBFS::Log.debug1 'Inner app no more running.'
44
+ service_stop
45
+ end
46
+ sleep 0.5
47
+ end
48
+ end
49
+
50
+ # checking whether process with given pid exists
51
+ def alive? pid
52
+ begin
53
+ Process.kill 0, pid
54
+ rescue Errno::ESRCH
55
+ return false
56
+ end
57
+ true
58
+ end
59
+
60
+ # kill inner application with given signal
61
+ # default signal is 9 - mercilessly kill the application
62
+ def kill_inner_app sig = 9
63
+ if alive? @pid # checking whether inner application is alive
64
+ begin
65
+ Process.kill sig, @pid # kill the inner application
66
+ rescue Exception => e
67
+ BBFS::Log.debug1 "kill inner app with #{sig} signal failed: #{e.message}"
68
+ sleep 1
69
+ Process.kill sig, @pid # second try to kill the inner application
70
+ end
71
+ if alive? @pid
72
+ BBFS::Log.debug1 'inner app still alive after kill. wait till exit...'
73
+ # may be redundant. children processes on Windows look be detached.
74
+ # also can be rather dangerous to use here.
75
+ pid_exit_stat = Process.waitpid2 @pid
76
+ BBFS::Log.debug1 "inner application exit status: #{pid_exit_stat}"
77
+ if alive? @pid
78
+ BBFS::Log.debug1 'inner app still alive after wait'
79
+ # this exception can be raised when using win32/process
80
+ raise 'inner app still alive after wait'
81
+ else
82
+ BBFS::Log.debug1 'inner app deleted after wait'
83
+ end
84
+ else
85
+ BBFS::Log.debug1 'inner app was killed'
86
+ end
87
+ else
88
+ # if got here then inner application is already exit. do nothing.
89
+ BBFS::Log.debug1 'inner app already deleted'
90
+ end
91
+ end
92
+
93
+ def service_stop
94
+ BBFS::Log.debug1 'service should be stopped'
95
+ [1, 2, 6, 15, 22, 9].each do |sig|
96
+ begin
97
+ BBFS::Log.debug1 "signal #{sig} sent to kill inner app"
98
+ kill_inner_app sig
99
+ rescue Exception => e
100
+ BBFS::Log.debug1 "#{e.message}"
101
+ next
102
+ end
103
+ BBFS::Log.debug1 'Wrapper was stopped'
104
+ # let log be written
105
+ sleep BBFS::Params['log_param_max_elapsed_time_in_seconds_from_last_flush'] + 0.5
106
+ exit!
107
+ end
108
+ BBFS::Log.error 'Failed to stop service'
109
+ sleep BBFS::Params['log_param_max_elapsed_time_in_seconds_from_last_flush'] + 0.5
110
+ #exit!
111
+ raise 'Failed to stop service'
112
+ end
113
+ end
114
+
115
+ WrapperDaemon.mainloop
116
+
@@ -1,5 +1,5 @@
1
1
  module BBFS
2
2
  module RunInBackground
3
- VERSION = "0.0.10"
3
+ VERSION = "0.0.11"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: run_in_background
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-17 00:00:00.000000000Z
12
+ date: 2013-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: log
16
- requirement: &19724160 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *19724160
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: params
27
- requirement: &19723740 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,7 +37,12 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *19723740
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  description: ! 'This library provides a basic cross-platform functionality to runarbitrary
37
47
  ruby scripts in background and control them.Supported platforms: Windows, Linux,
38
48
  Mac.'
@@ -44,6 +54,7 @@ extra_rdoc_files: []
44
54
  files:
45
55
  - lib/run_in_background.rb
46
56
  - lib/run_in_background/version.rb
57
+ - bin/run_in_background/daemon_wrapper
47
58
  - test/run_in_background/test_app
48
59
  - test/run_in_background/run_in_background_test.rb
49
60
  - ext/run_in_background/mkrf_conf.rb
@@ -67,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
78
  version: '0'
68
79
  requirements: []
69
80
  rubyforge_project:
70
- rubygems_version: 1.8.10
81
+ rubygems_version: 1.8.24
71
82
  signing_key:
72
83
  specification_version: 3
73
84
  summary: Cross-platform library for daemons management.