Skip to content

Commit

Permalink
move file related listeners to their own space (#6743)
Browse files Browse the repository at this point in the history
* move file related listeners to their own space

these listener methods don't have to do with `FileMetadata`, so they should be
reorganized out into their own class. this makes it easier for downstream apps
to override behaviors that are only related to files.

* fix typo "Listener" -> "Listeners"

---------

Co-authored-by: tamsin woo <[email protected]>
  • Loading branch information
tamsin woo and tamsin woo authored Mar 28, 2024
1 parent 27499ad commit b1789c0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
39 changes: 39 additions & 0 deletions app/services/hyrax/listeners/file_listener.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Hyrax
module Listeners
##
# Listens for events related to Files ({Valkyrie::StorageAdapter::File})
class FileListener
##
# Called when 'file.characterized' event is published;
# allows post-characterization handling, like derivatives generation.
#
# @param [Dry::Events::Event] event
# @return [void]
def on_file_characterized(event)
file_set = event[:file_set]

case file_set
when ActiveFedora::Base # ActiveFedora
CreateDerivativesJob
.perform_later(file_set, event[:file_id], event[:path_hint])
else
ValkyrieCreateDerivativesJob
.perform_later(file_set.id.to_s, event[:file_id])
end
end

##
# Called when 'file.uploaded' event is published
# @param [Dry::Events::Event] event
# @return [void]
def on_file_uploaded(event)
# Run characterization for original file only
return unless event[:metadata]&.original_file?

ValkyrieCharacterizationJob.perform_later(event[:metadata].id.to_s)
end
end
end
end
30 changes: 0 additions & 30 deletions app/services/hyrax/listeners/file_metadata_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,6 @@ module Listeners
##
# Listens for events related to {Hyrax::FileMetadata}
class FileMetadataListener
##
# Called when 'file.characterized' event is published;
# allows post-characterization handling, like derivatives generation.
#
# @param [Dry::Events::Event] event
# @return [void]
def on_file_characterized(event)
file_set = event[:file_set]

case file_set
when ActiveFedora::Base # ActiveFedora
CreateDerivativesJob
.perform_later(file_set, event[:file_id], event[:path_hint])
else
ValkyrieCreateDerivativesJob
.perform_later(file_set.id.to_s, event[:file_id])
end
end

##
# Called when 'file.metadata.updated' event is published; reindexes a
# {Hyrax::FileSet} when a file claiming to be its `pcdm_use:OriginalFile`
Expand All @@ -41,17 +22,6 @@ def on_file_metadata_updated(event)
"encountered an error #{err.message}. should this " \
"object be in a FileSet #{event[:metadata]}"
end

##
# Called when 'file.uploaded' event is published
# @param [Dry::Events::Event] event
# @return [void]
def on_file_uploaded(event)
# Run characterization for original file only
return unless event[:metadata]&.original_file?

ValkyrieCharacterizationJob.perform_later(event[:metadata].id.to_s)
end
end
end
end
11 changes: 11 additions & 0 deletions lib/hyrax/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ class Publisher

# @since 3.5.0
# @macro a_registered_event
# @note this event SHOULD be published file is characteried by the
# characterization service. Normally, this should happen close to
# when the `file.metadata.updated` event (since characterization
# typically involves updating the metadata). Listeners that intend
# to track updates to file metadata should listen on that event
# topic.
# The event payload MUST include a `:file_set` ({Hyrax::FileSet}),
# a `:file_id` (the id of the {Valkyrie::StorageAdapter::File}),
# and MAY have a `path_hint` for a local path / cache for the
# file.
register_event('file.characterized')

# @since 3.3.0
Expand Down Expand Up @@ -208,6 +218,7 @@ 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,
Expand Down

0 comments on commit b1789c0

Please sign in to comment.