Sha256: affa2f824b137ef8469e7a71dba77e881a149fa64b3359318860cbee9930a61f

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require "active_support/core_ext/class"
require "sidekiq"

module SyncMachine
  # Call EnsurePublication service via a Sidekiq job.
  class EnsurePublicationWorker
    include Sidekiq::Worker

    class_attribute :hooks

    [:build, :check_publishable, :publish, :after_publish].each do |hook|
      define_singleton_method(hook) do |&block|
        hooks[hook] = Hook.new(hook.to_s, block)
      end
    end

    def self.inherited(subclass)
      subclass.hooks = {}
    end

    def perform(subject_id, enqueue_time_str)
      subject = find_subject(subject_id)
      EnsurePublication.new(
        enqueue_time_str: enqueue_time_str,
        hooks: self.class.hooks,
        subject: subject,
        sync_module: SyncMachine.sync_module(self.class)
      ).run
    end

    private

    def find_subject(subject_id)
      sync_module = SyncMachine.sync_module(self.class)
      subject_class = sync_module.subject_class
      subject_class.find(subject_id)
    end

    # Wrap build, check_publishable, etc blocks.
    class Hook
      def initialize(name, block)
        @name = name
        @block = block
      end

      def call(*args)
        TracerAdapters.tracer_adapter.start_active_span(@name) do
          @block.call(*args)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sync_machine-1.4.0 lib/sync_machine/ensure_publication_worker.rb
sync_machine-1.3.0 lib/sync_machine/ensure_publication_worker.rb
sync_machine-1.2.0 lib/sync_machine/ensure_publication_worker.rb