ztk 3.2.3 → 3.2.4

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: ffb1bbfa5948f9298b26619d12e86a680366e12d
4
- data.tar.gz: c1c6fe133bee76e3c5fe69b4d081639e00f6fe40
3
+ metadata.gz: d760f1f213971dc97e6eeef8759269b2f73572e3
4
+ data.tar.gz: a50b3d2e09579bb30f39a1bcb1c9af5d2a433248
5
5
  SHA512:
6
- metadata.gz: 4adcc39b3c753ffef8b5a1fa2acbbc4546f8c847a65ec17c6081125c4e1cd8fdcb31650e2d4d0430406af9dfb0c4c9a0fbcc6f1fd00ef78d9b29b1822b484ce3
7
- data.tar.gz: 4a243a18dc8d59269e454c0f6f1a75eeee71cfd1816757cce87799a0953c2255cd658cc3919c2b602d08625bb4aa6827f0ba155f51c0179ed3e294e7ef2c9afc
6
+ metadata.gz: 571f41f9f17350346888a4eb77e7434374599557bb11a0c18d359c16f14942712fbc21f03d9af4d75c0735a2802010aa0040af8558a7c0a2eb8631b4e4141dc7
7
+ data.tar.gz: 80c28ff3598abeb9a3374afc3d9b29fb7a34f729d3f6f7e7ed2a985769bb08510daa2db5316e658d63d85082ca482baa2e7618e710903f7cc3ff97ae0438b7c5
data/lib/ztk/parallel.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'base64'
2
+ require 'timeout'
2
3
 
3
4
  module ZTK
4
5
 
@@ -47,6 +48,7 @@ module ZTK
47
48
  class Parallel < ZTK::Base
48
49
 
49
50
  class Break < ParallelError; end
51
+ class Timeout < ParallelError; end
50
52
 
51
53
  # Tests if we can marshal an exception via the results; otherwise creates
52
54
  # an exception we can marshal.
@@ -65,7 +67,7 @@ module ZTK
65
67
  when /darwin/ then
66
68
  (%x( sysctl hw.ncpu ).strip.split(':').last.strip.to_i - 1)
67
69
  when /linux/ then
68
- (%x( grep -c processor /proc/cpuinfo ).strip.strip.to_i - 1)
70
+ (%x( grep -c processor /proc/cpuinfo ).strip.to_i - 1)
69
71
  end
70
72
 
71
73
  # Platforms memory capacity in bytes
@@ -76,6 +78,14 @@ module ZTK
76
78
  (%x( grep MemTotal /proc/meminfo ).strip.split[1].to_i * 1024)
77
79
  end
78
80
 
81
+ # Child process timeout in seconds; <= 0 to disable
82
+ DEFAULT_CHILD_TIMEOUT = 0
83
+
84
+ # Which signals we want to trap for child signaling
85
+ trapped_signals = %w( term int hup )
86
+ trapped_signals << "kill" if RUBY_VERSION < "2.2.0"
87
+ TRAPPED_SIGNALS = trapped_signals.map(&:upcase)
88
+
79
89
  # Result Set
80
90
  attr_accessor :results
81
91
 
@@ -86,7 +96,8 @@ module ZTK
86
96
  def initialize(configuration={})
87
97
  super({
88
98
  :max_forks => MAX_FORKS,
89
- :raise_exceptions => true
99
+ :raise_exceptions => true,
100
+ :child_timeout => DEFAULT_CHILD_TIMEOUT
90
101
  }, configuration)
91
102
 
92
103
  (config.max_forks < 1) and log_and_raise(ParallelError, "max_forks must be equal to or greater than one!")
@@ -95,16 +106,13 @@ module ZTK
95
106
  @results = Array.new
96
107
  GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
97
108
 
98
- trapped_signals = %w( term int hup )
99
- trapped_signals << "kill" if RUBY_VERSION < "2.2.0"
100
-
101
- trapped_signals.map(&:upcase).each do |signal|
109
+ TRAPPED_SIGNALS.each do |signal|
102
110
  Signal.trap(signal) do
103
111
  $stderr.puts("SIG#{signal} received by PID##{Process.pid}; signaling child processes...")
104
112
 
105
113
  signal_all(signal)
106
114
 
107
- exit!(1)
115
+ (signal == "INT") or exit!(1)
108
116
  end
109
117
  end
110
118
  end
@@ -129,37 +137,45 @@ module ZTK
129
137
 
130
138
  config.before_fork and config.before_fork.call(Process.pid)
131
139
  pid = Process.fork do
132
-
133
- config.after_fork and config.after_fork.call(Process.pid)
134
-
135
- parent_writer.close
136
- parent_reader.close
137
-
138
- data = nil
139
140
  begin
140
- data = block.call
141
- rescue Exception => e
142
- config.ui.logger.fatal { e.message }
143
- e.backtrace.each do |line|
144
- config.ui.logger << line
145
- config.ui.logger << "\n"
141
+ TRAPPED_SIGNALS.each { |signal| Signal.trap(signal) { } }
142
+
143
+ parent_writer.close
144
+ parent_reader.close
145
+
146
+ ::Timeout.timeout(config.child_timeout, ZTK::Parallel::Timeout) do
147
+ config.after_fork and config.after_fork.call(Process.pid)
148
+
149
+ data = nil
150
+ begin
151
+ data = block.call
152
+ rescue Exception => e
153
+ config.ui.logger.fatal { e.message }
154
+ e.backtrace.each do |line|
155
+ config.ui.logger << "#{line}\n"
156
+ end
157
+ data = ExceptionWrapper.new(e)
158
+ end
159
+
160
+ if !data.nil?
161
+ config.ui.logger.debug { "write(#{data.inspect})" }
162
+ begin
163
+ child_writer.write(Base64.encode64(Marshal.dump(data)))
164
+ rescue Exception => e
165
+ config.ui.logger.warn { "Exception while writing data to child_writer! - #{e.inspect}" }
166
+ end
167
+ end
146
168
  end
147
- data = ExceptionWrapper.new(e)
148
- end
149
169
 
150
- if !data.nil?
151
- config.ui.logger.debug { "write(#{data.inspect})" }
152
- begin
153
- child_writer.write(Base64.encode64(Marshal.dump(data)))
154
- rescue Exception => e
155
- config.ui.logger.warn { "Exception while writing data to child_writer! - #{e.inspect}" }
156
- end
157
- end
170
+ rescue Exception => e
171
+ config.ui.logger.fatal { "Exception in Child Process Handler: #{e.inspect}" }
158
172
 
159
- child_reader.close
160
- child_writer.close
173
+ ensure
174
+ child_reader.close rescue nil
175
+ child_writer.close rescue nil
161
176
 
162
- Process.exit!(0)
177
+ Process.exit!(0)
178
+ end
163
179
  end
164
180
  config.after_fork and config.after_fork.call(Process.pid)
165
181
 
data/lib/ztk/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module ZTK
2
2
 
3
3
  # ZTK Version String
4
- VERSION = "3.2.3"
4
+ VERSION = "3.2.4"
5
5
 
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ztk
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.3
4
+ version: 3.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Patten
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-27 00:00:00.000000000 Z
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport