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`
- `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

Instance Attribute Details

#batch.createdObject (readonly)

Since:

  • 3.0.0



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

register_event('batch.created')

#collection.membership.updatedObject (readonly)

Since:

  • 3.0.0



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

register_event('collection.membership.updated')

#collection.metadata.updatedObject (readonly)

Since:

  • 3.0.0



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

register_event('collection.metadata.updated')

#file.downloadedObject (readonly)

Since:

  • 3.3.0



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

register_event('file.downloaded')

#file.set.attachedObject (readonly)

Since:

  • 3.0.0



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

register_event('file.set.attached')

#file.set.auditedObject (readonly)

Since:

  • 3.0.0



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

register_event('file.set.audited')

#file.set.restoredObject (readonly)

Since:

  • 3.0.0



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

register_event('file.set.restored')

#file.set.url.importedObject (readonly)

Since:

  • 3.0.0



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

register_event('file.set.url.imported')

#object.acl.updatedObject (readonly)

Since:

  • 3.0.0



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

register_event('object.acl.updated')

#object.deletedObject (readonly)

Since:

  • 3.0.0



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

register_event('object.deleted')

#object.depositedObject (readonly)

Since:

  • 3.0.0



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

register_event('object.deposited')

#object.failed_depositObject (readonly)

Since:

  • 3.0.0



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

register_event('object.failed_deposit')

#object.metadata.updatedObject (readonly)

Since:

  • 3.0.0



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

register_event('object.metadata.updated')