Class: JobWorkflow::WorkflowStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/job_workflow/workflow_status.rb

Defined Under Namespace

Classes: NotFoundError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, job_class_name:, status:) ⇒ WorkflowStatus

: (context: Context, job_class_name: String, status: status_type) -> void



68
69
70
71
72
# File 'lib/job_workflow/workflow_status.rb', line 68

def initialize(context:, job_class_name:, status:)
  @context = context #: Context
  @job_class_name = job_class_name #: String
  @status = status #: Symbol
end

Instance Attribute Details

#contextObject (readonly)

@rbs!

type status_type = :pending | :running | :completed | :failed


10
11
12
# File 'lib/job_workflow/workflow_status.rb', line 10

def context
  @context
end

#job_class_nameObject (readonly)

: String



11
12
13
# File 'lib/job_workflow/workflow_status.rb', line 11

def job_class_name
  @job_class_name
end

#statusObject (readonly)

: status_type



12
13
14
# File 'lib/job_workflow/workflow_status.rb', line 12

def status
  @status
end

Class Method Details

.find(job_id) ⇒ Object

: (String) -> WorkflowStatus

Raises:



16
17
18
19
20
21
# File 'lib/job_workflow/workflow_status.rb', line 16

def find(job_id)
  workflow_status = find_by(job_id:)
  raise NotFoundError, "Workflow with job_id '#{job_id}' not found" if workflow_status.nil?

  workflow_status
end

.find_by(job_id:) ⇒ Object

: (job_id: String) -> WorkflowStatus?



24
25
26
27
28
29
30
# File 'lib/job_workflow/workflow_status.rb', line 24

def find_by(job_id:)
  data = QueueAdapter.current.find_job(job_id)
  return if data.nil?
  return if data["class_name"] == JobWorkflow::SubTaskJob.name

  WorkflowStatus.from_job_data(data)
end

.from_job_data(data) ⇒ Object

: (Hash[String, untyped]) -> WorkflowStatus



33
34
35
36
37
38
39
40
# File 'lib/job_workflow/workflow_status.rb', line 33

def from_job_data(data)
  job_class_name = data["class_name"]
  job_class = job_class_name.constantize
  workflow = job_class._workflow
  context = context_from_job_data(data, workflow)

  new(context:, job_class_name:, status: data["status"])
end

Instance Method Details

#argumentsObject

: () -> Arguments



80
81
82
# File 'lib/job_workflow/workflow_status.rb', line 80

def arguments
  context.arguments
end

#completed?Boolean

: () -> bool

Returns:

  • (Boolean)


100
101
102
# File 'lib/job_workflow/workflow_status.rb', line 100

def completed?
  status == :succeeded
end

#current_task_nameObject

: () -> Symbol?



75
76
77
# File 'lib/job_workflow/workflow_status.rb', line 75

def current_task_name
  context._task_context.task&.task_name
end

#failed?Boolean

: () -> bool

Returns:

  • (Boolean)


105
106
107
# File 'lib/job_workflow/workflow_status.rb', line 105

def failed?
  status == :failed
end

#job_statusObject

: () -> JobStatus



90
91
92
# File 'lib/job_workflow/workflow_status.rb', line 90

def job_status
  context.job_status
end

#outputObject

: () -> Output



85
86
87
# File 'lib/job_workflow/workflow_status.rb', line 85

def output
  context.output
end

#pending?Boolean

: () -> bool

Returns:

  • (Boolean)


110
111
112
# File 'lib/job_workflow/workflow_status.rb', line 110

def pending?
  status == :pending
end

#running?Boolean

: () -> bool

Returns:

  • (Boolean)


95
96
97
# File 'lib/job_workflow/workflow_status.rb', line 95

def running?
  status == :running
end

#to_hObject

: () -> Hash[Symbol, untyped]



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/job_workflow/workflow_status.rb', line 115

def to_h
  {
    status:,
    job_class_name:,
    current_task_name:,
    arguments: arguments.to_h,
    output: output.flat_task_outputs.map do |task_output|
      {
        task_name: task_output.task_name,
        each_index: task_output.each_index,
        data: task_output.data
      }
    end
  }
end