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
48
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
87
88
89
90
91
92
93
|
# File 'app/workers/sync/providers_worker.rb', line 11
def perform
SEMAPHORE.with do |glory|
syncs = []
Sidekiq::Logging.logger.info "Requesting providers"
begin
providers = RestClient::Request.execute(
:method => :get,
:url => "#{Terminal.config.host}/terminal_pings/providers",
:timeout => 40,
:open_timeout => 60,
:headers => {
:params => {
:terminal => Terminal.keyword
}
}
)
rescue Exception => e
Sidekiq::Logging.logger.warn e.to_s
return
end
begin
providers = JSON.parse(ActiveSupport::Gzip.decompress(providers.to_s), :symbolize_names => true)
rescue Exception => e
Sidekiq::Logging.logger.warn "Unable to parse providers JSON: #{e.to_s}"
return
end
return if !Terminal.providers_updated_at.nil? && Terminal.providers_updated_at >= providers[:updated_at]
ActiveRecord::Base.transaction do
Promotion.destroy_all
Provider.destroy_all
Group.destroy_all
providers[:groups].each do |r|
local = Group.new
local.id = r[:id]
local.title = r[:title]
local.priority = r[:priority]
local.group_id = r[:parent_id]
local.save!
unless r[:icon].blank?
syncs << ['Group', local.id, r[:icon]]
end
end
providers[:providers].each do |r|
icon = r.delete :icon
local = Provider.new
local.id = r.delete :id
local.attributes = r
local.save!
unless icon.blank?
syncs << ['Provider', local.id, icon]
end
end
providers[:promotions].each_with_index do |r, i|
Promotion.create(
:provider_id => r,
:priority => i
)
end
end
Terminal.providers_updated_at = providers[:updated_at]
Sidekiq::Logging.logger.info "Providers updated. Timestamp: #{providers[:updated_at]}"
Sidekiq::Logging.logger.info "Icons to sync: #{syncs}"
syncs.each do |args|
Sync::IconsWorker.perform_async *args
end
end
rescue Timeout::Error => e
Sidekiq::Logging.logger.warn "Semaphore timeout. #{e.to_s}"
end
|