Class: DRbQS::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/drbqs/task/task.rb,
lib/drbqs/task/registrar.rb,
lib/drbqs/task/task_generator.rb

Overview

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

Direct Known Subclasses

CommandTask, TaskSet

Defined Under Namespace

Classes: Generator, Registrar, TaskSet

Constant Summary collapse

DEFAULT_GROUP =
:default

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



14
15
16
# File 'lib/drbqs/task/task.rb', line 14

def args
  @args
end

#groupObject

Returns the value of attribute group.



17
18
19
# File 'lib/drbqs/task/task.rb', line 17

def group
  @group
end

#hookObject (readonly)

Returns the value of attribute hook.



16
17
18
# File 'lib/drbqs/task/task.rb', line 16

def hook
  @hook
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



15
16
17
# File 'lib/drbqs/task/task.rb', line 15

def method_name
  @method_name
end

#noteObject

Returns the value of attribute note.



17
18
19
# File 'lib/drbqs/task/task.rb', line 17

def note
  @note
end

#objObject (readonly)

Returns the value of attribute obj.



13
14
15
# File 'lib/drbqs/task/task.rb', line 13

def obj
  @obj
end

Class Method Details

.call_task_method(obj, method_name, args) ⇒ Object



93
94
95
# 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) ⇒ Object



97
98
99
# 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

Instance Method Details

#==(other) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



62
63
64
# File 'lib/drbqs/task/task.rb', line 62

def drb_args(task_id)
  [@group, task_id] + simple_drb_args
end

#exec_hook(server, result) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# 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_argsObject



58
59
60
# File 'lib/drbqs/task/task.rb', line 58

def simple_drb_args
  [@marshal_obj, @method_name, @marshal_args]
end