Class: SpreeCmCommissioner::WaitingRoom::StampQueuePositionsJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/spree_cm_commissioner/waiting_room/stamp_queue_positions_job.rb

Constant Summary collapse

STAMP_LIMIT =
(ENV['WAITING_ROOM_POSITION_STAMP_LIMIT'] || 1000).to_i

Instance Method Summary collapse

Methods included from ApplicationJobDecorator

handle_deserialization_error, prepended

Instance Method Details

#performObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/jobs/spree_cm_commissioner/waiting_room/stamp_queue_positions_job.rb', line 10

def perform
  paths = stamp_paths
  avg_queue_to_enter = lobby_data[:avg_queue_to_enter_seconds].to_i
  total = paths.sum { |p| waiting_count(p) }
  return if total.zero?

  stamped_at = Time.zone.now
  global = 0

  paths.each do |path|
    remaining = STAMP_LIMIT - global
    break if remaining <= 0

    docs = waiting_query(path).order('queued_at').limit(remaining).get.to_a
    docs.each_slice(500) do |slice|
      firestore.batch do |b|
        slice.each do |doc|
          global += 1
          data = {
            position: global,
            queue_total: total,
            estimated_wait_seconds: avg_queue_to_enter,
            status_updated_at: stamped_at
          }
          data[:position_set_at] = stamped_at unless doc.data[:position_set_at]
          b.update(doc.ref, data)
        end
      end
    end
  end
end