Sha256: c0df943e17bbb738a3e0c2e7128c9635de67f9d4d4dcadf42a74487b42676c15

Contents?: true

Size: 1.43 KB

Versions: 11

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "securerandom"
require "sidekiq/client"

module Sidekiq
  class TransactionAwareClient
    def initialize(pool: nil, config: nil)
      @redis_client = Client.new(pool: pool, config: config)
    end

    def batching?
      Thread.current[:sidekiq_batch]
    end

    def push(item)
      # 6160 we can't support both Sidekiq::Batch and transactions.
      return @redis_client.push(item) if batching?

      # pre-allocate the JID so we can return it immediately and
      # save it to the database as part of the transaction.
      item["jid"] ||= SecureRandom.hex(12)
      AfterCommitEverywhere.after_commit { @redis_client.push(item) }
      item["jid"]
    end

    ##
    # We don't provide transactionality for push_bulk because we don't want
    # to hold potentially hundreds of thousands of job records in memory due to
    # a long running enqueue process.
    def push_bulk(items)
      @redis_client.push_bulk(items)
    end
  end
end

##
# Use `Sidekiq.transactional_push!` in your sidekiq.rb initializer
module Sidekiq
  def self.transactional_push!
    begin
      require "after_commit_everywhere"
    rescue LoadError
      raise %q(You need to add `gem "after_commit_everywhere"` to your Gemfile to use Sidekiq's transactional client)
    end

    Sidekiq.default_job_options["client_class"] = Sidekiq::TransactionAwareClient
    Sidekiq::JobUtil::TRANSIENT_ATTRIBUTES << "client_class"
    true
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sidekiq-7.3.7 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.3.6 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.3.5 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.3.4 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.3.3 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.3.2 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.3.1 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.3.0 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.2.4 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.2.3 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.2.2 lib/sidekiq/transaction_aware_client.rb