Sha256: 940fa3aff1f2d522b5b4f44302d2684d55443171f37b4d3aecf598e4f2203fa2

Contents?: true

Size: 1.5 KB

Versions: 21

Compression:

Stored size: 1.5 KB

Contents

module Sidekiq
  # Mixin for Sidekiq workers that enables the worker to be called via
  # Rails callback methods.
  module CallbacksWorker
    extend ActiveSupport::Concern

    mattr_accessor :workers
    self.workers = []

    included do
      thread_cattr_accessor :enabled, :inlined
      CallbacksWorker.workers << self
    end

    class_methods do
      def enabled?
        enabled.nil? || !!enabled
      end

      def disabled?
        !enabled?
      end

      def enable
        self.enabled = true
      end

      def disable
        self.enabled = false
      end

      def inlined?
        !!inlined
      end

      def inline
        self.inlined = true
      end

      def async
        self.inlined = false
      end

      def enqueue_on
        get_sidekiq_options['enqueue_on'] ||
          get_sidekiq_options[:enqueue_on] ||
          {}
      end

      def callbacks
        enqueue_on.except(:with, 'with', :ignore_if, 'ignore_if', :only_if, 'only_if')
      end

      def find_callback_args(model)
        with = enqueue_on[:with] || enqueue_on['with']

        if with.present?
          model.instance_exec(&with)
        else
          [model.id.to_s]
        end
      end

      def perform_callback?(model)
        only_if = enqueue_on[:only_if] || enqueue_on['only_if']
        ignore_if = enqueue_on[:ignore_if] || enqueue_on['ignore_if']

        (only_if.blank? || !!model.instance_exec(&only_if)) &&
          (ignore_if.blank? || !model.instance_exec(&ignore_if))
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
workarea-core-3.5.4 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.26 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.5.3 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.25 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.5.2 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.24 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.5.1 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.23 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.22 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.5.0 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.21 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.5.0.beta.1 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.20 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.19 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.18 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.17 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.16 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.15 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.14 app/workers/sidekiq/callbacks_worker.rb
workarea-core-3.4.13 app/workers/sidekiq/callbacks_worker.rb