Skip to content

Commit

Permalink
Merge pull request #58 from SonicGarden/dev
Browse files Browse the repository at this point in the history
0.2.0 リリース
  • Loading branch information
morikiyo authored Sep 13, 2024
2 parents 5f5ff63 + 8038d15 commit b9f0f1f
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sg_fargate_rails (0.1.17)
sg_fargate_rails (0.2.0)
aws-sdk-ec2 (~> 1.413)
aws-sdk-scheduler (~> 1.10)
lograge (~> 0.12)
Expand Down
16 changes: 16 additions & 0 deletions lib/sg_fargate_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,20 @@ def configure
yield(config)
end
end

class GeneratorVerification
class << self
def verify_version!
if current_generator_version < Gem::Version.new('0.13.0')
raise 'sg_fargate_rails_generator のバージョンを 0.13.0 以上にあげてください'
end
end

def current_generator_version
file_path = Rails.root.join('.sg_fargate_rails_generator')
version = file_path.exist? ? file_path.read.strip : '0.0.0'
Gem::Version.new(version)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/sg_fargate_rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module SgFargateRails
class Config
attr_reader :proxy_ip_addresses
attr_accessor :middleware_enabled
attr_accessor :scheduled_state_machine_enabled

# NOTE: good_jobダッシュボードへのアクセスをproxy経由のアクセスに制限するかどうか
attr_accessor :restrict_access_to_good_job_dashboard
Expand All @@ -10,6 +11,9 @@ def initialize
self.proxy_ip_addresses = ENV['SG_PROXY_IP_ADDRESSES']
self.restrict_access_to_good_job_dashboard = Rails.env.production?
self.middleware_enabled = !Rails.env.development? && !Rails.env.test?

# NOTE: このオプションは近い将来にデフォルト true へ変更、最終的に削除される予定
self.scheduled_state_machine_enabled = false
end

def proxy_ip_addresses=(ip_addresses)
Expand Down
49 changes: 47 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: container_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 All @@ -64,6 +86,14 @@ def input_overrides_json
}.to_json
end

def container_overrides_json
type = convert_container_type
{
**type,
"command": container_command,
}.to_json
end

def convert_container_type
CONTAINER_TYPES.fetch(@container_type)
end
Expand All @@ -90,9 +120,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 +144,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
2 changes: 1 addition & 1 deletion lib/sg_fargate_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SgFargateRails
VERSION = "0.1.17"
VERSION = "0.2.0"
end
38 changes: 26 additions & 12 deletions lib/tasks/sg_fargate_rails.rake
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ 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,
},
}
)
if SgFargateRails.config.scheduled_state_machine_enabled
schedule.create_start_execution_state_machine(group_name: group_name, cluster_arn: ecs_task.cluster_arn)
else
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,
},
}
)
end
end
end

Expand Down Expand Up @@ -56,4 +61,13 @@ namespace :sg_fargate_rails do
rescue ActiveRecord::ConcurrentMigrationError
exit SgFargateRails::EXIT_CONCURRENT_MIGRATION_ERROR
end

desc 'verify generator version'
task verify_generator_version: :environment do
SgFargateRails::GeneratorVerification.verify_version!
end
end

if Rake::Task.task_defined?("assets:precompile")
Rake::Task["assets:precompile"].enhance(["sg_fargate_rails:verify_generator_version"])
end

0 comments on commit b9f0f1f

Please sign in to comment.