Class: Libis::Workflow::Task

Inherits:
Object
  • Object
show all
Includes:
Tools::Logger, Tools::ParameterContainer
Defined in:
lib/libis/workflow/task.rb

Overview

noinspection RubyTooManyMethodsInspection

Direct Known Subclasses

TaskGroup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, cfg = {}) ⇒ Task

Returns a new instance of Task.



29
30
31
32
33
34
# File 'lib/libis/workflow/task.rb', line 29

def initialize(parent, cfg = {})
  @subitems_stopper = false
  @subtasks_stopper = false
  self.parent = parent
  configure cfg
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/libis/workflow/task.rb', line 18

def name
  @name
end

#parentObject

Returns the value of attribute parent.



18
19
20
# File 'lib/libis/workflow/task.rb', line 18

def parent
  @parent
end

#processing_itemObject

Returns the value of attribute processing_item.



18
19
20
# File 'lib/libis/workflow/task.rb', line 18

def processing_item
  @processing_item
end

#workitemObject

Returns the value of attribute workitem.



18
19
20
# File 'lib/libis/workflow/task.rb', line 18

def workitem
  @workitem
end

Class Method Details

.task_classesObject



25
26
27
# File 'lib/libis/workflow/task.rb', line 25

def self.task_classes
  ObjectSpace.each_object(::Class).select { |klass| klass < self }
end

Instance Method Details

#<<(task) ⇒ Object



36
37
38
# File 'lib/libis/workflow/task.rb', line 36

def <<(task)
  raise Libis::WorkflowError, "Processing task '#{self.namepath}' is not allowed to have subtasks."
end

#apply_options(opts) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/libis/workflow/task.rb', line 108

def apply_options(opts)
  o = {}
  o.merge!(opts[self.class.to_s] || {})
  o.merge!(opts[self.name] || opts[self.names.join('/')] || {})

  if o and o.is_a? Hash
    default_values.each do |name, _|
      next unless o.key?(name.to_s)
      next if o[name.to_s].nil?
      paramdef = get_parameter_definition name.to_sym
      value = paramdef.parse(o[name.to_s])
      self.parameter(name.to_sym, value)
    end
  end
end

#loggerObject



136
137
138
# File 'lib/libis/workflow/task.rb', line 136

def logger
  (self.parent || self.get_run).logger
end

#message(severity, msg, *args) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/libis/workflow/task.rb', line 124

def message(severity, msg, *args)
  taskname = self.namepath rescue nil
  self.set_application(taskname)
  item = self.workitem rescue nil
  item = args.shift if args.size > 0 and args[0].is_a?(::Libis::Workflow::Base::WorkItem)
  subject = item.namepath rescue nil
  subject ||= item.name rescue nil
  subject ||= item.to_s rescue nil
  self.set_subject(subject)
  super(severity, msg, *args)
end

#namepathObject



104
105
106
# File 'lib/libis/workflow/task.rb', line 104

def namepath;
  self.names.join('/');
end

#namesObject



100
101
102
# File 'lib/libis/workflow/task.rb', line 100

def names
  (self.parent.names rescue Array.new).push(name).compact
end

#run(item) ⇒ Object

Parameters:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/libis/workflow/task.rb', line 41

def run(item)
  check_item_type ::Libis::Workflow::Base::WorkItem, item
  self.workitem = item

  case self.action
    when :retry
      if item.check_status(:DONE, self.namepath)
        debug 'Retry: skipping task %s because it has finished successfully.', item, self.namepath
        return
      end
    when :failed
      return
    else
  end

  (parameter(:retry_count)+1).times do

    item = run_item(item)

    case item.status(self.namepath)
      when :DONE
        self.action = :run
        return
      when :ASYNC_WAIT
        self.action = :retry
      when :ASYNC_HALT
        break
      when :FAILED
        break
      else
        return
    end

    self.action = :retry

    sleep(parameter(:retry_interval))

  end

  item.get_run.action = :failed

rescue WorkflowError => e
  error e.message, item
  set_status item, :FAILED

rescue WorkflowAbort => e
  set_status item, :FAILED
  raise e if parent

rescue ::Exception => e
  set_status item, :FAILED
  fatal "Exception occured: #{e.message}", item
  debug e.backtrace.join("\n")

ensure
  item.save!

end