Sha256: 5246eadbd545d94bd3520bec2431b06a9f655604e682f14d9fa0790d328ab555

Contents?: true

Size: 1.41 KB

Versions: 18

Compression:

Stored size: 1.41 KB

Contents

module CanvasSync
  module JobBatches
    module BatchAwareJob
      extend ActiveSupport::Concern

      included do
        around_perform do |job, block|
          if (@bid) # This _must_ be @bid - not just bid
            prev_batch = Thread.current[:batch]
            begin
              Thread.current[:batch] = Batch.new(@bid)
              block.call
              Thread.current[:batch].save_context_changes
              Batch.process_successful_job(@bid, job_id)
            rescue
              Batch.process_failed_job(@bid, job_id)
              raise
            ensure
              Thread.current[:batch] = prev_batch
            end
          else
            block.call
          end
        end

        around_enqueue do |job, block|
          if (batch = Thread.current[:batch])
            @bid = batch.bid
            batch.increment_job_queue(job_id) if @bid
          end
          block.call
        end
      end

      def bid
        @bid || Thread.current[:batch]&.bid
      end

      def batch
        Thread.current[:batch]
      end

      def batch_context
        batch&.context || {}
      end

      def valid_within_batch?
        batch.valid?
      end

      def serialize
        super.tap do |data|
          data['batch_id'] = @bid # This _must_ be @bid - not just bid
        end
      end

      def deserialize(data)
        super
        @bid = data['batch_id']
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
canvas_sync-0.17.23 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.23.beta8 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.23.beta7 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.23.beta6 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.23.beta5 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.23.beta4 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.23.beta2 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.23.beta1 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.20 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.19 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.18 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.17.beta1 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.16 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.15 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.14 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.13 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.12 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.10.beta2 lib/canvas_sync/job_batches/batch_aware_job.rb