Skip to content

Commit bafbeed

Browse files
Merge pull request #17 from datacite/configure-sqs-connection
Configure sqs connection
2 parents 22fa8d9 + adf16e4 commit bafbeed

File tree

7 files changed

+72
-8
lines changed

7 files changed

+72
-8
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
class EventImportWorker
4+
include Shoryuken::Worker
5+
6+
shoryuken_options queue: -> { "#{ENV["RAILS_ENV"]}_events" }, auto_delete: true
7+
8+
def perform(sqs_message = nil, data = nil)
9+
log_prefix = "[Events:EventImportWorker]"
10+
11+
if data.blank?
12+
Rails.logger.info("#{log_prefix} Message data was blank")
13+
return
14+
end
15+
16+
log_identifier = "subj_id: #{data["subjId"]}, " \
17+
"obj_id: #{data["objId"]}, " \
18+
"source_id: #{data["sourceId"]}, " \
19+
"relation_type_id: #{data["relationTypeId"]}"
20+
21+
Rails.logger.info("#{log_prefix} Start of event message processing for #{log_identifier}")
22+
Rails.logger.info("#{log_prefix} Searching for event with #{log_identifier}")
23+
24+
event = Event.find_by(
25+
subj_id: data["subjId"],
26+
obj_id: data["objId"],
27+
source_id: data["sourceId"],
28+
relation_type_id: data["relationTypeId"],
29+
)
30+
31+
if event.blank?
32+
Rails.logger.info("#{log_prefix} Creating a new event with #{log_identifier}")
33+
else
34+
Rails.logger.info("#{log_prefix} Update an existing event with #{log_identifier}")
35+
end
36+
37+
Rails.logger.info("#{log_prefix} End of event message processing for #{log_identifier}")
38+
end
39+
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: 6 additions & 2 deletions
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"],
6-
region: "us-east-1",
7-
credentials: Aws::Credentials.new("test", "test"),
8+
region: ENV["AWS_REGION"],
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: 6 additions & 0 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=
@@ -13,6 +14,11 @@ services:
1314
- ES_PORT=9200
1415
- ES_SCHEME=http
1516
- ELASTIC_PASSWORD=AnUnsecurePassword123
17+
- AWS_ENDPOINT=http://localstack:4566
18+
- AWS_REGION=us-east-1
19+
- AWS_ACCESS_KEY_ID=test
20+
- AWS_SECRET_ACCESS_KEY=test
21+
- DISABLE_QUEUE_WORKER=
1622
build:
1723
context: ./
1824
dockerfile: Dockerfile

vendor/docker/shoryuken.sh

Lines changed: 2 additions & 2 deletions
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
5-
if [ -n "$AWS_REGION" ]; then
4+
# Disabled with DISABLE_QUEUE_WORKER, start shoryuken
5+
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)