Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36166 - Revamp job hooks #791

Merged
merged 2 commits into from Nov 22, 2023

Conversation

adamruzicka
Copy link
Contributor

@adamruzicka adamruzicka commented Mar 6, 2023

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. Note, both the generic and per-feature are emitted.

@m-bucher
Copy link
Contributor

Wanted to try this on my local foreman-3.1, so I backported the fix onto 5.0.6 and built it.
Unfortunately, when trying to create a webhook (foreman_webhooks-3.0.0) I get a UI-error.
Not sure if it is related to the fix 🤔 Any idea (I do not have a 3.1-dev environment, so the traceback is probably not helpful 🙈 )?

MyError: The push method cannot be invoked on an Immutable data structure.,
    in TypeaheadContainer(n)
    in div
    in t
    in div
    in t
    in j
    in t
    in FormikConnect(t)
    in m
    in div
    in section
    in u
    in ForwardRef
    in Tabs
    in l
    in form
    in d
    in t
    in f
    in y
    in y
    in div
    in t
    in div
    in div
    in div
    in me
    in r
    in t
    in H
    in t
    in div
    in Portal
    in t
    in Ae
    in m
    in y
    in y
    in d
    in Unknown
    in Unknown
    in Unknown
    in C
    in t
    in t
    in l
    in f
    in d
    in div
    in u
    in d
    in y
    in t
    in n
    in a
    in C
    in c
    in div
    in g
    in IntlProvider
    in I18nProviderWrapper(g)
    in d
    in StoreProvider(I18nProviderWrapper(g))
    in DataProvider(StoreProvider(I18nProviderWrapper(g)))

@adamruzicka
Copy link
Contributor Author

Not sure if it is related to the fix

Considering I'm not touching any frontend bits here, I'd say it is unrelated, but who knows

end

def emit_event(execution_plan, hook)
raise "HALT"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, that was just to see the hooks are actually getting called, fixed.

@m-bucher
Copy link
Contributor

Considering I'm not touching any frontend bits here, I'd say it is unrelated, but who knows

My current hypothesis is that simply the number of events is somehow to much for the TypeaheadSelect-component.
Based on the fact, that with this fix it gets 137 events 🤔

@m-bucher
Copy link
Contributor

OK, so apparently it also happens on forklift with latest foreman_webhooks-Plugin. I should probably open an issue for foreman_webhooks, even though the problem seems to lay deeper 🤔

MyError: The push method cannot be invoked on an Immutable data structure.
    in TypeaheadContainer(WrappedTypeahead) (created by FieldInner)
    in div (created by Col)
    in Col (created by FormField)
    in div (created by FormGroup)
    in FormGroup (created by FormField)
    in FormField (created by FieldInner)
    in FieldInner (created by Context.Consumer)
    in FormikConnect(FieldInner) (created by ForemanFormikField)
    in ForemanFormikField (created by WebhookFormTabs)
    in div (created by WebhookFormTabs)
    in section (created by Context.Consumer)
    in TabContentBase (created by ForwardRef)
    in ForwardRef (created by Tabs)
    in Tabs (created by WebhookFormTabs)
    in WebhookFormTabs (created by WebhookForm)
    in form (created by Form)
    in Form (created by Formik)
    in Formik (created by ForemanForm)
    in ForemanForm (created by WebhookForm)
    in WebhookForm (created by ConnectedWebhookForm)
    in ConnectedWebhookForm (created by WebhookCreateModal)
    in div (created by ModalBody)
    in ModalBody (created by ForemanModal)
    in div (created by CustomModalDialog)
    in div (created by CustomModalDialog)
    in div (created by CustomModalDialog)
    in CustomModalDialog (created by Modal)
    in Transition (created by Fade)
    in Fade (created by DialogTransition)
    in DialogTransition (created by Modal)
    in RefHolder (created by Modal)
    in div (created by Modal)
    in Portal (created by Modal)
    in Modal (created by Modal)
    in Modal (created by ForemanModal)
    in ForemanModal (created by ConnectedForemanModal)
    in ConnectedForemanModal (created by WebhookCreateModal)
    in WebhookCreateModal (created by WebhooksIndexPage)
    in WebhooksIndexPage (created by Context.Consumer)
    in Route
    in Switch (created by ForemanSwitcher)
    in ForemanSwitcher (created by AppSwitcher)
    in AppSwitcher (created by ReactApp)
    in ErrorBoundary (created by ReactApp)
    in div (created by LayoutContainer)
    in LayoutContainer (created by Layout)
    in Layout (created by ConnectedLayout)
    in ConnectedLayout (created by ReactApp)
    in Router (created by ConnectedRouter)
    in ConnectedRouter (created by Context.Consumer)
    in ConnectedRouterWithContext (created by ConnectFunction)
    in ConnectFunction (created by ReactApp)
    in ApolloProvider (created by ReactApp)
    in div (created by ReactApp)
    in ReactApp (created by I18nProviderWrapper(ReactApp))
    in IntlProvider (created by I18nProviderWrapper(ReactApp))
    in I18nProviderWrapper(ReactApp) (created by StoreProvider(I18nProviderWrapper(ReactApp)))
    in Provider (created by StoreProvider(I18nProviderWrapper(ReactApp)))
    in StoreProvider(I18nProviderWrapper(ReactApp)) (created by DataProvider(StoreProvider(I18nProviderWrapper(ReactApp))))
    in DataProvider(StoreProvider(I18nProviderWrapper(ReactApp)))

