Class: ForemanOpenbolt::TaskJob

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/foreman_openbolt/task_job.rb

Constant Summary collapse

STATUSES =

Constants

%w[pending running success failure exception invalid].freeze
COMPLETED_STATUSES =
%w[success failure exception invalid].freeze
RUNNING_STATUSES =
%w[pending running].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_execution!(proxy:, task_name:, task_description:, targets:, job_id:, parameters: {}, options: {}) ⇒ Object

Class methods



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/foreman_openbolt/task_job.rb', line 33

def self.create_from_execution!(proxy:, task_name:, task_description:, targets:, job_id:, parameters: {}, options: {})
  create!(
    job_id: job_id,
    smart_proxy: proxy,
    task_name: task_name,
    task_description: task_description,
    targets: targets,
    task_parameters: parameters,
    openbolt_options: options,
    status: 'pending',
    submitted_at: Time.current
  )
end

Instance Method Details

#cleanup_proxy_artifactsObject

Schedule cleanup of proxy artifacts if we have successfully saved the results



97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/foreman_openbolt/task_job.rb', line 97

def cleanup_proxy_artifacts
  return unless completed?

  ForemanTasks.async_task(::Actions::ForemanOpenbolt::CleanupProxyArtifacts,
    smart_proxy_id,
    job_id)
  Rails.logger.debug { "Scheduled cleanup for job #{job_id} on proxy #{smart_proxy_id}" }
rescue StandardError => e
  Rails.logger.error("Failed to schedule cleanup for job #{job_id}: #{e.class}: #{e.message}")
  Rails.logger.error(e.backtrace.join("\n")) if e.backtrace
end

#completed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/foreman_openbolt/task_job.rb', line 47

def completed?
  status.in?(COMPLETED_STATUSES)
end

#durationObject



55
56
57
58
# File 'app/models/foreman_openbolt/task_job.rb', line 55

def duration
  return nil unless  && completed_at
  completed_at - 
end

#formatted_targetsObject



80
81
82
# File 'app/models/foreman_openbolt/task_job.rb', line 80

def formatted_targets
  targets&.join(', ') || ''
end

#running?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/models/foreman_openbolt/task_job.rb', line 51

def running?
  status.in?(RUNNING_STATUSES)
end

#saved_result_and_log?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/models/foreman_openbolt/task_job.rb', line 92

def saved_result_and_log?
  saved_change_to_result? || saved_change_to_log?
end

#set_completed_atObject



84
85
86
# File 'app/models/foreman_openbolt/task_job.rb', line 84

def set_completed_at
  self.completed_at ||= Time.current if completed?
end

#status_changed_to_completed?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/models/foreman_openbolt/task_job.rb', line 88

def status_changed_to_completed?
  status_changed? && completed?
end

#target_countObject



76
77
78
# File 'app/models/foreman_openbolt/task_job.rb', line 76

def target_count
  targets&.size || 0
end

#update_from_proxy_result!(proxy_result) ⇒ Object

Result/log will already be scrubbed by the proxy



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/foreman_openbolt/task_job.rb', line 61

def update_from_proxy_result!(proxy_result)
  if proxy_result.blank?
    Rails.logger.warn("Received blank proxy result for job #{job_id}, skipping update")
    return
  end

  transaction do
    self.status = proxy_result['status'] if proxy_result['status'].present?
    self.command = proxy_result['command'] if proxy_result['command'].present?
    self.result = proxy_result['value'] if proxy_result.key?('value')
    self.log = proxy_result['log'] if proxy_result.key?('log')
    save!
  end
end