Class: Restaurants::SlugWorker

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

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#fetch_restaurant_data(slug, options = {}) ⇒ Object



49
50
51
52
53
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
80
81
82
83
84
85
86
# File 'app/workers/restaurants/slug_worker.rb', line 49

def fetch_restaurant_data(slug, options = {})
  url = "#{Figaro.env.HH_HOST_URL!}/api/v5/restaurants/#{slug}/slug.json"

  # Default query parameters
  default_params = {
    write: true,
    locale: 'en',
    include_packages: false,
    include_pictures: true,
    minor_version: 3,
    include_last_reviews: false,
    include_blogger_reviews: false,
  }

  # Merge default params dengan options
  query_params = default_params.merge(options)

  # convert hash to query string
  query_string = URI.encode_www_form(query_params)

  faraday = DefaultFaradayClient.create_faraday_connection do |conn|
    conn.request :json
    conn.response :json
  end

  response = faraday.get("#{url}?#{query_string}")

  # Raise error if response status is not 200
  if response.status >= 400
    HH_LOGGER.error("API returned error status \#{response.status} for slug \#{slug} with options \#{options}")
    raise Faraday::Error, "API error \#{response.status}: \#{response.body}"
  end

  response
rescue Faraday::Error => e
  HH_LOGGER.error("HTTP request failed for slug #{slug} with options #{options}: #{e.message}")
  raise e
end

#perform(restaurant_id) ⇒ Object



6
7
8
9
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
41
42
43
44
45
46
47
# File 'app/workers/restaurants/slug_worker.rb', line 6

def perform(restaurant_id)
  return if restaurant_id.blank?

  restaurant = Restaurant.find_by(id: restaurant_id)
  return if restaurant.blank?

  slug = restaurant.slug

  Retriable.retriable(on: [Errno::ECONNRESET, Faraday::ConnectionFailed, Faraday::Error], tries: 3, base_interval: 1,
                      on_retry: ->(_exception, _try, _elapsed_time, _next_interval) {
                        HH_LOGGER.warn("Retrying due to \#{exception.class}: \#{exception.message} (Attempt \#{try})")
                      }) do
    MyLocaleManager.available_locales.each do |locale|
      if ENV['RAILS_ENV_REAL'] == 'staging' && ENV['NAMESPACE'] == 'venus'
        fetch_restaurant_data(slug, {
            client_type: 'web',
            locale: 'en',
            include_packages: false,
            include_pictures: true,
            preview_mode: false,
            minor_version: 3,
            include_last_reviews: false,
            include_blogger_reviews: false
        });
      else
        fetch_restaurant_data(slug, { include_packages: true, locale: locale })
        fetch_restaurant_data(slug, { include_pictures: false, locale: locale })
        fetch_restaurant_data(slug, { include_packages: false, include_pictures: false, locale: locale })
        fetch_restaurant_data(slug, { include_packages: false, include_pictures: true, locale: locale })
        fetch_restaurant_data(
          slug,
          {
            include_packages: false, include_pictures: true, include_last_reviews: false, include_blogger_reviews: false, locale: locale
          },
        )
      end
    end

    # preview_mode is for internal team, we don't need to do cache warm up for it
    # fetch_restaurant_data(slug, { preview_mode: true })
  end
end