cromwell 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -121,7 +121,11 @@ You can inspect Cromwell's state with two methods:
121
121
 
122
122
  === Preventing termination
123
123
 
124
- Since version 0.1.2, you can prevent termination of your script even when a signal was caught. To do so, use <code>Cromwell.should_exit = false</code> like that:
124
+ Since version 0.1.2, you can prevent termination of your script even when a signal was caught. To do so, use
125
+
126
+ Cromwell.should_exit = false
127
+
128
+ like that:
125
129
 
126
130
  puts 'You can try to kill me but I will survive!'
127
131
  Cromwell.protect {
@@ -144,17 +148,53 @@ This script will continue working even after <code>^C</code>:
144
148
  You're still here?
145
149
  $
146
150
 
151
+ === Removing traps
152
+
153
+ As of version 0.2, any traps that were previously installed are restored by the unprotect method (which is also called by the protect method if a block was given to it, of course). This means if you had some other code installed to handle signals (profiling, debugging etc.) it should be restored. For example:
154
+
155
+ Signal.trap("INT") { puts "Original signal handler!" }
156
+
157
+ puts 'See you in a while...'
158
+ Cromwell.protect {
159
+ sleep 1
160
+ }
161
+ puts "Try to ^C now to see original signal handler in action!"
162
+ sleep 10
163
+
164
+ When run, it goes like this:
165
+
166
+ $ ruby examples/example5.rb
167
+ See you in a while...
168
+ [ 1 second passes... ]
169
+ Try to ^C now to see original signal handler in action!
170
+ ^COriginal signal handler!
171
+ ^COriginal signal handler!
172
+ ^COriginal signal handler!
173
+ [ ten seconds pass... ]
174
+ $
175
+
176
+ Of course, had I press <code>^C</code> right after "See you in a while...", the script would terminate after 1 second at the end of the protected block.
177
+
147
178
  == Compatibility
148
179
 
149
180
  Works for me. Tested on Mac OS X 10.4--10.6 and a little bit on Debian Linux. If it works for you too, I'd be glad to know. Cromwell's reliability depends heavily on your operating system's signals implementation reliability (which may not be very stable on some systems).
150
181
 
151
182
  == To Do list
152
183
 
153
- * Remove traps when they are not needed anymore. Currently they remain in place and when you call protect again (even with different signals list), the old ones are still effective.
154
184
  * Allow to customize behavior after catching a signal. Right now, the script is terminated after the protected block is done (even if the signal would not normally cause script termination).
155
- * Play well with other trap handlers that might be installed.
156
185
  * Add logger support.
157
186
 
187
+ == Changelog
188
+
189
+ === 0.2
190
+
191
+ * Remove traps when they are not needed anymore and restore original traps.
192
+
193
+ === 0.1.2
194
+
195
+ * Allow to prevent termination of your script even when a signal was caught.
196
+ * Ensure that examples use ../lib/cromwell.rb not the installed gem
197
+
158
198
  == Note on Patches/Pull Requests
159
199
 
160
200
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/cromwell.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cromwell}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Przemyslaw Kowalczyk"]
12
- s.date = %q{2010-01-07}
12
+ s.date = %q{2010-01-08}
13
13
  s.description = %q{A very simple wrapper over Signal#trap method that allows you to easily protect your scripts from being killed while they are doing something that should not be interrupted (e.g. interacting with some non-transactional service) or is too costly to restart (e.g. long computations). }
14
14
  s.email = %q{szeryf@negativeiq.pl}
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "examples/example2.rb",
29
29
  "examples/example3.rb",
30
30
  "examples/example4.rb",
31
+ "examples/example5.rb",
31
32
  "lib/cromwell.rb",
32
33
  "test/helper.rb",
33
34
  "test/test_cromwell.rb"
@@ -43,7 +44,8 @@ Gem::Specification.new do |s|
43
44
  "examples/example1.rb",
44
45
  "examples/example2.rb",
45
46
  "examples/example3.rb",
46
- "examples/example4.rb"
47
+ "examples/example4.rb",
48
+ "examples/example5.rb"
47
49
  ]
