diff --git a/lib/sg_fargate_rails/event_bridge_schedule.rb b/lib/sg_fargate_rails/event_bridge_schedule.rb index b34da07..e01c47a 100644 --- a/lib/sg_fargate_rails/event_bridge_schedule.rb +++ b/lib/sg_fargate_rails/event_bridge_schedule.rb @@ -70,7 +70,7 @@ def input_overrides_json end def convert_container_type - CONTAINER_TYPES[@container_type] + @container_type ? CONTAINER_TYPES.fetch(@container_type) : nil end def container_command @@ -93,9 +93,8 @@ def client end class << self - def parse(filename) - schedules = YAML.unsafe_load(File.open(filename))[environment] - schedules.filter_map { |name, info| EventBridgeSchedule.new(name, info['cron'], info['command'], info['container_type']) if name != '<<' } + def convert(schedules) + schedules.map { |name, info| EventBridgeSchedule.new(name.to_s, info[:cron], info[:command], info[:container_type]) } end def delete_all!(group_name) @@ -109,10 +108,6 @@ def client @client ||= Aws::Scheduler::Client.new(region: region, credentials: credentials) end - def environment - ENV['RAILS_ENV'] - end - def region ENV['AWS_REGION'] || 'ap-northeast-1' end diff --git a/lib/tasks/sg_fargate_rails.rake b/lib/tasks/sg_fargate_rails.rake index 2baa491..860ee9e 100644 --- a/lib/tasks/sg_fargate_rails.rake +++ b/lib/tasks/sg_fargate_rails.rake @@ -12,8 +12,7 @@ namespace :sg_fargate_rails do SgFargateRails::EventBridgeSchedule.delete_all!(group_name) Rails.logger.info "[EventBridgeSchedule] Register schedules in #{group_name}" - config_file = Rails.root.join('config/eventbridge_schedules.yml') - SgFargateRails::EventBridgeSchedule.parse(config_file).each do |schedule| + 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( diff --git a/spec/fixtures/event_bridge_schedule/blank_schedule.yml b/spec/fixtures/event_bridge_schedule/blank_schedule.yml deleted file mode 100644 index a518dd8..0000000 --- a/spec/fixtures/event_bridge_schedule/blank_schedule.yml +++ /dev/null @@ -1,7 +0,0 @@ -common: &common - -staging: - <<: *common - -production: - <<: *common \ No newline at end of file diff --git a/spec/fixtures/event_bridge_schedule/schedule.yml b/spec/fixtures/event_bridge_schedule/schedule.yml deleted file mode 100644 index 640b0b8..0000000 --- a/spec/fixtures/event_bridge_schedule/schedule.yml +++ /dev/null @@ -1,11 +0,0 @@ -common: &common - daily_backup_to_s3: - command: 'jobmon --estimate-time=3000 sg_tiny_backup:backup' - cron: 'cron(30 1 * * ? *)' - container_type: medium - -staging: - <<: *common - -production: - <<: *common \ No newline at end of file diff --git a/spec/sg_fargate_rails/event_bridge_schedule_spec.rb b/spec/sg_fargate_rails/event_bridge_schedule_spec.rb index cfe760b..175e605 100644 --- a/spec/sg_fargate_rails/event_bridge_schedule_spec.rb +++ b/spec/sg_fargate_rails/event_bridge_schedule_spec.rb @@ -48,15 +48,10 @@ end end - describe '#parse' do + describe '.convert' do context 'scheduleの登録がない場合' do - let(:filename) { 'spec/fixtures/event_bridge_schedule/blank_schedule.yml' } - it do - allow(ENV).to receive(:[]).and_call_original - allow(ENV).to receive(:[]).with('RAILS_ENV').and_return('staging') - - expect(SgFargateRails::EventBridgeSchedule.parse(filename)).to eq [] + expect(SgFargateRails::EventBridgeSchedule.convert({})).to eq [] end end @@ -64,10 +59,13 @@ let(:filename) { 'spec/fixtures/event_bridge_schedule/schedule.yml' } it do - allow(ENV).to receive(:[]).and_call_original - allow(ENV).to receive(:[]).with('RAILS_ENV').and_return('staging') - - results = SgFargateRails::EventBridgeSchedule.parse(filename) + results = SgFargateRails::EventBridgeSchedule.convert({ + daily_backup_to_s3: { + command: 'jobmon --estimate-time=3000 sg_tiny_backup:backup', + cron: 'cron(30 1 * * ? *)', + container_type: 'medium', + } + }) expect(results.size).to eq 1 expect(results.first.name).to eq 'daily_backup_to_s3' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 84092eb..94318b3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,3 @@ require 'sg_fargate_rails' require 'rubygems' require 'rspec' -require 'yaml' \ No newline at end of file