Class: AddOns::GenerateOpeningHoursWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/add_ons/generate_opening_hours_worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Attribute Details

#add_onObject (readonly)

Returns the value of attribute add_on.



6
7
8
# File 'app/workers/add_ons/generate_opening_hours_worker.rb', line 6

def add_on
  @add_on
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



6
7
8
# File 'app/workers/add_ons/generate_opening_hours_worker.rb', line 6

def restaurant
  @restaurant
end

#restaurant_add_onObject (readonly)

Returns the value of attribute restaurant_add_on.



6
7
8
# File 'app/workers/add_ons/generate_opening_hours_worker.rb', line 6

def restaurant_add_on
  @restaurant_add_on
end

Instance Method Details

#generate(restaurant_add_on_id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/workers/add_ons/generate_opening_hours_worker.rb', line 23

def generate(restaurant_add_on_id)
  return unless setup_variables(restaurant_add_on_id)

  date = Time.now_in_tz(restaurant.time_zone).to_date
  x_days = 6

  opening_hours = []
  dates = date.upto(date + x_days.days).sort_by(&:cwday)

  dates.each do |day|
    schedule = time_slots(day)

    if schedule[:open].nil?
      next if restaurant_add_on.end_date.blank?

      loop do
        break if restaurant_add_on.end_date < day

        day += 7.days
        schedule = time_slots(day)
        break if schedule[:open].present?
      end
    end

    schedule[:open] = schedule[:close] = 'closed' if schedule[:open].blank?
    opening_hour = schedule.merge(cwday: day.cwday, restaurant_add_on_id: restaurant_add_on_id)
    opening_hours.push(opening_hour)
  end

  Retriable.retriable on: [ActiveRecord::Deadlocked], tries: 10 do
    ::AddOns::OpeningHour.import(
      opening_hours, raise_error: true,
                     on_duplicate_key_update: %i[cwday open close restaurant_add_on_id]
    )
  end
  restaurant_add_on.touch
end

#perform(restaurant_add_on_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/workers/add_ons/generate_opening_hours_worker.rb', line 8

def perform(restaurant_add_on_id)
  return unless setup_variables(restaurant_add_on_id)

  cache_key = [
    restaurant.inv_cache_key,
    restaurant_add_on.cache_key,
    add_on.cache_key,
    add_on.agendas.cache_key,
  ]
  Rails.cache.fetch("#{self.class}:#{restaurant_add_on_id}:#{CityHash.hash32(cache_key)}",
                    expires_in: CACHEFLOW.generate_expiry) do
    generate(restaurant_add_on_id)
  end
end