Skip to content

Commit

Permalink
Merge pull request #27 from SonicGarden/fix-yaml-parse
Browse files Browse the repository at this point in the history
[review] Use Rails.application.config_for
  • Loading branch information
interu authored Nov 7, 2023
2 parents 7446b10 + 8789b72 commit 8ab6023
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 40 deletions.
11 changes: 3 additions & 8 deletions lib/sg_fargate_rails/event_bridge_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/tasks/sg_fargate_rails.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 0 additions & 7 deletions spec/fixtures/event_bridge_schedule/blank_schedule.yml

This file was deleted.

11 changes: 0 additions & 11 deletions spec/fixtures/event_bridge_schedule/schedule.yml

This file was deleted.

20 changes: 9 additions & 11 deletions spec/sg_fargate_rails/event_bridge_schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,24 @@
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

context 'scheduleの登録が複数存在する場合' do
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
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'sg_fargate_rails'
require 'rubygems'
require 'rspec'
require 'yaml'

0 comments on commit 8ab6023

Please sign in to comment.