Class: Serially::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/serially/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_name, task_order, options, task_manager, &run_block) ⇒ Task

Returns a new instance of Task.



6
7
8
9
10
11
12
13
# File 'lib/serially/task.rb', line 6

def initialize(task_name, task_order, options, task_manager, &run_block)
  @name = task_name.to_sym
  @task_order = task_order
  @klass = task_manager.klass
  @options = options
  @run_block = run_block
  @task_manager = task_manager
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



4
5
6
# File 'lib/serially/task.rb', line 4

def klass
  @klass
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/serially/task.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/serially/task.rb', line 4

def options
  @options
end

#run_blockObject

Returns the value of attribute run_block.



4
5
6
# File 'lib/serially/task.rb', line 4

def run_block
  @run_block
end

#task_orderObject

Returns the value of attribute task_order.



4
5
6
# File 'lib/serially/task.rb', line 4

def task_order
  @task_order
end

Instance Method Details

#<=>(task) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/serially/task.rb', line 23

def <=>(task)
  if task.is_a? Symbol
    name <=> task
  else
    name <=> task.name
  end
end

#==(task) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/serially/task.rb', line 15

def ==(task)
  if task.is_a? Symbol
    name == task
  else
    name == task.name && self.klass == task.klass
  end
end

#on_error!(instance, result_msg, result_obj) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/serially/task.rb', line 55

def on_error!(instance, result_msg, result_obj)
  if options[:on_error]
    if !klass.method_defined?(options[:on_error])
      raise Serially::ConfigurationError.new("Serially: error handler #{options[:on_error]} not found for task #{self.name}")
    end

    begin
      status = instance.send(options[:on_error], result_msg, result_obj)
    rescue StandardError => exc
      Resque.logger.error("Serially: error handler for task '#{@name}' raised exception: #{exc.message}")
      status = false
    end
    status
  end
end

#run!(instance) ⇒ Object

args - arguments needed to create an instance of your class. If you don’t provide custom implementation for create_instance, pass instance_id or hash of arguments,



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/serially/task.rb', line 37

def run!(instance)
  if instance
    if !@run_block && !instance.respond_to?(@name)
      raise Serially::ConfigurationError.new("Serially task #{@name} in class #{@klass} doesn't have an implementation method or a block to run")
    end
    begin
      status, msg, result_obj = @run_block ? @run_block.call(instance) : instance.send(@name)
    rescue StandardError => exc
      return [false, "Serially: task '#{@name}' raised exception: #{exc.message}", exc]
    end
  else
    return [false, "Serially: instance couldn't be created, task '#{@name}'' not started"]
  end
  # returns true (unless status == nil/false/[]), '' (unless msg is a not empty string) and result_obj, which might be nil
  # if task doesn't return it
  [status.present?, msg.to_s, result_obj]
end

#to_sObject



31
32
33
# File 'lib/serially/task.rb', line 31

def to_s
  name.to_s
end