Class: Cromwell

Inherits:
Object
  • Object
show all
Defined in:
lib/cromwell.rb

Constant Summary collapse

DEFAULT_SIGNAL_LIST =
%w[INT TERM HUP QUIT].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.custom_trapsObject

Use this Hash to provide custom traps for signals while protection is active. The key should be a signal name (in same form as you give it to protect method) and the value should be a Proc object that will be called when appropriate signal is caught.

Note that your block will replace default Cromwell’s handler completely, so if you actually wanted to get the same behavior, you should use:

Cromwell.should_exit = true

Example:

Cromwell.custom_traps["INT"] = proc {
  puts "Trying your ^C skills, are you?"
}


40
41
42
# File 'lib/cromwell.rb', line 40

def custom_traps
  @custom_traps
end

.loggerObject

Something to log Cromwell’s messages. This can be a Logger or any other class that accepts debug and info messages. Default value is nil.



13
14
15
# File 'lib/cromwell.rb', line 13

def logger
  @logger
end

.protectedObject (readonly) Also known as: protected?

True if the protection is currently active.



22
23
24
# File 'lib/cromwell.rb', line 22

def protected
  @protected
end

.should_exitObject Also known as: should_exit?

True when the script will be terminated after protected block, i.e. when a signal was caught that was protected from. You can set it to false to prevent script from termination even if a signal was caught.



18
19
20
# File 'lib/cromwell.rb', line 18

def should_exit
  @should_exit
end

Class Method Details

.protect(*signals, &block) ⇒ Object

call-seq:

Cromwell.protect(*signals) { ... some code ... }
Cromwell.protect(*signals)
Cromwell.protect { ... some code ... }
Cromwell.protect

Starts protecting your code. If called with a block, only the code within a block is executed with signal protection. Without a block, script is protected until unprotect is called. Signals can be given in all the forms that Signal#trap recognizes. Without parameters, the code is protected from the signals in DEFAULT_SIGNAL_LIST. More info and examples in README.rdoc.



53
54
55
56
57
58
59
# File 'lib/cromwell.rb', line 53

def protect *signals, &block
  debug "Protect called with [#{signals * ', '}]"
  set_up_traps(signals.empty? ? DEFAULT_SIGNAL_LIST : signals.flatten)
  @should_exit = false
  @protected   = true
  protect_block(&block) if block_given?
end

.unprotectObject

call-seq:

Cromwell.unprotect

Turns off protection from signals. Will terminate the script with Kernel#exit if signal was caught earlier (so any at_exit code will be executed). The protect method calls this automatically when executed with a block.



67
68
69
70
71
72
73
74
75
76
# File 'lib/cromwell.rb', line 67

def unprotect
  debug "Unprotect called, should_exit? = #{@should_exit}"
  @protected = false
  if @should_exit
    info "Exiting because should_exit is true"
    exit
  else
    restore_old_traps
  end
end