Class: Sidekiq::Batch::Callback::Finalize

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/batch/callback.rb

Instance Method Summary collapse

Instance Method Details

#cleanup_redis(bid, callback_bid = nil) ⇒ Object



97
98
99
100
# File 'lib/sidekiq/batch/callback.rb', line 97

def cleanup_redis bid, callback_bid=nil
  Sidekiq::Batch.cleanup_redis bid
  Sidekiq::Batch.cleanup_redis callback_bid if callback_bid
end

#complete(bid, status, parent_bid) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sidekiq/batch/callback.rb', line 62

def complete(bid, status, parent_bid)
  pending, children, success = Sidekiq.redis do |r|
    r.multi do |pipeline|
      pipeline.hincrby("BID-#{bid}", "pending", 0)
      pipeline.hincrby("BID-#{bid}", "children", 0)
      pipeline.scard("BID-#{bid}-success")
    end
  end

  # if we batch was successful run success callback
  if pending.to_i.zero? && children == success
    Batch.enqueue_callbacks(:success, bid)

  elsif parent_bid
    # if batch was not successfull check and see if its parent is complete
    # if the parent is complete we trigger the complete callback
    # We don't want to run this if the batch was successfull because the success
    # callback may add more jobs to the parent batch

    Sidekiq.logger.debug {"Finalize parent complete bid: #{parent_bid}"}
    _, complete, pending, children, failure = Sidekiq.redis do |r|
      r.multi do |pipeline|
        pipeline.sadd("BID-#{parent_bid}-complete", [bid])
        pipeline.scard("BID-#{parent_bid}-complete")
        pipeline.hincrby("BID-#{parent_bid}", "pending", 0)
        pipeline.hincrby("BID-#{parent_bid}", "children", 0)
        pipeline.scard("BID-#{parent_bid}-failed")
      end
    end
    if complete == children && pending == failure
      Batch.enqueue_callbacks(:complete, parent_bid)
    end
  end
end

#dispatch(status, opts) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sidekiq/batch/callback.rb', line 21

def dispatch status, opts
  bid = opts["bid"]
  callback_bid = status.bid
  event = opts["event"].to_sym
  callback_batch = bid != callback_bid

  Sidekiq.logger.debug {"Finalize #{event} batch id: #{opts["bid"]}, callback batch id: #{callback_bid} callback_batch #{callback_batch}"}

  batch_status = Status.new bid
  send(event, bid, batch_status, batch_status.parent_bid)


  # Different events are run in different callback batches
  Sidekiq::Batch.cleanup_redis callback_bid if callback_batch
  Sidekiq::Batch.cleanup_redis bid if event == :success
end

#success(bid, status, parent_bid) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sidekiq/batch/callback.rb', line 38

def success(bid, status, parent_bid)
  return unless parent_bid

  _, _, success, _, complete, pending, children, failure = Sidekiq.redis do |r|
    r.multi do |pipeline|
      pipeline.sadd("BID-#{parent_bid}-success", [bid])
      pipeline.expire("BID-#{parent_bid}-success", Sidekiq::Batch::BID_EXPIRE_TTL)
      pipeline.scard("BID-#{parent_bid}-success")
      pipeline.sadd("BID-#{parent_bid}-complete", [bid])
      pipeline.scard("BID-#{parent_bid}-complete")
      pipeline.hincrby("BID-#{parent_bid}", "pending", 0)
      pipeline.hincrby("BID-#{parent_bid}", "children", 0)
      pipeline.scard("BID-#{parent_bid}-failed")
    end
  end
  # if job finished successfully and parent batch completed call parent complete callback
  # Success callback is called after complete callback
  if complete == children && pending == failure
    Sidekiq.logger.debug {"Finalize parent complete bid: #{parent_bid}"}
    Batch.enqueue_callbacks(:complete, parent_bid)
  end

end