-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidekiq.rb
47 lines (40 loc) · 1.26 KB
/
sidekiq.rb
1
2
3
4
5
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
# frozen_string_literal: true
require_relative 'lib/rollbar'
require 'sidekiq'
require 'hiredis'
require_relative 'lib/bill_recognizer'
require_relative 'lib/logging'
require 'timeout'
Sidekiq.configure_client do |config|
# Run only 1 thread.
config.redis = { size: 1, url: 'redis://redis' }
puts 'Sidekiq client configured.'
end
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://redis' }
puts 'Sidekiq server configured.'
end
REDIS = Redis.new(driver: :hiredis, host: 'redis')
class RecognitionWorker
include Sidekiq::Worker
def perform(id, image_url, customer_vat_number)
log(image_url, customer_vat_number)
recognizer = BillRecognizer.new(
image_url: image_url,
customer_vat_number: customer_vat_number
)
timeout_in_secs = 400
bill_attributes = Timeout.timeout(timeout_in_secs) { recognizer.recognize }
bill_attributes[:id] = id
logger.info bill_attributes
REDIS.publish 'results', bill_attributes.to_json
rescue Exception => e # rubocop:disable Lint/RescueException
Rollbar.error(e)
raise e
end
def log(image_url, customer_vat_number)
logger.info "performing recognition on #{image_url}"
logger.info "for VAT number #{customer_vat_number}"
Logging.logger = logger
end
end