@ofedoren
Copy link
Member

theforeman/foreman_webhooks#56 should fix that.

@adamruzicka
Copy link
Contributor Author

@m-bucher @ofedoren anything left to do here?

def emit_event(execution_plan, hook)
return unless root_action?

if input["job_features"]&.any?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested it again on my forklift and had the problem, that the Event was not triggered, because there were no job_features present:

{
  "host": {
    "id": 1,
    "name": "centos8-katello-devel-stable.example.com"
  },
  "job_category": "Commands",
  "description": "Run uname -a",
  "job_invocation_id": 1,
  "job_features": [],
  "proxy_id": 1,
  "delegated_action_id": 2,
  "with_event_logging": true,
  "locale": "de",
  "current_request_id": "8c551231-c6d4-49fa-9864-845ad758a0ba",
  "current_timezone": "Europe/Berlin",
  "current_user_id": 4,
  "current_organization_id": 1,
  "current_location_id": 2
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you trigger the job?

Edit: Wait, what are we even doing here
Edit 2: Oh, this, I'll take a look

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to trigger the event "#{self.class.event_name_base}_#{self.class.event_name_suffix(hook)}" if there are no features defined, which will then mean triggering the 'generic' actions.remote_execution.run_hosts_job_(failed|success|running) event.

Comment on lines 30 to 36

if input["job_features"]&.any?
payload = event_payload(execution_plan)
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following change sends 'Host(s) Job' event, if no features are specified.
We could also always trigger that event since 🤔

Suggested change
if input["job_features"]&.any?
payload = event_payload(execution_plan)
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
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
else
name = "#{self.class.event_name_base}_#{self.class.event_name_suffix(hook)}"
trigger_hook name, payload: payload
end

@adamruzicka
Copy link
Contributor Author

@m-bucher Hi, could you please check this out once more?

@m-bucher
Copy link
Contributor

@adamruzicka sorry for the delay 😇

The latest patch works for me 👍

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.
@adamruzicka
Copy link
Contributor Author

Sorry that I keep changing this all the time, but I managed to remove some of the code by reusing things which were added in tasks recently.

I mapped multiple features to a single template and then run it. Excerpts from logs:

# grep d91fe80a log/development.log | grep EventSubscriber | grep running
2023-10-16T15:18:13 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_ansible_run_host_running.event.foreman event received
2023-10-16T15:18:13 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_katello_package_install_running.event.foreman event received
2023-10-16T15:18:13 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_running.event.foreman event received

# grep d91fe80a log/development.log | grep EventSubscriber | grep succeeded
2023-10-16T15:18:17 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_host_job_ansible_run_host_succeeded.event.foreman event received
2023-10-16T15:18:17 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_host_job_katello_package_install_succeeded.event.foreman event received
2023-10-16T15:18:17 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_host_job_succeeded.event.foreman event received
2023-10-16T15:18:28 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_ansible_run_host_succeeded.event.foreman event received
2023-10-16T15:18:28 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_katello_package_install_succeeded.event.foreman event received
2023-10-16T15:18:28 [I|app|d91fe80a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_succeeded.event.foreman event received

On failure

# grep 44c0528a log/development.log | grep EventSubscriber | grep running
2023-10-16T15:35:10 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_katello_package_install_running.event.foreman event received
2023-10-16T15:35:10 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_ansible_run_host_running.event.foreman event received
2023-10-16T15:35:10 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_running.event.foreman event received

# grep 44c0528a log/development.log | grep EventSubscriber | grep failed
2023-10-16T15:35:16 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_host_job_katello_package_install_failed.event.foreman event received
2023-10-16T15:35:16 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_host_job_ansible_run_host_failed.event.foreman event received
2023-10-16T15:35:16 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_host_job_failed.event.foreman event received
2023-10-16T15:35:26 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_katello_package_install_failed.event.foreman event received
2023-10-16T15:35:26 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_ansible_run_host_failed.event.foreman event received
2023-10-16T15:35:26 [I|app|44c0528a] ForemanWebhooks::EventSubscriber: actions.remote_execution.run_hosts_job_failed.event.foreman event received

Copy link
Contributor

@m-bucher m-bucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still works as far as I can say so 👍

But you might want to merge it eventually 😉

@adamruzicka adamruzicka merged commit cde629a into theforeman:master Nov 22, 2023
4 checks passed
@adamruzicka adamruzicka deleted the event-revamp branch November 22, 2023 13:07
@adamruzicka
Copy link
Contributor Author

Thank you @m-bucher !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants