Class: Cromwell

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

Constant Summary collapse

DEFAULT_SIGNAL_LIST =
%w[INT TERM HUP QUIT].freeze
@@logger =
nil
@@should_exit =
false
@@protected =
false
@@old_traps =
{}

Class Method Summary collapse

Class Method Details

.loggerObject

call-seq:

Cromwell.logger

Returns logger. There is no default logger, so if you haven’t set this before, it will be nil.



84
85
86
# File 'lib/cromwell.rb', line 84

def logger
  @@logger
end

.logger=(logger) ⇒ Object

call-seq:

Cromwell.logger = some_logger

Set a logger for Cromwell.



92
93
94
# File 'lib/cromwell.rb', line 92

def logger= logger
  @@logger = logger
end

.protect(*signals) ⇒ 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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cromwell.rb', line 21

def protect *signals
  debug "Protect called with [#{signals * ', '}]"
  set_up_traps(signals.empty? ? DEFAULT_SIGNAL_LIST : signals.flatten)
  @@should_exit = false
  @@protected   = true
  if block_given?
    begin
      debug "About to yield to block."
      yield
      debug "After yielding to block."
    ensure
      unprotect
    end
  end
end

.protected?Boolean

call-seq:

Cromwell.protected?

True if the protection is currently active.

Returns:

  • (Boolean)


76
77
78
# File 'lib/cromwell.rb', line 76

def protected?
  @@protected
end

.should_exit=(boolean) ⇒ Object

call-seq:

Cromwell.should_exit = boolean

Set to false to prevent script from termination even if a signal was caught. You can also set this to true to have your script terminated after protected block should you wish so.



68
69
70
# File 'lib/cromwell.rb', line 68

def should_exit= boolean
  @@should_exit = boolean
end

.should_exit?Boolean

call-seq:

Cromwell.should_exit?

True when the script will be terminated after protected block, i.e. when a signal was caught that was protected from.

Returns:

  • (Boolean)


59
60
61
# File 'lib/cromwell.rb', line 59

def should_exit?
  @@should_exit
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.



43
44
45
46
47
48
49
50
51
52
# File 'lib/cromwell.rb', line 43

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