Class: BeanstalkFarmer::Job

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

Defined Under Namespace

Classes: OutOfTimeError

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Job) initialize(job)

A new instance of Job

Parameters:

  • job (Beanstalk::Job)

    A Beanstalk job that has been reserved to be worked upon



13
14
15
16
# File 'lib/beanstalk_farmer/job.rb', line 13

def initialize(job)
  self.job = job
  set_name_and_arguments
end

Instance Attribute Details

- (Object) args

Returns the value of attribute args



9
10
11
# File 'lib/beanstalk_farmer/job.rb', line 9

def args
  @args
end

- (Object) job

Returns the value of attribute job



9
10
11
# File 'lib/beanstalk_farmer/job.rb', line 9

def job
  @job
end

- (Object) name

Returns the value of attribute name



9
10
11
# File 'lib/beanstalk_farmer/job.rb', line 9

def name
  @name
end

Class Method Details

+ (Hash) handler_pool(reset = false)

A pool of job handlers that can work on jobs in our queue

Parameters:

  • reset (Boolean) (defaults to: false)

    (false) When true, our collection of job handlers will be purged from memory

Returns:

  • (Hash)

    the collection of job handlers



39
40
41
42
# File 'lib/beanstalk_farmer/job.rb', line 39

def self.handler_pool(reset=false)
  @pool   = nil if reset
  @pool ||= Hash.new
end

Instance Method Details

- (Object) work

Performs work for this job



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/beanstalk_farmer/job.rb', line 19

def work
  # Stalker, the inspiration for this project, subtracted 1 from the job's
  # TTR value. I'm not sure why at this point in time. Maybe to compensate
  # for Job setup time.
  Timeout.timeout(job.ttr) do
    handler = self.class.handler_pool[name]
    handler.call(args)
  end
rescue Timeout::Error
  raise OutOfTimeError, "#{name} could not finish in #{job.ttr} seconds"
ensure
  job.delete
end