Class: GenerateCompactRestaurantsWorker

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

Overview

Generate CompactRestaurant if restaurant is belongs to a branch, then it will generate compact restaurant with branch_id = x and branch_id = nil otherwise will generate compact restaurant with restaurant_id = x and branch_id = nil

this class is called very often which causing too many jobs in the Sidekiq queue so we added a feature to skip the process is already ran recently if you need to force the worker to execute, then make skip_cache variable to be true

worker = GenerateCompactRestaurantsWorker.new
worker.skip_cache = true
worker.perform(restaurant_id)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Constructor Details

#initializeGenerateCompactRestaurantsWorker

Returns a new instance of GenerateCompactRestaurantsWorker.



20
21
22
# File 'app/workers/generate_compact_restaurants_worker.rb', line 20

def initialize
  @skip_cache = false
end

Instance Attribute Details

#restaurant_idObject

Returns the value of attribute restaurant_id.



13
14
15
# File 'app/workers/generate_compact_restaurants_worker.rb', line 13

def restaurant_id
  @restaurant_id
end

#skip_cache=(value) ⇒ Object

Sets the attribute skip_cache

Parameters:

  • value

    the value to set the attribute skip_cache to.



18
19
20
# File 'app/workers/generate_compact_restaurants_worker.rb', line 18

def skip_cache=(value)
  @skip_cache = value
end

Instance Method Details

#generate_by_branch(city_id) ⇒ Object



59
60
61
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
96
97
98
99
100
# File 'app/workers/generate_compact_restaurants_worker.rb', line 59

def generate_by_branch(city_id)
  cr = CompactRestaurant.find_or_initialize_by(branch_id: branch_id, city_id: city_id)
  return if !skip_cache && cr.persisted? && !cache_stale? && picture_is_valid?(cr)

  unless cr.updated_by == 'admin'
    cr.description_en = cr.branch.restaurants.map { |r| "<div>#{r.seo&.description_en}</div>" }.join
    cr.description_th = cr.branch.restaurants.map { |r| "<div>#{r.seo&.description_th}</div>" }.join
    cr.updated_by = 'system'
  end

  picture_attr = if cr.is_cover_set && cr.restaurant_picture_id.present? && cr.picture.present?
                   { restaurant_picture_id: cr.restaurant_picture_id }
                 else
                   # Surasit asked Saiqul to use any restaurant's picture as cover picture if there is no restaurant cover picture is set
                   picture = Restaurants::Picture.select(:restaurant_id,
                                                         :id).where(restaurant_id: cr.branch.restaurants.select(:id)).limit(1).first
                   if picture.present?
                     { restaurant_picture_id: picture.id }
                   else
                     {}
                   end
                 end

  CompactRestaurant.where(restaurant_id: restaurants.map(&:id)).destroy_all

  attributes = {
    restaurant_id: nil,
    branch_id: branch_id,
    city_id: city_id,
  }.merge(common_attributes).merge(picture_attr)
  cr.attributes = attributes
  unless cr.valid?
    cr = CompactRestaurant.find_or_initialize_by({
                                                   restaurant_id: nil,
                                                   branch_id: branch_id,
                                                   city_id: city_id,
                                                 })

    cr.attributes = common_attributes.merge(picture_attr)
  end
  cr.save!
end

#generate_by_restaurantObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/workers/generate_compact_restaurants_worker.rb', line 45

def generate_by_restaurant
  cr = CompactRestaurant.find_or_initialize_by(restaurant_id: restaurant_id)
  unless cr.updated_by == 'admin'
    cr.description_en = cr.restaurant.seo&.description_en
    cr.description_th = cr.restaurant.seo&.description_th
    cr.updated_by == 'system'
  end
  return if !skip_cache && cr.persisted? && !cache_stale? && picture_is_valid?(cr)

  attributes = common_attributes.merge(restaurant_id: restaurant_id, branch_id: nil, city_id: restaurant.city_id)
  cr.attributes = attributes
  cr.save!
end

#perform(id) ⇒ Object

Parameters:

  • id (Integer)

    Restaurant ID



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/workers/generate_compact_restaurants_worker.rb', line 25

def perform(id)
  self.restaurant_id = id
  if !restaurant.active || restaurant.date_offer_expired?(Date.today) || restaurant.has_no_offers?
    delete_compact_restaurant
    remove_non_city_branch(restaurant.branch_id) if restaurant.branch_id.present?
  elsif restaurant.branch_id.present? && has_active_branches?
    remove_non_city_branch(restaurant.branch_id)
    restaurant_city_ids = restaurants.map(&:city_id).uniq
    restaurant_city_ids.each do |city_id|
      generate_by_branch(city_id)
    end
    CompactRestaurant.where(restaurant_id: restaurant_city_ids).delete_all
  elsif restaurant.branch_id.present? && !has_active_branches?
    remove_cr_for_inactive_branch(restaurant.branch_id)
    generate_by_restaurant
  else
    generate_by_restaurant
  end
end

#restaurantObject



102
103
104
# File 'app/workers/generate_compact_restaurants_worker.rb', line 102

def restaurant
  @restaurant[restaurant_id]
end