Class: JobBoss::Batch

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Defined in:
lib/job_boss/batch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Batch

Returns a new instance of Batch.



26
27
28
29
30
31
# File 'lib/job_boss/batch.rb', line 26

def initialize(options = {})
  options[:priority] ||= 1

  @priority = options[:priority]
  @batch_id = options[:batch_id] || Batch.generate_batch_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Allow calling of Job class methods from a batch which will be called on in the scope of the jobs for the batch Examples: wait_for_jobs, result_hash, time_taken, completed_percent, cancelled?



41
42
43
# File 'lib/job_boss/batch.rb', line 41

def method_missing(sym, *args, &block)
  jobs.send(sym, *args, &block)
end

Instance Attribute Details

#batch_idObject

Returns the value of attribute batch_id.



5
6
7
# File 'lib/job_boss/batch.rb', line 5

def batch_id
  @batch_id
end

#priorityObject

Returns the value of attribute priority.



5
6
7
# File 'lib/job_boss/batch.rb', line 5

def priority
  @priority
end

Class Method Details

.generate_batch_id(size = 32) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/job_boss/batch.rb', line 47

def generate_batch_id(size = 32)
  characters = (0..9).to_a + ('a'..'f').to_a

  (1..size).collect do |i|
    characters[rand(characters.size)]
  end.join
end

Instance Method Details

#jobsObject

Returns ActiveRecord::Relation representing query for jobs in batch



34
35
36
# File 'lib/job_boss/batch.rb', line 34

def jobs
  Job.where('batch_id = ?', @batch_id)
end

#queue(attributes = {}) ⇒ Object

Used to queue jobs in a batch Usage:

batch.queue.math.is_prime?(42)


11
12
13
14
# File 'lib/job_boss/batch.rb', line 11

def queue(attributes = {})
  require 'job_boss/queuer'
  Queuer.new({:priority => @priority, :batch_id => @batch_id}.merge(attributes))
end

#queue_path(path, *args) ⇒ Object

Used to queue jobs in a batch Usage:

batch.queue_path('math#in_prime?', 42)


20
21
22
23
24
# File 'lib/job_boss/batch.rb', line 20

def queue_path(path, *args)
  controller, action = path.split('#')

  queue.send(controller).send(action, *args)
end