Class: ForemanOpenbolt::TaskJob
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ForemanOpenbolt::TaskJob
- 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
-
.create_from_execution!(proxy:, task_name:, task_description:, targets:, job_id:, parameters: {}, options: {}) ⇒ Object
Class methods.
Instance Method Summary collapse
-
#cleanup_proxy_artifacts ⇒ Object
Schedule cleanup of proxy artifacts if we have successfully saved the results.
- #completed? ⇒ Boolean
- #duration ⇒ Object
- #formatted_targets ⇒ Object
- #running? ⇒ Boolean
- #saved_result_and_log? ⇒ Boolean
- #set_completed_at ⇒ Object
- #status_changed_to_completed? ⇒ Boolean
- #target_count ⇒ Object
-
#update_from_proxy_result!(proxy_result) ⇒ Object
Result/log will already be scrubbed by the proxy.
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: , status: 'pending', submitted_at: Time.current ) end |
Instance Method Details
#cleanup_proxy_artifacts ⇒ Object
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.}") Rails.logger.error(e.backtrace.join("\n")) if e.backtrace end |
#completed? ⇒ Boolean
47 48 49 |
# File 'app/models/foreman_openbolt/task_job.rb', line 47 def completed? status.in?(COMPLETED_STATUSES) end |
#duration ⇒ Object
55 56 57 58 |
# File 'app/models/foreman_openbolt/task_job.rb', line 55 def duration return nil unless submitted_at && completed_at completed_at - submitted_at end |
#formatted_targets ⇒ Object
80 81 82 |
# File 'app/models/foreman_openbolt/task_job.rb', line 80 def formatted_targets targets&.join(', ') || '' end |
#running? ⇒ 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
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_at ⇒ Object
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
88 89 90 |
# File 'app/models/foreman_openbolt/task_job.rb', line 88 def status_changed_to_completed? status_changed? && completed? end |
#target_count ⇒ Object
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 |