Skip to content

Commit 99886b9

Browse files
Configure sqs connection and initial event_import_worker
1 parent dc22949 commit 99886b9

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,4 @@ RUBY VERSION
393393
ruby 3.1.6p260
394394

395395
BUNDLED WITH
396-
2.5.6
396+
2.5.6

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
# DataCite Events Service
22

3-
DataCite Events Service
3+
This is the code repository for the DataCite Events REST API.
4+
5+
### Adding new Shoryuken workers
6+
7+
1. Add workers to the app/workers directory
8+
2. Ensure you set the shoryuken_options e.g. `shoryuken_options queue: -> { "#{ENV['RAILS_ENV']}\_events" }, auto_delete: true`
9+
3. Queues use environment prefixes. The prefix is set with the environment variable RAILS_ENV locally.
10+
4. Add a requires statement for your queue in config/initializers/\_shoryuken.rb file e.g. `require Rails.root.join("app/workers/event_import_worker.rb")`
11+
12+
### Starting the Shoryuken workers
13+
14+
1. Workers are disabled in development by default.
15+
2. The environment variable DISABLE_QUEUE_WORKER is used in development to switch the worker on or off when you start the container.
16+
3. The DISABLE_QUEUE_WORKER is set to nil by defalt in the docker-compose.yml i.e. DISABLE_QUEUE_WORKER=
17+
4. If you want to start workers by default when the container spins up set DISABLE_QUEUE_WORKER to a truthy value i.e. 1, TRUE, true, foobar
18+
5. Alternatively you can start the workers manually by bashing into the events_api container and running `bundle exec shoryuken -R -C config/shoryuken.yml`

app/workers/event_import_worker.rb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# frozen_string_literal: true
22

3-
# The EventImportWorker subscribes to the events queue (with environment prefix i.e. stage_events production_events).
4-
# It deqeues a message from the events queue.
5-
# Determines if the event exists:
6-
# If the event does not exist, we create a new event (mysql and opensearch)
7-
# If the event does exist, we update the existing event (mysql and opensearch)
83
class EventImportWorker
94
include Shoryuken::Worker
105

11-
# Ensures that we use the rails environment on our queue prefix
12-
shoryuken_options queue: -> { "#{ENV["RAILS_ENV"]}_events" }, auto_delete: true
6+
shoryuken_options queue: -> { "#{ENV['RAILS_ENV']}_events" }, auto_delete: true
137

14-
def perform(sqs_message = nil, data = nil)
8+
def perform(sqs_message=nil, data=nil)
159
log_prefix = "[Events:EventImportWorker]"
10+
11+
if data.blank?
12+
Rails.logger.info("#{log_prefix} Message data was blank")
13+
return
14+
end
15+
1616
log_identifier = "subj_id: #{data["subjId"]}, " \
1717
"obj_id: #{data["objId"]}, " \
1818
"source_id: #{data["sourceId"]}, " \
@@ -22,22 +22,18 @@ def perform(sqs_message = nil, data = nil)
2222
Rails.logger.info("#{log_prefix} Searching for event with #{log_identifier}")
2323

2424
event = Event.find_by(
25-
sub_id: data["subjId"],
25+
subj_id: data["subjId"],
2626
obj_id: data["objId"],
2727
source_id: data["sourceId"],
2828
relation_type_id: data["relationTypeId"],
2929
)
3030

3131
if event.blank?
32-
Rails.logger.info("#{LOG_PREFIX} Creating a new event with #{log_identifier}")
33-
# create the event in mysql
34-
# and index the event
32+
Rails.logger.info("#{log_prefix} Creating a new event with #{log_identifier}")
3533
else
36-
Rails.logger.info("#{LOG_PREFIX} Update an existing event with #{log_identifier}")
37-
# update the event in mysql
38-
# and index the event
34+
Rails.logger.info("#{log_prefix} Update an existing event with #{log_identifier}")
3935
end
4036

41-
Rails.logger.info("#{LOG_PREFIX} Update existing event with #{log_identifier}")
37+
Rails.logger.info("#{log_prefix} End of event message processing for #{log_identifier}")
4238
end
4339
end

config/application.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class Application < Rails::Application
3030
# Autoload paths
3131
# You typically add directories to autoload_paths so that classes and modules are loaded
3232
# automatically as they are referenced. This makes development more convenient
33-
# config.autoload_paths << "#{config.root}/app/models/factories"
33+
# config.autoload_paths << "#{config.root}/app/workers"
3434

3535
# Eager load paths
3636
# You typically add directories to eager_load_paths to ensure that all necessary classes
3737
# and modules are loaded at startup, improving performance in production.
38-
# config.eager_load_paths << "#{config.root}/app/models/factories"
38+
# config.eager_load_paths << "#{config.root}/app/workers"
3939

4040
config.api_only = true
4141

config/initializers/_shoryuken.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# frozen_string_literal: true
22

3+
require Rails.root.join("app/workers/event_import_worker.rb")
4+
35
if Rails.env.development?
46
Aws.config.update({
57
endpoint: ENV["AWS_ENDPOINT"],
68
region: ENV["AWS_REGION"],
7-
credentials: Aws::Credentials.new(ENV["AWS_ACCESS_KEY"], ENV["AWS_SECRET_ACCESS_KEY"]),
9+
credentials: Aws::Credentials.new(ENV["AWS_ACCESS_KEY_ID"], ENV["AWS_SECRET_ACCESS_KEY"]),
810
})
911
end
1012

13+
# This ensures that we work with queues that
14+
# have an environment prefix i.e. stage_events or development_events
1115
Shoryuken.active_job_queue_name_prefixing = true

docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ services:
22
api:
33
container_name: events_api
44
environment:
5+
- RAILS_ENV=development
56
- LOG_LEVEL=debug
67
- MYSQL_USER=root
78
- MYSQL_PASSWORD=
@@ -14,9 +15,9 @@ services:
1415
- ES_SCHEME=http
1516
- ELASTIC_PASSWORD=AnUnsecurePassword123
1617
- AWS_ENDPOINT=http://localstack:4566
17-
- AWS_ACCESS_KEY=test
18-
- AWS_SECRET_ACCESS_KEY=test
1918
- AWS_REGION=us-east-1
19+
- AWS_ACCESS_KEY_ID=test
20+
- AWS_SECRET_ACCESS_KEY=test
2021
- DISABLE_QUEUE_WORKER=
2122
build:
2223
context: ./

vendor/docker/shoryuken.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22
cd /home/app/webapp
33
exec 2>&1
4-
# If AWS_REGION is set and not explicitly disabled with DISABLE_QUEUE_WORKER, start shoryuken
4+
# Disabled with DISABLE_QUEUE_WORKER, start shoryuken
55
if [ -n "$DISABLE_QUEUE_WORKER" ]; then
66
exec /sbin/setuser app bundle exec shoryuken -R -C config/shoryuken.yml
77
fi

0 commit comments

Comments
 (0)