Method: DRbQS::Task#initialize

Defined in:
lib/drbqs/task/task.rb

#initialize(obj, method_name, opts = {}, &hook) ⇒ 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.

Nodes calculate by obj.method_name(*opts[:args]) 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[:hook] and &hook then &hook is prior to opts[:hook].

Parameters:

  • obj (Object)

    An object that has a method "method_name"

  • method_name (Symbol)

    Method name of calculation

  • opts (Hash) (defaults to: {})

    The options of tasks.

  • hook (Proc)

    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.

Options Hash (opts):

  • :args (Array)

    An array of arguments of method "method_name"

  • :note (String)

    Note for a task

  • :hook (Symbol)

    Method name for hook that takes two arguments server and the result object.

  • :group (Symbol)

    Group of nodes to execute the task.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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