Class: Hyrax::Publisher

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/hyrax/publisher.rb

Overview

TODO:

audit Hyrax code (and dependencies!) for places where events should be published, but are not.

Note:

this API replaces an older ‘Hyrax::Callbacks` interface, with added thread safety and capacity for many listeners on a single publication stream.

Note:

we call this “Publisher” to differentiate from the ‘Hyrax::Event` model. This class is a `Dry::Events` publisher.

This is an application-wide publisher for Hyrax’s Pub/Sub interface.

Hyrax publishes events on a variety of streams. The streams are namespaced using ‘dry-rb`’s dot notation idiom to help with organization. Namespaces reflect the kinds of resources the event applied to.

- `batch`: events related to the performance of `BatchCreateJob`
- `collection`: events related to the lifecycle of PCDM Collections
- `file`: events related to the lifecycle of File/FileMetadata
- `file.set`: events related to the lifecycle of Hydra Works FileSets
- `object`: events related to the lifecycle of all PCDM Objects

Applications SHOULD publish events whenever the relevant actions are performed. While Hyrax provides certain out-of-the-box listeners to power (e.g.) notifications, event streams are useful for much more: implementing local logging or instrumentation, adding application-specific callback-like handlers, etc… Ensuring events are consistently published is key to their usefulness.

Below is an example of subscribing using an anonymous block. A potential disadvantage of an anonymous block is that you cannot easily unsubscribe to that block.

Below is an example of subscribing using an object. A potential advantage of subscribing with an object is that you can later unsubscribe the object.

Examples:

publishing an event

publisher = Hyrax::Publisher.instance

publisher.publish('object.deposited', object: deposited_object, user: depositing_user)

use Hyrax.publisher

Hyrax.publisher.publish('object.deposited', object: deposited_object, user: depositing_user)

subscribing to an event type/stream with a block handler

publisher = Hyrax::Publisher.instance

publisher.subscribe('object.deposited') do |event|
  do_something(event[:object])
end

subscribing to an event type/stream with an event listener.


class EventListener
  # @param event [#[]] The given event[:object] should be the deposited object.
  def on_object_deposited(event)
    do_something(event[:object])
  end
end
event_listener = EventListener.new

publisher = Hyrax::Publisher.instance

publisher.subscribe(event_listener)

# The above subscribed event_listener instance will receive an #on_object_deposited message
# with an event that has two keys: `:object` and `:user`
publisher.publish('object.deposited', object: deposited_object, user: depositing_user)

publisher.unsubscribe(event_listener)

See Also:

Registered Events collapse

Registered Events collapse

Instance Attribute Details

#batch.createdObject (readonly)

Since:

  • 3.0.0



98
# File 'lib/hyrax/publisher.rb', line 98

register_event('batch.created')

#collection.deletedObject (readonly)

Since:

  • 3.4.0



102
# File 'lib/hyrax/publisher.rb', line 102

register_event('collection.deleted')

Instance Method Details

#default_listenersObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/hyrax/publisher.rb', line 217

def default_listeners
  @default_listeners ||=
    [Hyrax::Listeners::ACLIndexListener.new,
     Hyrax::Listeners::BatchNotificationListener.new,
     Hyrax::Listeners::FileListener.new,
     Hyrax::Listeners::FileMetadataListener.new,
     Hyrax::Listeners::FileSetLifecycleListener.new,
     Hyrax::Listeners::FileSetLifecycleNotificationListener.new,
     Hyrax::Listeners::MemberCleanupListener.new,
     Hyrax::Listeners::MetadataIndexListener.new,
     Hyrax::Listeners::ObjectLifecycleListener.new,
     Hyrax::Listeners::TrophyCleanupListener.new,
     Hyrax::Listeners::WorkflowListener.new].freeze
end