Sha256: 2608f6fd13de3e69af546a92dfac7b277994b13b0710888b9e3da7e27e447cf2

Contents?: true

Size: 1.87 KB

Versions: 9

Compression:

Stored size: 1.87 KB

Contents

module CanvasSync::JobUniqueness
  module Compat
    module ActiveJob

      class ActiveJobLockContext < LockContext
        def job_scheduled_at
          job_instance&.scheduled_at
        end

        def reenqueue(schedule_in:)
          job_class.set(
            queue: job_queue.to_sym,
            wait: schedule_in,
            priortity: job_instance.priority,
          ).perform_later(*job_instance.arguments)
        end
      end

      module UniqueJobExtension
        extend ActiveSupport::Concern

        included do
          set_callback(:enqueue, :around, prepend: true) do |job, block|
            ctx = uniqueness_lock_context
            @uniqueness_cache_data = ctx.cache_data
            ctx.handle_lifecycle!(:enqueue, &block)
          end

          around_perform do |job, block|
            ctx = uniqueness_lock_context
            ctx.handle_lifecycle!(:perform, &block)
          end
        end

        def serialize
          super.tap do |data|
            data['uniqueness_cache_data'] = @uniqueness_cache_data.stringify_keys
          end
        end

        def deserialize(data)
          super
          @uniqueness_cache_data = data['uniqueness_cache_data']&.symbolize_keys
        end

        def uniqueness_lock_context
          ActiveJobLockContext.new({
            job_clazz: self.class,
            jid: self.job_id,
            args: self.arguments,
            queue: self.queue_name,
            **(@uniqueness_cache_data || {})
          }, job_instance: self)
        end
      end

      module JobExtension
        extend ActiveSupport::Concern
        include UniqueJobCommon

        class_methods do
          def ensure_uniqueness(**kwargs)
            super(**kwargs)
            include UniqueJobExtension
          end
        end
      end

      def self.configure
        ::ActiveJob::Base.include JobExtension
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
canvas_sync-0.22.9 lib/canvas_sync/job_uniqueness/compat/active_job.rb
canvas_sync-0.22.8 lib/canvas_sync/job_uniqueness/compat/active_job.rb
canvas_sync-0.22.6 lib/canvas_sync/job_uniqueness/compat/active_job.rb
canvas_sync-0.22.5 lib/canvas_sync/job_uniqueness/compat/active_job.rb
canvas_sync-0.22.4 lib/canvas_sync/job_uniqueness/compat/active_job.rb
canvas_sync-0.22.3 lib/canvas_sync/job_uniqueness/compat/active_job.rb
canvas_sync-0.22.2 lib/canvas_sync/job_uniqueness/compat/active_job.rb
canvas_sync-0.22.1 lib/canvas_sync/job_uniqueness/compat/active_job.rb
canvas_sync-0.22.0 lib/canvas_sync/job_uniqueness/compat/active_job.rb