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
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
|
# File 'app/workers/ping_worker.rb', line 10
def perform
begin
SEMAPHORE.with do |glory|
condition = Terminal.condition
Sidekiq::Logging.logger.info "Requesting with updated_at: #{Terminal.providers_updated_at}"
begin
response = RestClient::Request.execute(
:method => :post,
:url => "#{Terminal.config.host}/terminal_pings",
:timeout => 40,
:open_timeout => 60,
:headers => {
"Content-Type" => "application/json",
"Accept" => "application/json"
},
:payload => JSON.dump(
:terminal => Terminal.keyword,
:terminal_ping => condition
)
)
rescue Exception => e
Sidekiq::Logging.logger.warn e.to_s
return
end
begin
response = JSON.parse(response.to_s, :symbolize_names => true)
rescue Exception => e
Sidekiq::Logging.logger.warn "Unable to parse JSON: #{e.to_s}"
return
end
Sidekiq::Logging.logger.info "Response: #{response.inspect}"
Terminal.address = response[:address]
unless Terminal.modified_at == response[:profile][:modified_at]
Terminal.support_phone = response[:profile][:support_phone]
Terminal.modified_at = response[:profile][:modified_at]
Sync::LogoWorker.perform_async response[:profile][:logo]
end
response[:orders].each do |foreign|
existing = Order.find_by_foreign_id(foreign[:id])
unless existing.nil?
if existing.foreign_created_at == foreign[:created_at] || !existing.complete? next
end
existing.destroy
end
foreign[:foreign_id] = foreign.delete(:id)
foreign[:foreign_created_at] = foreign.delete(:created_at)
order = Order.create!(foreign)
order.acknowledge
order.perform
end
Sync::ProvidersWorker.perform_async if response[:update_providers]
last_session = response[:last_session_started_at] || 0
Sync::SessionsWorker.perform_async(last_session )if SessionRecord.exists?([ 'started_at > ?', last_session ])
end
rescue Timeout::Error => e
Sidekiq::Logging.logger.warn "Semaphore timeout. #{e.to_s}"
end
end
|