Class: QueueToTheFuture::Job

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

Overview

A proxy object for the future return value of a block.

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Job

Creates a job and schedules it by calling Coordinator#schedule.

Parameters:

  • *args (List)

    The list of arguments to pass to the given block

  • &block (Proc)

    The block to be executed



10
11
12
13
14
15
# File 'lib/queue_to_the_future/job.rb', line 10

def initialize(*args, &block)
  @args   = args
  @block  = block
  
  Coordinator.schedule(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

Allows the job to behave as the return value of the block.

Accessing any method on the job will cause code to block until the job is completed.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/queue_to_the_future/job.rb', line 34

def method_missing(*args, &block)
  Thread.pass until defined?(@result)
  
  case @result
  when Exception
    def self.method_missing(*args, &block); raise @result; end
  else
    def self.method_missing(*args, &block); @result.send(*args, &block); end
  end
  
  self.method_missing(*args, &block)
end

Instance Method Details

#__execute__nil

Execute the job.

This is called by the worker the job gets assigned to.

Returns:

  • (nil)


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

def __execute__
  @result = @block[*@args]
rescue Exception => e
  @result = e
ensure
  # Prevent multiple executions
  def self.__execute__; nil; end
end