class Robinhood::Process

This class wraps a previously defined process. It’s responsible to prevent the same process to be run at the sime time on different System Processes.

A Process leverages a Celluloid Actor, so it will spawned in a different thread and communicate with the rest of the system with messages.

Attributes

name[R]
options[R]

Public Class Methods

new(name, options, block) click to toggle source

Public: Initializes a Process

name - A String that will be used as a global identifier. It’s

important that it is unique, since the locking will be
performed this name as scope.

options - A Hash of options that configure this Process.

:throttle - An Integer that represents the minimum number of
            seconds that this process will take. In other
            words, if its execution takes less than that, we'll
            wait a little bit until it hits the next execution.
:timeout  - An Integer representing the period of time after
            which this process' execution will be considered as
            'hung' and another execution will take place.

block - The block that will be evaluated each time this Process gets

executed.
# File lib/robinhood/process.rb, line 34
def initialize(name, options, block)
  @name = name
  @options = options
  @block = block
  async.run
end

Private Instance Methods

lock() click to toggle source
# File lib/robinhood/process.rb, line 59
def lock
  mutex.lock
end
lock_name() click to toggle source
# File lib/robinhood/process.rb, line 67
def lock_name
  "robinhood:#{name}:lock"
end
mutex() click to toggle source
# File lib/robinhood/process.rb, line 83
def mutex
  @mutex ||= Mutex.new(lock_name, block: 5, sleep: 1, expire: timeout)
end
run() click to toggle source
# File lib/robinhood/process.rb, line 43
def run
  return unless lock

  begin
    time = Benchmark.realtime{ instance_eval(&@block) }
    if difference = throttle - time
      sleep(difference)
    end
  ensure
    unlock
    sleep(1)
  end
ensure
  async.run
end
throttle() click to toggle source
# File lib/robinhood/process.rb, line 71
def throttle
  if (throttle = options[:throttle]) != nil
    throttle
  else
    0.05
  end
end
timeout() click to toggle source
# File lib/robinhood/process.rb, line 79
def timeout
  options[:timeout] || 300
end
unlock() click to toggle source
# File lib/robinhood/process.rb, line 63
def unlock
  mutex.unlock
end