Sha256: 3390bbc93d3695d56127d1fb5ab38a8a88903d9f260d849988808e96b152916a

Contents?: true

Size: 1.48 KB

Versions: 24

Compression:

Stored size: 1.48 KB

Contents

require "active_support/concern"

module RocketJob
  module Plugins
    # Wraps every #perform call with an Active Record transaction / unit or work.
    #
    # If the perform raises an exception it will cause any database changes to be rolled back.
    #
    # For Batch Jobs the transaction is at the slice level so that the entire slice succeeds,
    # or is rolled back.
    #
    # Example:
    #   # Update User and create an Audit entry as a single database transaction.
    #   # If Audit.create! fails then the user change will also be rolled back.
    #   class MyJob < RocketJob::Job
    #     include RocketJob::Plugins::Transaction
    #
    #     def perform
    #       u = User.find(name: 'Jack')
    #       u.age = 21
    #       u.save!
    #
    #       Audit.create!(table: 'user', description: 'Changed age to 21')
    #     end
    #   end
    #
    # Performance
    # - On CRuby an empty transaction block call takes about 1ms.
    # - On JRuby an empty transaction block call takes about 55ms.
    #
    # Note:
    # - This plugin will only be activated if ActiveRecord has been loaded first.
    module Transaction
      extend ActiveSupport::Concern

      included do
        if defined?(ActiveRecord::Base)
          respond_to?(:around_slice) ? around_slice(:rocket_job_transaction) : around_perform(:rocket_job_transaction)
        end
      end

      private

      def rocket_job_transaction(&block)
        ActiveRecord::Base.transaction(&block)
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rocketjob-6.3.1 lib/rocket_job/plugins/transaction.rb
rocketjob-6.3.0 lib/rocket_job/plugins/transaction.rb
rocketjob-6.2.0 lib/rocket_job/plugins/transaction.rb
rocketjob-6.1.1 lib/rocket_job/plugins/transaction.rb
rocketjob-6.1.0 lib/rocket_job/plugins/transaction.rb
rocketjob-6.0.3 lib/rocket_job/plugins/transaction.rb
rocketjob-6.0.2 lib/rocket_job/plugins/transaction.rb
rocketjob-6.0.1 lib/rocket_job/plugins/transaction.rb
rocketjob-6.0.0 lib/rocket_job/plugins/transaction.rb
rocketjob-6.0.0.rc3 lib/rocket_job/plugins/transaction.rb
rocketjob-6.0.0.rc2 lib/rocket_job/plugins/transaction.rb
rocketjob-6.0.0.rc1 lib/rocket_job/plugins/transaction.rb
rocketjob-5.4.1 lib/rocket_job/plugins/transaction.rb
rocketjob-5.4.0 lib/rocket_job/plugins/transaction.rb
rocketjob-5.4.0.beta2 lib/rocket_job/plugins/transaction.rb
rocketjob-5.4.0.beta1 lib/rocket_job/plugins/transaction.rb
rocketjob-5.3.3 lib/rocket_job/plugins/transaction.rb
rocketjob-5.3.2 lib/rocket_job/plugins/transaction.rb
rocketjob-5.3.1 lib/rocket_job/plugins/transaction.rb
rocketjob-5.3.0 lib/rocket_job/plugins/transaction.rb