Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[review] Use Rails.application.config_for #27

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

おかしな値指定した場合は、黙って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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails.application.config_forのパース処理自体をテストするのは無駄なので、パース結果からの変換のみをテストするように見直し。

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'