48
50
 
49
51
  if s.respond_to? :specification_version then
@@ -0,0 +1,13 @@
1
+ # ensure that we use ../lib/cromwell.rb, not the installed gem
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ require 'cromwell'
5
+
6
+ Signal.trap("INT") { puts "Original signal handler!" }
7
+
8
+ puts 'See you in a while...'
9
+ Cromwell.protect {
10
+ sleep 1
11
+ }
12
+ puts "Try to ^C now to see original signal handler in action!"
13
+ sleep 10
data/lib/cromwell.rb CHANGED
@@ -35,6 +35,7 @@ class Cromwell
35
35
  def unprotect
36
36
  @@protected = false
37
37
  exit if @@should_exit
38
+ restore_old_traps
38
39
  end
39
40
 
40
41
  # call-seq:
@@ -47,12 +48,12 @@ class Cromwell
47
48
  end
48
49
 
49
50
  # call-seq:
50
- # Cromwell.should_exit = bool
51
+ # Cromwell.should_exit = boolean
51
52
  #
52
53
  # Set to false to prevent script from termination even if a signal was caught. You can also set
53
54
  # this to true to have your script terminated after protected block should you wish so.
54
- def should_exit= b
55
- @@should_exit = b
55
+ def should_exit= boolean
56
+ @@should_exit = boolean
56
57
  end
57
58
 
58
59
  # call-seq:
@@ -69,15 +70,27 @@ class Cromwell
69
70
  end
70
71
 
71
72
  def set_up_trap signal
72
- trap signal do
73
+ old_trap = trap signal do
73
74
  if @@protected
74
- #puts "Just a minute now."
75
75
  @@should_exit = true
76
76
  "IGNORE"
77
77
  else
78
78
  exit
79
79
  end
80
80
  end
81
+ stash old_trap, signal
82
+ end
83
+
84
+ def stash old_trap, signal
85
+ @@old_traps ||= {}
86
+ @@old_traps[signal] = old_trap
87
+ end
88
+
89
+ def restore_old_traps
90
+ @@old_traps.each do |signal, old_trap|
91
+ trap signal, old_trap
92
+ end
93
+ @@old_traps = {}
81
94
  end
82
95
  end
83
96
  end
@@ -80,6 +80,20 @@ class TestCromwell < Test::Unit::TestCase
80
80
  end
81
81
  end # general functionality
82
82
 
83
+ context "original traps" do
84
+ should "be restored" do
85
+ im_in_ur_blok_touchin_ur_vars = false
86
+ Signal.trap("HUP") {
87
+ im_in_ur_blok_touchin_ur_vars = true
88
+ }
89
+ Cromwell.protect {
90
+ im_in_ur_blok_touchin_ur_vars = :maybe?
91
+ }
92
+ Process.kill("HUP", $$)
93
+ assert im_in_ur_blok_touchin_ur_vars
94
+ end
95
+ end
96
+
83
97
  context "method protect" do
84
98
  should "set up trap with given signals" do
85
99
  Cromwell.expects(:set_up_traps).with(["HUP", "TERM"])
@@ -145,5 +159,5 @@ class TestCromwell < Test::Unit::TestCase
145
159
  Cromwell.unprotect
146
160
  end
147
161
  end
148
-
162
+
149
163
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cromwell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Przemyslaw Kowalczyk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-07 00:00:00 +01:00
12
+ date: 2010-01-08 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -53,6 +53,7 @@ files:
53
53
  - examples/example2.rb
54
54
  - examples/example3.rb
55
55
  - examples/example4.rb
56
+ - examples/example5.rb
56
57
  - lib/cromwell.rb
57
58
  - test/helper.rb
58
59
  - test/test_cromwell.rb
@@ -91,3 +92,4 @@ test_files:
91
92
  - examples/example2.rb
92
93
  - examples/example3.rb
93
94
  - examples/example4.rb
95
+ - examples/example5.rb