Skip to content

Commit ec94752

Browse files
authored
Create Shoryuken Worker configuration (#10058)
* Create Shoryuken config * example infra * add queue URL to env variables * update gemfile.lock * add sqs worker service * add building the sqs image to github action * set desired count to 0 for prod * monkeypatch enqueue_after_transaction_commit? for ShoryukenAdapter * also install aws-sdk-sqs * correct monkey_patches.rb * fix worker using the wrong task definition * add init script to set region * fix whitespace
1 parent b773ee0 commit ec94752

File tree

16 files changed

+328
-0
lines changed

16 files changed

+328
-0
lines changed

.github/actions/build-environment/action.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ runs:
4747
tag: ${{inputs.environment}}-api
4848
build_tag: ${{ inputs.build_tag }}
4949
environment: ${{ inputs.environment }}
50+
- name: Build and push SQS Worker
51+
uses: ./.github/actions/build-image
52+
with:
53+
target: shoryuken
54+
registry: ${{ inputs.registry }}
55+
tag: ${{inputs.environment}}-sqs-worker
56+
build_tag: ${{ inputs.build_tag }}
57+
environment: ${{ inputs.environment }}
5058
- name: Build and push Image
5159
uses: ./.github/actions/build-image
5260
with:

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ RUN gem install mailcatcher
8787

8888
ENTRYPOINT ["/rails/bin/docker-entrypoint-sidekiq"]
8989

90+
FROM runtime AS shoryuken
91+
92+
USER rails:rails
93+
94+
ENTRYPOINT ["/rails/bin/docker-entrypoint-shoryuken"]
95+
9096
FROM runtime AS monolith
9197

9298
EXPOSE 3000

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ gem 'ostruct'
6565
gem 'selectize-rails', github: 'jfly/selectize-rails'
6666

6767
gem 'aws-sdk-s3'
68+
gem 'aws-sdk-sqs'
6869
gem 'aws-sdk-rds'
6970
gem 'aws-sdk-cloudfront'
7071

@@ -155,4 +156,5 @@ group :production do
155156
gem 'rack'
156157
gem 'newrelic_rpm'
157158
gem 'wkhtmltopdf-binary-ng'
159+
gem 'shoryuken'
158160
end

Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ GEM
177177
aws-sdk-core (~> 3, >= 3.210.0)
178178
aws-sdk-kms (~> 1)
179179
aws-sigv4 (~> 1.5)
180+
aws-sdk-sqs (1.87.0)
181+
aws-sdk-core (~> 3, >= 3.210.0)
182+
aws-sigv4 (~> 1.5)
180183
aws-sigv4 (1.10.1)
181184
aws-eventstream (~> 1, >= 1.0.2)
182185
babel-source (5.8.35)
@@ -705,6 +708,10 @@ GEM
705708
railties (>= 5.2)
706709
semantic_range (>= 2.3.0)
707710
shellany (0.0.1)
711+
shoryuken (6.2.1)
712+
aws-sdk-core (>= 2)
713+
concurrent-ruby
714+
thor
708715
sidekiq (7.3.4)
709716
connection_pool (>= 2.3.0)
710717
logger
@@ -825,6 +832,7 @@ DEPENDENCIES
825832
aws-sdk-cloudfront
826833
aws-sdk-rds
827834
aws-sdk-s3
835+
aws-sdk-sqs
828836
better_errors
829837
binding_of_caller
830838
blocks
@@ -911,6 +919,7 @@ DEPENDENCIES
911919
seedbank
912920
selectize-rails!
913921
shakapacker (= 8.0.2)
922+
shoryuken
914923
sidekiq
915924
sidekiq-cron!
916925
simple_form

app/jobs/add_registration_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class AddRegistrationJob < ApplicationJob
4+
self.queue_adapter = :shoryuken unless Rails.env.local?
5+
46
before_enqueue do |job|
57
_, competition_id, user_id = job.arguments
68
Rails.cache.write(CacheAccess.registration_processing_cache_key(competition_id, user_id), true)

bin/docker-entrypoint-shoryuken

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash -e
2+
bundle exec shoryuken -R -C config/shoryuken.yml

config/initializers/monkey_patches.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,13 @@ def self.migration_table_name
102102
end
103103
end
104104
end
105+
# Temporary fix until https://github.com/ruby-shoryuken/shoryuken/pull/777 or
106+
# https://github.com/rails/rails/pull/53336 is merged
107+
if Rails.env.production?
108+
ActiveJob::QueueAdapters::ShoryukenAdapter.class_eval do
109+
def enqueue_after_transaction_commit?
110+
true
111+
end
112+
end
113+
end
105114
end

config/initializers/shoryuken.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
if Rails.env.production?
4+
Shoryuken.configure_client do |config|
5+
config.sqs_client = Aws::SQS::Client.new(
6+
region: EnvConfig.DATABASE_AWS_REGION,
7+
credentials: Aws::ECSCredentials.new,
8+
)
9+
end
10+
11+
Shoryuken.configure_server do |config|
12+
config.sqs_client = Aws::SQS::Client.new(
13+
region: EnvConfig.DATABASE_AWS_REGION,
14+
credentials: Aws::ECSCredentials.new,
15+
)
16+
end
17+
end

config/shoryuken.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
queues:
2+
- <%= EnvConfig.REGISTRATION_QUEUE %>
3+
delay: 1
4+
concurrency: 2

env_config.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
mandatory :WCA_REGISTRATIONS_URL, :string
3333
mandatory :ASSET_HOST, :string
3434
mandatory :CDN_ASSETS_DISTRIBUTION_ID, :string
35+
mandatory :REGISTRATION_QUEUE, :string
3536

3637
if is_compiling_assets
3738
mandatory :V2_REGISTRATIONS_POLL_URL, :string
@@ -57,6 +58,8 @@
5758
optional :WCA_REGISTRATIONS_POLL_URL, :string, ''
5859
optional :PAYPAL_BASE_URL, :string, ''
5960
optional :WRC_WEBHOOK_URL, :string, ''
61+
optional :REGISTRATION_QUEUE, :string, ''
62+
6063
optional :V2_REGISTRATIONS_POLL_URL, :string, ''
6164
optional :V3_REGISTRATIONS_POLL_URL, :string, ''
6265

0 commit comments

Comments
 (0)