Sha256: 6606841669b335a422a4b608247a5e842656ad5791edcf1948154827830d21f8

Contents?: true

Size: 1.25 KB

Versions: 20

Compression:

Stored size: 1.25 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 push(item)
      # 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

20 entries across 20 versions & 1 rubygems

Version Path
sidekiq-7.2.1 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.2.0 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.1.6 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.1.5 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.1.4 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.1.3 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.1.2 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.1.1 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.1.0 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.9 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.8 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.7 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.6 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.5 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.4 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.3 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.2 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.1 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.0 lib/sidekiq/transaction_aware_client.rb
sidekiq-7.0.0.beta1 lib/sidekiq/transaction_aware_client.rb