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, Libis::Workflow::Tasks::Analyzer

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

#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



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

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

  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



144
145
146
# File 'lib/libis/workflow/task.rb', line 144

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

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/libis/workflow/task.rb', line 122

def message(severity, msg, *args)
  taskname = self.namepath rescue nil
  self.set_application(taskname)
  item = self.workitem
  item = args.shift if args.size > 0 and args[0].is_a?(::Libis::Workflow::Base::WorkItem)
  if item
    subject = nil
    begin
      subject = item.to_s
      subject = item.name
      subject = item.namepath
    rescue
      # do nothing
    end
    self.set_subject(subject)
    return unless super(severity, msg, *args)
    item.log_message(
        severity, msg.is_a?(Integer) ? {id: msg} : {text: (msg.to_s rescue '')}.merge(task: taskname), *args
    )
  end
end

#namepathObject



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

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

#namesObject



97
98
99
# File 'lib/libis/workflow/task.rb', line 97

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
# 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
      return if item.check_status(:DONE, self.namepath)
    when :failed
      return
    else
  end

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

    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
  update_status item, :FAILED

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

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

ensure
  item.save

end