Skip to content

Commit c05fefa

Browse files
committed
sidekiq integration
1 parent e0e4547 commit c05fefa

5 files changed

Lines changed: 146 additions & 24 deletions

File tree

lib/ontologies_linked_data.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
# Require base model
4141
require 'ontologies_linked_data/models/base'
4242

43-
43+
# Require base job
44+
require 'ontologies_linked_data/jobs/base'
4445

4546

4647
# Require all models and services
@@ -67,6 +68,11 @@
6768
require m
6869
end
6970

71+
# We need to require deterministic - that is why we have the sort.
72+
jobs = Dir.glob("#{project_root}/ontologies_linked_data/jobs/**/*.rb").sort
73+
jobs.each do |job|
74+
require job
75+
end
7076

7177

7278
module LinkedData
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'sidekiq'
2+
module LinkedData
3+
module Jobs
4+
class Base
5+
include Sidekiq::Job
6+
sidekiq_options queue: 'default'
7+
8+
# Base class for non-retryable errors
9+
class HandledException < StandardError; end
10+
class NonRetryableError < HandledException; end
11+
12+
sidekiq_retry_in do |count, exception|
13+
case exception
14+
when NonRetryableError
15+
:kill
16+
else
17+
nil
18+
end
19+
end
20+
end
21+
end
22+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'ontologies_linked_data/config/config'
2+
3+
module LinkedData
4+
module Jobs
5+
class BatchIndexJob < LinkedData::Jobs::Base
6+
7+
class ModelNotFoundError < NonRetryableError; end
8+
class ModelNotIndexableError < NonRetryableError; end
9+
10+
def perform(model_name)
11+
raise InvalidParameterError, 'model_name parameter is required' if model_name.blank?
12+
13+
model = Goo.model_by_name(model_name.to_sym)
14+
raise ModelNotIndexableError, "#{model_name} is not indexable" if model.nil? || !model.index_enabled?
15+
16+
batch_index(model)
17+
end
18+
19+
private
20+
def batch_index(model)
21+
all_attrs = get_attributes_to_include([:all], model)
22+
collections = model.where.include(all_attrs).all
23+
24+
indexed = []
25+
not_indexed = []
26+
collections.each do |m|
27+
begin
28+
response = m.index.dig('responseHeader', 'status')
29+
if response.eql?(0)
30+
indexed << m.id
31+
else
32+
not_indexed << m.id
33+
logger.error "Failed to index: #{m.id} - status: #{response}"
34+
end
35+
rescue StandardError => e
36+
not_indexed << m.id
37+
logger.error "Error indexing #{m.id}: #{e.message}"
38+
raise e
39+
end
40+
if (indexed.size % 100).zero?
41+
logger.info "Successfully indexed #{indexed.size} out of #{collections.size} for the model: #{model}"
42+
end
43+
end
44+
end
45+
46+
def get_attributes_to_include(includes_param, klass)
47+
ld = klass.goo_attrs_to_load(includes_param)
48+
ld.delete(:properties)
49+
ld
50+
end
51+
end
52+
end
53+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module LinkedData
2+
module Jobs
3+
class EmailNotificationJob < LinkedData::Jobs::Base
4+
sidekiq_options queue: 'mailers'
5+
6+
def perform(options = {})
7+
return unless LinkedData.settings.enable_notifications
8+
9+
headers = { 'Content-Type' => 'text/html' }
10+
sender = options['sender'] || LinkedData.settings.email_sender
11+
recipients = Array(options['recipients']).uniq
12+
raise ArgumentError, 'Recipient needs to be provided in options[:recipients]' if !recipients || recipients.empty?
13+
14+
# By default we override all recipients to avoid
15+
# sending emails from testing environments.
16+
# Set `email_disable_override` in production
17+
# to send to the actual user.
18+
unless LinkedData.settings.email_disable_override
19+
headers['Overridden-Sender'] = recipients
20+
recipients = LinkedData.settings.email_override
21+
end
22+
23+
Pony.mail({
24+
to: recipients,
25+
from: sender,
26+
subject: options['subject'],
27+
body: options['body'],
28+
headers: headers,
29+
via: :smtp,
30+
enable_starttls_auto: LinkedData.settings.enable_starttls_auto,
31+
via_options: mail_options
32+
})
33+
end
34+
35+
private
36+
37+
def mail_options
38+
options = {
39+
address: LinkedData.settings.smtp_host,
40+
port: LinkedData.settings.smtp_port,
41+
domain: LinkedData.settings.smtp_domain # the HELO domain provided by the client to the server
42+
}
43+
44+
if LinkedData.settings.smtp_auth_type && LinkedData.settings.smtp_auth_type != :none
45+
options.merge!({
46+
user_name: LinkedData.settings.smtp_user,
47+
password: LinkedData.settings.smtp_password,
48+
authentication: LinkedData.settings.smtp_auth_type
49+
})
50+
end
51+
52+
options
53+
end
54+
end
55+
end
56+
end

lib/ontologies_linked_data/utils/notifier.rb

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,15 @@ class Notifier
44
def self.notify(options = {})
55
return unless LinkedData.settings.enable_notifications
66

7-
headers = { 'Content-Type' => 'text/html' }
8-
sender = options[:sender] || LinkedData.settings.email_sender
9-
recipients = Array(options[:recipients]).uniq
10-
raise ArgumentError, 'Recipient needs to be provided in options[:recipients]' if !recipients || recipients.empty?
11-
12-
# By default we override all recipients to avoid
13-
# sending emails from testing environments.
14-
# Set `email_disable_override` in production
15-
# to send to the actual user.
16-
unless LinkedData.settings.email_disable_override
17-
headers['Overridden-Sender'] = recipients
18-
recipients = LinkedData.settings.email_override
19-
end
7+
email_options = {
8+
'sender' => options[:sender],
9+
'recipients' => options[:recipients],
10+
'subject' => options[:subject],
11+
'body' => options[:body]
12+
}
2013

21-
Pony.mail({
22-
to: recipients,
23-
from: sender,
24-
subject: options[:subject],
25-
body: options[:body],
26-
headers: headers,
27-
via: :smtp,
28-
enable_starttls_auto: LinkedData.settings.enable_starttls_auto,
29-
via_options: mail_options
30-
})
14+
# Queue the email job
15+
LinkedData::Jobs::EmailNotificationJob.perform_async(email_options)
3116
end
3217

3318
def self.notify_support_grouped(subject, body)

0 commit comments

Comments
 (0)