Skip to content

Commit

Permalink
Fixes #36166 - Revamp job hooks
Browse files Browse the repository at this point in the history
Per host in job now have per feature events when the action succeeds or fails.

Per job now have general running/stopped/failed as well as per feature
running/stopped/failed events.
  • Loading branch information
adamruzicka committed Mar 6, 2023
1 parent 4b31ed7 commit 954b183
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
19 changes: 6 additions & 13 deletions app/lib/actions/remote_execution/run_host_job.rb
Expand Up @@ -5,8 +5,9 @@ class RunHostJob < Actions::EntryAction
include ::Actions::Helpers::WithDelegatedAction
include ::Actions::ObservableAction
include ::Actions::RemoteExecution::TemplateInvocationProgressLogging
include ::Actions::RemoteExecution::EventHelpers

execution_plan_hooks.use :emit_feature_event, :on => :success
execution_plan_hooks.use :emit_event, :on => [:success, :failure]

middleware.do_not_use Dynflow::Middleware::Common::Transaction
middleware.use Actions::Middleware::HideSecrets
Expand Down Expand Up @@ -80,20 +81,12 @@ def finalize(*args)
end
end

def self.feature_job_event_name(label, suffix = :success)
::Foreman::Observable.event_name_for("#{::Actions::RemoteExecution::RunHostJob.event_name_base}_#{label}_#{::Actions::RemoteExecution::RunHostJob.event_name_suffix(suffix)}")
def self.event_states
[:success, :failure]
end

def emit_feature_event(execution_plan, hook = :success)
return unless root_action?

payload = event_payload(execution_plan)
if input["job_features"]&.any?
input['job_features'].each do |feature|
name = "#{self.class.event_name_base}_#{feature}_#{self.class.event_name_suffix(hook)}"
trigger_hook name, payload: payload
end
end
def emit_event(plan)
super(plan, execution_plan.result == :success ? :success : :failure)
end

def secrets(host, job_invocation, provider)
Expand Down
22 changes: 17 additions & 5 deletions app/lib/actions/remote_execution/run_hosts_job.rb
Expand Up @@ -6,14 +6,17 @@ class RunHostsJob < Actions::ActionWithSubPlans
include Dynflow::Action::WithPollingSubPlans
include Actions::RecurringAction
include Actions::ObservableAction
include Actions::RemoteExecution::EventHelpers

middleware.use Actions::Middleware::BindJobInvocation
middleware.use Actions::Middleware::RecurringLogic
middleware.use Actions::Middleware::WatchDelegatedProxySubTasks

execution_plan_hooks.use :notify_on_success, :on => :success
execution_plan_hooks.use :notify_on_failure, :on => :failure
execution_plan_hooks.use :emit_event_running, :on => :running
execution_plan_hooks.use :emit_running_event, :on => :running
execution_plan_hooks.use :emit_failure_event, :on => :failure
execution_plan_hooks.use :emit_success_event, :on => :success

class CheckOnProxyActions; end

Expand All @@ -31,7 +34,8 @@ def delay(delay_options, job_invocation)
def plan(job_invocation)
job_invocation.task_group.save! if job_invocation.task_group.try(:new_record?)
task.add_missing_task_groups(job_invocation.task_group) if job_invocation.task_group
action_subject(job_invocation)
features = job_invocation.pattern_templates.flat_map { |t| t.remote_execution_features.pluck(:label) }.uniq
action_subject(job_invocation, job_features: features)
job_invocation.targeting.resolve_hosts! if job_invocation.targeting.dynamic? || !job_invocation.targeting.resolved?
set_up_concurrency_control job_invocation
input.update(:job_category => job_invocation.job_category)
Expand Down Expand Up @@ -157,11 +161,19 @@ def proxy_batch_size
input[:proxy_batch_size]
end

def self.event_names
super + [event_name_base + '_' + event_name_suffix('running')]
def self.event_states
[:success, :failure, :running]
end

def emit_event_running(plan)
def emit_failure_event(plan)
emit_event(plan, :failure)
end

def emit_success_event(plan)
emit_event(plan, :success)
end

def emit_running_event(plan)
emit_event(plan, :running)
end

Expand Down
6 changes: 4 additions & 2 deletions lib/foreman_remote_execution/engine.rb
Expand Up @@ -279,8 +279,10 @@ class Engine < ::Rails::Engine
::Dynflow::Action.descendants.select do |klass|
klass <= ::Actions::ObservableAction
end.map(&:namespaced_event_names) +
RemoteExecutionFeature.all.pluck(:label).map do |label|
::Actions::RemoteExecution::RunHostJob.feature_job_event_name(label)
RemoteExecutionFeature.all.pluck(:label).flat_map do |label|
[::Actions::RemoteExecution::RunHostJob, ::Actions::RemoteExecution::RunHostsJob].map do |klass|
klass.feature_job_event_names(label)
end
end
)
end
Expand Down

0 comments on commit 954b183

Please sign in to comment.