Class: DTK::State::WorkflowInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/state/workflow_instance.rb,
lib/state/workflow_instance/attribute_type_info.rb

Defined Under Namespace

Classes: AttributeTypeInfo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, name, crd_content) ⇒ WorkflowInstance

Returns a new instance of WorkflowInstance.



7
8
9
10
11
12
13
14
15
# File 'lib/state/workflow_instance.rb', line 7

def initialize(namespace, name, crd_content)
  @name        = name
  @namespace   = namespace
  @assembly          = crd_content.references.assembly
  @workflow_template = crd_content.references.workflow
  @attributes        = crd_content.spec.attributes
  @workflow          = crd_content.spec.workflow
  @attribute_type_info = AttributeTypeInfo.create_from_kube_hash(@attributes.to_h || {})
end

Instance Attribute Details

#assemblyObject (readonly)

Returns the value of attribute assembly.



5
6
7
# File 'lib/state/workflow_instance.rb', line 5

def assembly
  @assembly
end

#attribute_type_infoObject (readonly)

Returns the value of attribute attribute_type_info.



5
6
7
# File 'lib/state/workflow_instance.rb', line 5

def attribute_type_info
  @attribute_type_info
end

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/state/workflow_instance.rb', line 5

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/state/workflow_instance.rb', line 5

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



5
6
7
# File 'lib/state/workflow_instance.rb', line 5

def namespace
  @namespace
end

#workflowObject (readonly)

Returns the value of attribute workflow.



5
6
7
# File 'lib/state/workflow_instance.rb', line 5

def workflow
  @workflow
end

#workflow_templateObject (readonly)

Returns the value of attribute workflow_template.



5
6
7
# File 'lib/state/workflow_instance.rb', line 5

def workflow_template
  @workflow_template
end

Class Method Details

.find_action(id, workflow = @workflow) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/state/workflow_instance.rb', line 78

def self.find_action(id, workflow = @workflow)
  action = nil
  subtasks = workflow[:subtasks]
  subtasks.each do |subtask|
    if(subtask.id.to_s == id.to_s)
      action = subtask
      break
    elsif subtask[:subtasks]
      action = find_action(id, subtask)
      break unless action.nil?
    end
  end
  action
end

.get(namespace, name, opts = {}) ⇒ Object



17
18
19
20
# File 'lib/state/workflow_instance.rb', line 17

def self.get(namespace, name, opts = {})
  workflow_instance = ::DTK::CrdClient.get_kubeclient(opts).get_workflow_instance(name, namespace)
  WorkflowInstance.new(namespace, name, workflow_instance)
end

.get_action_attributes(namespace, name, action_id, opts = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/state/workflow_instance.rb', line 27

def self.get_action_attributes(namespace, name, action_id, opts = {})
  workflow_instance = get(namespace, name, opts)
  action = WorkflowInstance.find_action(action_id, workflow_instance.workflow)
  return nil unless action
  attributes = action[:attributes] || {}
  attributes.to_h
end

.get_attributes(namespace, name, opts = {}) ⇒ Object



22
23
24
25
# File 'lib/state/workflow_instance.rb', line 22

def self.get_attributes(namespace, name, opts = {})
  workflow_instance = get(namespace, name, opts)
  workflow_instance.attributes.to_h
end

.patchError!(patches, message, action_index_steps) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/state/workflow_instance.rb', line 56

def self.patchError!(patches, message, action_index_steps)
  errorPatch = {
    "op" => "add",
    "path" => "/spec/status/steps/#{action_index_steps}/errorMsg",
    "value" => message
  }
  patches << errorPatch
end

.update_action_level_result_attributes(namespace, name, attributes, action_id, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/state/workflow_instance.rb', line 35

def self.update_action_level_result_attributes(namespace, name, attributes, action_id, opts = {})
  return "Dynamic attributes do not exist for action with id #{@action_id}, nothing to update" if attributes.nil? || attributes.empty?
  attributes.delete_if { |key, value| value.nil? || value.to_s.strip == '' }
  workflow_instance = ::DTK::CrdClient.get_kubeclient(opts).get_workflow_instance(name, namespace)
  workflow = workflow_instance[:spec][:workflow]

  action = WorkflowInstance.find_action(action_id, workflow)
  action[:attributes] = {} if !action[:attributes]
  attributes.each do |attr_name, attr_val|
    action[:attributes][attr_name.to_sym] = {} unless action[:attributes][attr_name.to_sym]
    unless action[:attributes][attr_name.to_sym][:hidden]
      if attr_val.is_a? Hash
        action[:attributes][attr_name.to_sym][:value] = attr_val[:value] || attr_val
      else
        action[:attributes][attr_name.to_sym][:value] = attr_val
      end
    end
  end
  ::DTK::CrdClient.get_kubeclient(opts).update_workflow_instance(workflow_instance)
end

.update_action_status(namespace, name, parent_id, action_id, status, error_message = "", opts = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/state/workflow_instance.rb', line 65

def self.update_action_status(namespace, name, parent_id, action_id, status, error_message = "", opts = {})
  workflow_instance = ::DTK::CrdClient.get_kubeclient(opts).get_workflow_instance(name, namespace)
  steps = workflow_instance[:spec][:status][:steps]
  action_index_steps = steps.find_index { |action| action[:id].eql? action_id }
  patch = [{
    "op" => "replace",
    "path" => "/spec/status/steps/#{action_index_steps}/state",
    "value" => status
  }]
  patchError!(patch, error_message, action_index_steps) unless error_message.empty? || error_message.nil?
  ::DTK::CrdClient.get_kubeclient(opts).json_patch_workflow_instance(name, patch, namespace)
end