Module: ProcessStarter
- Defined in:
- lib/process_starter.rb
Constant Summary collapse
- WINRUN =
run in windows?
(RUBY_PLATFORM =~ /mswin|mingw/) ? true : false
- HAVE_SPAWN =
have Ruby 1.9 spawn method
defined?(spawn) ? true : false
- HAVE_POSIX_SPAWN =
loaded posix/spawn lib
defined?(POSIX::Spawn::spawn) ? true : false
Class Attribute Summary collapse
-
.allowed_methods ⇒ Object
Returns the value of attribute allowed_methods.
Class Method Summary collapse
-
.close_io(do_close = false, debug = false) ⇒ Object
find all opened io descriptors in current process without FD_CLOEXEC flag (auto close on exec/system/spawn) based on close_io from: daemons.rubyforge.org/Daemonize.html.
- .log(m) ⇒ Object
-
.start(s, allowed_methods = nil) ⇒ Object
Starts the process not waiting for it’s finish, so child process run in parralel not inheritting file descriptors, so it finds and closes all opened handels works in windows and linux.
Class Attribute Details
.allowed_methods ⇒ Object
Returns the value of attribute allowed_methods.
17 18 19 |
# File 'lib/process_starter.rb', line 17 def allowed_methods @allowed_methods end |
Class Method Details
.close_io(do_close = false, debug = false) ⇒ Object
find all opened io descriptors in current process without FD_CLOEXEC flag (auto close on exec/system/spawn) based on close_io from: daemons.rubyforge.org/Daemonize.html
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 |
# File 'lib/process_starter.rb', line 94 def self.close_io( do_close=false, debug = false ) close_them = [] # Make sure all input/output streams are closed puts "close_io: Part I" if debug # Part I: close all IO objects (except for STDIN/STDOUT/STDERR) ObjectSpace.each_object(IO) do |io| unless [STDIN, STDOUT, STDERR].include?(io) begin # puts "os #{io.to_i}, and=" # puts io.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC if (not io.closed?) and (io.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC) == 0 close_them << io.to_i puts "Closing #{io.inspect}" if debug io.close if do_close end rescue ::Exception end end end # puts close_them.inspect puts "close_io: Part II" if debug # Make sure all input/output streams are closed # Part II: close all file decriptors (except for STDIN/STDOUT/STDERR) ios = Array.new(8192) {|i| IO.for_fd(i) rescue nil}.compact ios.each do |io| puts io.to_i if debug next if io.fileno < 3 puts "checking flag" if debug r = begin; io.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC != 0 rescue false; end puts if debug # puts io.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC # next if (io.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC != 0) next if r # puts "adding" close_them << io.to_i if do_close puts "Closing #{io.inspect}" if debug io.close ### rescue nil -- DO NOT need to have rescue here, because if we cannot close - it is some kind of exception which need to be tracked end end # puts close_them.inspect close_them.uniq end |
.log(m) ⇒ Object
23 24 25 |
# File 'lib/process_starter.rb', line 23 def self.log(m) defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER.info(m) : puts(m) end |
.start(s, allowed_methods = nil) ⇒ Object
Starts the process not waiting for it’s finish, so child process run in parralel not inheritting file descriptors, so it finds and closes all opened handels works in windows and linux
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 |
# File 'lib/process_starter.rb', line 31 def self.start(s, allowed_methods = nil ) allowed_methods ||= self.allowed_methods log "********* ProcessStarter.start INVOKING EXTERNAL COMMAND #{s}, WINRUN IS #{WINRUN}, HAVE_POSIX_SPAWN IS #{HAVE_POSIX_SPAWN}, HAVE_SPAWN is #{HAVE_SPAWN}, allowed_methods are #{allowed_methods.inspect}" if WINRUN # if s.index("'") # s = s.gsub("'","\\'") # end @@have_psexec ||= allowed_methods.include?(:psexec) ? ((`psexec /?` rescue "") =~ /computer/ ? 2 : 1) : -1 if @@have_psexec == 2 and allowed_methods.include?(:psexec) log "starting using psexec" # -d = detach (nowait) # -w = current working directory r = system("psexec -d -w #{Dir.pwd} #{s}") else log "starting using START cmd /c (inherits descriptors (?) - THIS IS BAD, PLEASE DOWNLOAD and COPY PSEXEC TO AVAILABLE 'PATH')" # CANNOT call close_io here, because it will close descriptors in current (parent) process # close_io(true,true) system("start /b cmd /c #{s}") end else log "Executing in Linux style" # fix "s" for stdin, stdout, stderr nilport = WINRUN ? "NUL" : "/dev/null" # not specified stdin? if not s =~ /</ s = s + " <#{nilport}" end # not specified both stdout and stderr? if not s =~ />/ s = s + " >#{nilport} 2>#{nilport}" end # not specified stderr? if not s =~ /2>/ s = s + " 2>#{nilport}" end # we omit the case: "program 2>nil", which specifies that stdout is not redirected, which means that # if user specify 2>, he is clever, and has to specify 1> too. if HAVE_POSIX_SPAWN and allowed_methods.include?(:posix_spawn) close_them = Hash[ close_io().map {|x| [x, :close]} ] log "POSIX::Spawn::spawn with close descriptors: #{close_them.inspect}" Process.detach POSIX::Spawn::spawn( s, close_them) elsif HAVE_SPAWN and allowed_methods.include?(:spawn) # we hope on http://www.ruby-doc.org/core-1.9.3/Process.html#method-c-spawn # [close_others => true] option # TODO: check it log "Ruby 1.9 spawn" Process.detach spawn( s ) else log "fork{ close_io; exec }" Process.detach fork { close_io(true) exec(s) } end end end |