Sha256: 892a7cac3246ccdcb0e2e02cf5f24f56f7ad3f07920e40fadf78d426ec02bd67

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module ExternalServices
  module Action
    extend ActiveSupport::Concern

    QUEUE_PREFIX = 'external_services'

    included do
      scope :processed,   -> { where.not(processed_at: nil) }
      scope :unprocessed, -> { where(processed_at: nil) }

      after_commit :kick_active_job
    end

    def processed?
      processed_at.present?
    end

    def set_processed!
      update! processed_at: Time.now
    end

    def execute!
      raise NotImplementedError
    end

    def kick_active_job
      # return if processed? || api_disabled?
      return if saved_changes.size == 1 && saved_change_to_options.present?

      job_class.set(queue: prefixed_queue).perform_later(id)
    end

    def prefixed_queue
      [QUEUE_PREFIX, queue].join('__')
    end

    module ClassMethods
      def clear_sidekiq_queues
        Sidekiq::Queue.all.each do |queue|
          queue.clear if queue.name.include?(QUEUE_PREFIX)
        end
      end

      def perform_unprocessed
        Rails.logger.info "Running unprocessed #{self.class.name.demodulize} api actions..."

        unprocessed.each(&:kick_active_job)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
external_services-0.5.4 app/models/concerns/external_services/action.rb