-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
b773ee0
commit ec94752
Showing
16 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash -e | ||
bundle exec shoryuken -R -C config/shoryuken.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
if Rails.env.production? | ||
Shoryuken.configure_client do |config| | ||
config.sqs_client = Aws::SQS::Client.new( | ||
region: EnvConfig.DATABASE_AWS_REGION, | ||
credentials: Aws::ECSCredentials.new, | ||
) | ||
end | ||
|
||
Shoryuken.configure_server do |config| | ||
config.sqs_client = Aws::SQS::Client.new( | ||
region: EnvConfig.DATABASE_AWS_REGION, | ||
credentials: Aws::ECSCredentials.new, | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
queues: | ||
- <%= EnvConfig.REGISTRATION_QUEUE %> | ||
delay: 1 | ||
concurrency: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Define the SQS FIFO queue | ||
resource "aws_sqs_queue" "this" { | ||
name = "registrations-monolith.fifo" | ||
fifo_queue = true | ||
content_based_deduplication = true | ||
deduplication_scope = "queue" | ||
delay_seconds = 0 | ||
max_message_size = 262144 | ||
message_retention_seconds = 345600 | ||
receive_wait_time_seconds = 0 | ||
visibility_timeout_seconds = 60 | ||
tags = { | ||
Env = "Production" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
resource "aws_cloudwatch_log_group" "worker" { | ||
name = "${var.name_prefix}-sqs-worker" | ||
} | ||
|
||
resource "aws_ecs_task_definition" "worker" { | ||
family = "${var.name_prefix}-sqs-worker" | ||
|
||
network_mode = "awsvpc" | ||
requires_compatibilities = ["EC2"] | ||
|
||
# We configure the roles to allow `aws ecs execute-command` into a task, | ||
# as in https://aws.amazon.com/blogs/containers/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2 | ||
execution_role_arn = aws_iam_role.task_execution_role.arn | ||
task_role_arn = aws_iam_role.task_role.arn | ||
|
||
cpu = "256" | ||
memory = "256" | ||
|
||
container_definitions = jsonencode([ | ||
|
||
{ | ||
name = "sqs-worker-staging" | ||
image = "${var.shared.ecr_repository.repository_url}:production-sqs-worker" | ||
cpu = 256 | ||
memory = 256 | ||
portMappings = [] | ||
logConfiguration = { | ||
logDriver = "awslogs" | ||
options = { | ||
awslogs-group = aws_cloudwatch_log_group.this.name | ||
awslogs-region = var.region | ||
awslogs-stream-prefix = var.name_prefix | ||
} | ||
} | ||
environment = local.rails_environment | ||
healthCheck = { | ||
command = ["CMD-SHELL", "pgrep ruby || exit 1"] | ||
interval = 30 | ||
retries = 3 | ||
startPeriod = 60 | ||
timeout = 5 | ||
} | ||
}, | ||
]) | ||
|
||
tags = { | ||
Name = var.name_prefix | ||
} | ||
} | ||
|
||
data "aws_ecs_task_definition" "worker" { | ||
task_definition = aws_ecs_task_definition.worker.family | ||
} | ||
|
||
resource "aws_ecs_service" "worker" { | ||
name = "${var.name_prefix}-sqs-worker" | ||
cluster = var.shared.ecs_cluster.id | ||
# During deployment a new task revision is created with modified | ||
# container image, so we want use data.aws_ecs_task_definition to | ||
# always point to the active task definition | ||
task_definition = data.aws_ecs_task_definition.auxiliary.arn | ||
desired_count = 0 | ||
scheduling_strategy = "REPLICA" | ||
deployment_maximum_percent = 200 | ||
deployment_minimum_healthy_percent = 50 | ||
health_check_grace_period_seconds = 0 | ||
|
||
capacity_provider_strategy { | ||
capacity_provider = var.shared.t3_capacity_provider.name | ||
weight = 1 | ||
} | ||
|
||
enable_execute_command = true | ||
|
||
deployment_circuit_breaker { | ||
enable = true | ||
rollback = false | ||
} | ||
|
||
ordered_placement_strategy { | ||
type = "spread" | ||
field = "attribute:ecs.availability-zone" | ||
} | ||
|
||
ordered_placement_strategy { | ||
type = "spread" | ||
field = "instanceId" | ||
} | ||
|
||
network_configuration { | ||
security_groups = [var.shared.cluster_security.id] | ||
subnets = var.shared.private_subnets[*].id | ||
} | ||
|
||
deployment_controller { | ||
type = "ECS" | ||
} | ||
|
||
tags = { | ||
Name = var.name_prefix | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Define the SQS FIFO queue | ||
resource "aws_sqs_queue" "this" { | ||
name = "registrations-monolith-staging.fifo" | ||
fifo_queue = true | ||
content_based_deduplication = true | ||
deduplication_scope = "queue" | ||
delay_seconds = 0 | ||
max_message_size = 262144 | ||
message_retention_seconds = 345600 | ||
receive_wait_time_seconds = 0 | ||
visibility_timeout_seconds = 60 | ||
tags = { | ||
Env = "staging" | ||
} | ||
} |
Oops, something went wrong.