Class: CanvasSync::JobBatches::Batch::Callback::Finalize

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_sync/job_batches/callback.rb

Instance Method Summary collapse

Instance Method Details

#complete(bid, status, parent_bid) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/canvas_sync/job_batches/callback.rb', line 113

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

  if parent_bid && !(pending.to_i.zero? && children == success)
    # 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

    Batch.logger.debug {"Finalize parent complete bid: #{parent_bid}"}
    _, _, complete, pending, children, failure = Batch.redis do |r|
      r.multi do
        r.sadd("BID-#{parent_bid}-batches-complete", bid)
        r.sadd("BID-#{parent_bid}-batches-failed", bid)
        r.scard("BID-#{parent_bid}-batches-complete")
        r.hincrby("BID-#{parent_bid}", "pending", 0)
        r.hincrby("BID-#{parent_bid}", "children", 0)
        r.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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/canvas_sync/job_batches/callback.rb', line 54

def dispatch(status, opts)
  bid = opts["bid"]
  event = opts["event"].to_sym

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

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

  if event == :success
    if opts['origin'].present?
      origin_bid = opts['origin']['for_bid']
      _, pending, success_ran = Batch.redis do |r|
        r.multi do
          r.srem("BID-#{origin_bid}-pending_callbacks", opts['origin']['event'])
          r.scard("BID-#{origin_bid}-pending_callbacks")
          r.hget("BID-#{origin_bid}", "success")
        end
      end
      Batch.cleanup_redis(origin_bid) if pending == 0 && success_ran == 'true'
    end
    if (Batch.redis {|r| r.scard("BID-#{bid}-pending_callbacks") }) == 0
      Batch.cleanup_redis(bid)
    end
  end
end

#success(bid, status, parent_bid) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/canvas_sync/job_batches/callback.rb', line 81

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

  _, _, success, _, _, complete, pending, children, success, failure = Batch.redis do |r|
    r.multi do
      r.sadd("BID-#{parent_bid}-batches-success", bid)
      r.expire("BID-#{parent_bid}-batches-success", Batch::BID_EXPIRE_TTL)
      r.scard("BID-#{parent_bid}-batches-success")

      r.srem("BID-#{parent_bid}-batches-failed", bid)
      r.sadd("BID-#{parent_bid}-batches-complete", bid)
      r.scard("BID-#{parent_bid}-batches-complete")

      r.hincrby("BID-#{parent_bid}", "pending", 0)
      r.hincrby("BID-#{parent_bid}", "children", 0)
      r.scard("BID-#{parent_bid}-batches-success")
      r.scard("BID-#{parent_bid}-failed")
    end
  end

  # If the job finished successfully and parent batch is completed, call parent :complete callback
  # Parent :success callback will be called by its :complete callback
  if complete == children && pending == failure
    Batch.logger.debug {"Finalize parent complete bid: #{parent_bid}"}
    Batch.enqueue_callbacks(:complete, parent_bid)
  end
  if pending.to_i.zero? && children == success
    Batch.logger.debug {"Finalize parent success bid: #{parent_bid}"}
    Batch.enqueue_callbacks(:success, parent_bid)
  end
end