Sha256: 44a3fa6ba4fc5c2dfc7f28b4c2360d61d742faabc4e5ba244a64a20b38b6003d

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 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 SuccessfulFailure => err
              Thread.current[:batch].save_context_changes
              Batch.process_successful_job(@bid, job_id)
              raise
            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])
            batch.increment_job_queue(job_id) if (@bid = batch.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

2 entries across 2 versions & 1 rubygems

Version Path
canvas_sync-0.17.5.beta2 lib/canvas_sync/job_batches/batch_aware_job.rb
canvas_sync-0.17.5.beta1 lib/canvas_sync/job_batches/batch_aware_job.rb