Module: Batchy

Extended by:
Batchy
Included in:
Batchy
Defined in:
lib/batchy.rb,
lib/batchy/batch.rb,
lib/batchy/version.rb,
lib/batchy/configuration.rb,
lib/batchy/stopped_error.rb,
lib/generators/batchy/upgrade_generator.rb,
lib/generators/batchy/active_record_generator.rb

Defined Under Namespace

Classes: ActiveRecordGenerator, Batch, Configuration, Error, StoppedError, UpgradeGenerator

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

Instance Method Details

#clean_expiredObject

Issue a SIGTERM to all expired batches. This will result in an error being raised inside the batchy block and a final error state for the batch.



32
33
34
35
36
# File 'lib/batchy.rb', line 32

def clean_expired
  Batchy::Batch.expired.each do | b |
    b.kill
  end
end

#clean_expired!Object

This is dangerous. If you open a batch inside your main program this process could kill your main application. It is turned off by default.



41
42
43
44
45
46
47
48
49
50
# File 'lib/batchy.rb', line 41

def clean_expired!
  unless Batchy.configure.allow_mass_sigkill
    Kernel.warn 'Mass sigkill is not allowed.  Use "clean_expired" to issue a mass SIGTERM, or set the "allow_mass_sigkill" config option to true'
    return false
  end

  Batchy::Batch.expired.each do | b |
    b.kill!
  end
end

#clear_configurationObject

Sets all configuration options back to default



54
55
56
# File 'lib/batchy.rb', line 54

def clear_configuration
  @configuration = nil
end

#configureObject

Get configuration options If a block is provided, it sets the contained configuration options.



21
22
23
24
25
26
27
# File 'lib/batchy.rb', line 21

def configure
  @configuration ||= Configuration.new
  if block_given?
    yield @configuration
  end
  @configuration
end

#create_batch(options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/batchy.rb', line 73

def create_batch options
  ignore_block = options[:on_ignore] ? options[:on_ignore] : nil
  ensure_block = options[:on_ensure] ? options[:on_ensure] : nil
  options.delete(:on_ignore) if options[:on_ignore]
  options.delete(:on_ensure) if options[:on_ensure]

  batch = Batch.create options
  batch.on_ignore ignore_block unless ignore_block.nil?    
  batch.on_ensure ensure_block unless ensure_block.nil?    
  return batch
end

#currentObject

Get the current batch



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

def current
  @current ||= nil
end

#loggerObject

Sets the logger for batchy



64
65
66
67
# File 'lib/batchy.rb', line 64

def logger
  @logger ||= Logger.new(STDOUT)
  @logger
end

#logger=(logger) ⇒ Object



69
70
71
# File 'lib/batchy.rb', line 69

def logger=(logger)
  @logger = logger
end

#run(*args) ⇒ Object

The main entry point for batchy. It wraps code in a batchy block. Batchy handles errors, logging and allows for callbacks.



88
89
90
91
92
93
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
# File 'lib/batchy.rb', line 88

def run *args
  options = args.extract_options!
  batch = create_batch options

  batch.clear_zombies unless batch.guid.nil?
  batch.start!

  unless batch.ignored?
    begin
      # Set the proclist process name
      previous_name = $0
      process_name = [Batchy.configure.process_name_prefix, batch.name].join(" ")
      $0 = process_name if Batchy.configure.name_process
      
      # Set parent if there is an outer batch
      if Batchy.current
        batch.parent = Batchy.current
      end
      
      # Set current batch
      @current = batch
      
      # Save everything before yielding
      batch.save!
      
      yield batch
      
      batch.run_success_callbacks
    rescue Exception => e
      batch.error = e
      
      batch.run_failure_callbacks
    ensure
      batch.finished_at = DateTime.now
      batch.finish!
      
      batch.run_ensure_callbacks
      
      # Set current batch to parent (or nil if no parent)
      @current = batch.parent
      
      # Set proclist process name back
      $0 = previous_name
    end
  end
end