Skip to content

Create event hook infrastructure#1308

Merged
scottwulf merged 1 commit intocrowbar:masterfrom
scottwulf:event-hooks
Aug 30, 2017
Merged

Create event hook infrastructure#1308
scottwulf merged 1 commit intocrowbar:masterfrom
scottwulf:event-hooks

Conversation

@scottwulf
Copy link
Contributor

Rebase of PR #952 but only including the new infrastructure class

For usage examples, see:
#952
crowbar/crowbar-ha#171
crowbar/crowbar-openstack#717

There are several cases where we have events that should trigger some
activity in some other part of Crowbar. For instance:

  • when the public name of a node is saved, it may impact the endpoint
    of an OpenStack service
  • when the public name of the VIP of haproxy is changed, it impacts the
    endpoint of OpenStack services
  • when the keystone proposal is applied, we may want to reapply all
    proposals that depend on keystone

What we need for this is the ability to notify about the events in the
rails application and then dispatch the notifications to hooks which
listen for them and decide if some action should be triggered.

The main reason we didn't have this in the past is that we likely don't
want to do that in the foreground of the rails application. But now that
we have delayed_job, we can send the notifications and run the hooks in
the background.

In this commit, we add the simple infrastructure about notifications and
hooks:

  • the events are defined with a name and a hash that contains the
    details of the event. The structure of the hash depends on the event.

  • a simple dispatcher exists that simply connects the hooks to the
    events.

  • the hooks only exist for service objects for the time being; a
    service object simply needs to have a event_hook method to register
    the hook, and will need to filter for the events it cares about. The
    signature of event_hook is as follows:
    def event_hook(role, event, details)

It could be argued that the hooks should be registered for some specific
events (hence moving the filter to the event dispatcher), but it's not
worth the complexity for now.

Copy link
Contributor

@matelakat matelakat left a comment

Choose a reason for hiding this comment

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

My Main worry is that a non-properly implemented event handler might prevent other handlers to be called.

begin
service.event_hook(role, event, details)
rescue StandardError => e
raise "Error while executing event hook of #{barclamp} for #{event}: #{e.message}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we want to include that it is for a specific proposal - as if the barclamp has multiple proposals, it might fail in one of them?

role = proposal.role
next if role.nil?
begin
service.event_hook(role, event, details)
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if event_hook is implemented badly and raises an error? I guess in that case we raise and do not execute the hook for the rest of the barclamps?

Rails.logger.error(
"Error while executing event_hook for barclamp '#{barclamp}', " \
"proposal '#{proposal.name}', and event '#{event}'. " \
"Error message is '#{e.message}'.")

Choose a reason for hiding this comment

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

Layout/MultilineMethodCallBraceLayout: Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument.

Rebase of PR crowbar#952 but only including the new infrastructure class

For usage examples, see:
crowbar#952
crowbar/crowbar-ha#171
crowbar/crowbar-openstack#717

There are several cases where we have events that should trigger some
activity in some other part of Crowbar. For instance:

 - when the public name of a node is saved, it may impact the endpoint
   of an OpenStack service
 - when the public name of the VIP of haproxy is changed, it impacts the
   endpoint of OpenStack services
 - when the keystone proposal is applied, we may want to reapply all
   proposals that depend on keystone

What we need for this is the ability to notify about the events in the
rails application and then dispatch the notifications to hooks which
listen for them and decide if some action should be triggered.

The main reason we didn't have this in the past is that we likely don't
want to do that in the foreground of the rails application. But now that
we have delayed_job, we can send the notifications and run the hooks in
the background.

In this commit, we add the simple infrastructure about notifications and
hooks:

 - the events are defined with a name and a hash that contains the
   details of the event. The structure of the hash depends on the event.

 - a simple dispatcher exists that simply connects the hooks to the
   events.

 - the hooks only exist for service objects for the time being; a
   service object simply needs to have a event_hook method to register
   the hook, and will need to filter for the events it cares about. The
   signature of event_hook is as follows:
     def event_hook(role, event, details)

It could be argued that the hooks should be registered for some specific
events (hence moving the filter to the event dispatcher), but it's not
worth the complexity for now.
@scottwulf scottwulf merged commit 1f2af50 into crowbar:master Aug 30, 2017
@scottwulf scottwulf deleted the event-hooks branch August 30, 2017 08:54
@matelakat
Copy link
Contributor

@toabctl
Copy link
Contributor

toabctl commented Aug 31, 2017

@scottwulf we usually wait for 2 approvals before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants