Skip to content

Commit

Permalink
Merge pull request #49 from SonicGarden/eb-scheduler-start-state-machine
Browse files Browse the repository at this point in the history
[review] StateMachine を起動する EventBridge Scheduler を作成する
  • Loading branch information
interu authored Jun 28, 2024
2 parents ddd4e6d + 4fffdc3 commit 11426fb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
41 changes: 39 additions & 2 deletions lib/sg_fargate_rails/event_bridge_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def initialize(name:, cron:, command:, container_type: 'small', storage_size_gb:
@use_bundler = use_bundler
end

# TODO: 利用しなくなる (state machine に移行する) ので、このメソッドは削除する
def create_run_task(group_name:, cluster_arn:, task_definition_arn:, network_configuration:)
params = {
name: @name,
Expand Down Expand Up @@ -48,6 +49,27 @@ def create_run_task(group_name:, cluster_arn:, task_definition_arn:, network_con
client.create_schedule(params)
end

def create_start_execution_state_machine(group_name:, cluster_arn:)
params = {
name: @name,
state: 'ENABLED',
flexible_time_window: { mode: 'OFF' },
group_name: group_name,
schedule_expression: @cron,
schedule_expression_timezone: timezone,
target: {
arn: state_machine_arn(group_name, cluster_arn),
input: input_overrides_json,
retry_policy: {
maximum_event_age_in_seconds: 120,
maximum_retry_attempts: 2,
},
role_arn: role_arn_for_state_machine(group_name, cluster_arn),
},
}
client.create_schedule(params)
end

def input_overrides_json
type = convert_container_type
size = convert_storage_size
Expand Down Expand Up @@ -90,9 +112,16 @@ def timezone
ENV['TZ'] || 'Asia/Tokyo'
end

def account_id(cluster_arn)
cluster_arn.split(':')[4]
end

def role_arn_for(group_name, cluster_arn)
account_id = cluster_arn.split(':')[4]
"arn:aws:iam::#{account_id}:role/#{group_name}-eventbridge-scheduler-role"
"arn:aws:iam::#{account_id(cluster_arn)}:role/#{group_name}-eventbridge-scheduler-role"
end

def role_arn_for_state_machine(group_name, cluster_arn)
"arn:aws:iam::#{account_id(cluster_arn)}:role/#{group_name}-step-functions-state-machine-role"
end

def client
Expand All @@ -107,6 +136,14 @@ def splitted_command
end
end

def region
ENV['AWS_REGION'] || 'ap-northeast-1'
end

def state_machine_arn(group_name, cluster_arn)
"arn:aws:states:#{region}:#{account_id(cluster_arn)}:stateMachine:#{group_name}-rails-state-machine"
end

class << self
def convert(schedules)
schedules.to_h.map { |name, info|
Expand Down
14 changes: 2 additions & 12 deletions lib/tasks/sg_fargate_rails.rake
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,9 @@ namespace :sg_fargate_rails do
Rails.logger.info "[EventBridgeSchedule] Register schedules in #{group_name}"
SgFargateRails::EventBridgeSchedule.convert(Rails.application.config_for('eventbridge_schedules')).each do |schedule|
Rails.logger.info "[EventBridgeSchedule] Register schedule #{schedule.name} in #{group_name}"

# TODO: この辺で AWS の API Limit などのエラーが発生するとスケジュールが消えたままとなるので、エラーの内容に応じてリトライなどのエラー処理が必要
schedule.create_run_task(
group_name: group_name,
cluster_arn: ecs_task.cluster_arn,
task_definition_arn: ecs_task.task_definition_arn,
network_configuration: {
awsvpc_configuration: {
assign_public_ip: 'ENABLED',
security_groups: ecs_task.security_group_ids,
subnets: ecs_task.public_subnet_ids,
},
}
)
schedule.create_start_execution_state_machine(group_name: group_name, cluster_arn: ecs_task.cluster_arn)
end
end

Expand Down

0 comments on commit 11426fb

Please sign in to comment.