class DRbQS::Task
The tasks defined by this class are sent to nodes and calculated by the nodes.
Constants
- DEFAULT_GROUP
Attributes
Public Class Methods
# File lib/drbqs/task/task.rb, line 93 def self.call_task_method(obj, method_name, args) obj.__send__(method_name, *args) end
# 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
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
# 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
# File lib/drbqs/task/task.rb, line 62 def drb_args(task_id) [@group, task_id] + simple_drb_args end
# 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
# File lib/drbqs/task/task.rb, line 58 def simple_drb_args [@marshal_obj, @method_name, @marshal_args] end