class DRbQS::Task

The tasks defined by this class are sent to nodes and calculated by the nodes.

Constants

DEFAULT_GROUP

Attributes

args[R]
group[RW]
hook[R]
method_name[R]
note[RW]
obj[R]

Public Class Methods

call_task_method(obj, method_name, args) click to toggle source
# File lib/drbqs/task/task.rb, line 93
def self.call_task_method(obj, method_name, args)
  obj.__send__(method_name, *args)
end
execute_task(marshal_obj, method_name, marshal_args) click to toggle source
# File lib/drbqs/task/task.rb, line 97
def self.execute_task(marshal_obj, method_name, marshal_args)
  self.call_task_method(Marshal.load(marshal_obj), method_name, Marshal.load(marshal_args))
end
new(obj, method_name, opts = {}, &hook) click to toggle source

Nodes calculate by obj.method_name(*opts) and send the result to their server. Then the server executes &hook with a server instance and an object of result. For the communication of a server and nodes we must convert obj to a string by Marshal.dump. If we set both opts and &hook then &hook is prior to opts. @param [Object] obj An object that has a method “method_name” @param [Symbol] method_name Method name of calculation @param [Hash] opts The options of tasks. @option opts [Array] :args An array of arguments of method “method_name” @option opts [String] :note Note for a task @option opts [Symbol] :hook Method name for hook

that takes two arguments server and the result object.

@option opts [Symbol] :group Group of nodes to execute the task. @param [Proc] hook A server execute hook as a callback when the server receive the result

hook take two arguments: a DRbQS::Server object and a result of task.

@note Changes of obj on a node are not sent to a server.

That is, opts[:hook] must not depend on changes of instance variables on a node.
# File lib/drbqs/task/task.rb, line 36
def initialize(obj, method_name, opts = {}, &hook)
  @obj = obj
  begin
    @marshal_obj = Marshal.dump(@obj)
  rescue
    raise "Can not dump #{@obj.inspect}."
  end
  @method_name = method_name.intern
  @args = opts[:args] || []
  unless Array === @args
    raise "Arguments of task must be an array."
  end
  begin
    @marshal_args = Marshal.dump(@args)
  rescue
    raise "Can not dump #{@args.inspect}."
  end
  @note = opts[:note]
  @hook = hook || opts[:hook]
  @group = opts[:group] || DRbQS::Task::DEFAULT_GROUP
end

Public Instance Methods

==(other) click to toggle source
# File lib/drbqs/task/task.rb, line 78
def ==(other)
  if @marshal_obj == other.instance_variable_get(:@marshal_obj) &&
      @method_name == other.instance_variable_get(:@method_name) &&
      @marshal_args == other.instance_variable_get(:@marshal_args)
    if Proc === @hook && Proc === other.hook
      # Return false at this time.
      false
    else
      @hook == other.hook
    end
  else
    false
  end
end
drb_args(task_id) click to toggle source
# File lib/drbqs/task/task.rb, line 62
def drb_args(task_id)
  [@group, task_id] + simple_drb_args
end
exec_hook(server, result) click to toggle source
# File lib/drbqs/task/task.rb, line 66
def exec_hook(server, result)
  case @hook
  when Proc
    @hook.call(server, result)
  when Symbol, String
    @obj.__send__(@hook, server, result)
  else
    return nil
  end
  true
end
simple_drb_args() click to toggle source
# File lib/drbqs/task/task.rb, line 58
def simple_drb_args
  [@marshal_obj, @method_name, @marshal_args]
end