Skip to content

Commit

Permalink
Merge pull request #50 from SonicGarden/container-size
Browse files Browse the repository at this point in the history
[review] コンテナサイズを調整可能に
  • Loading branch information
interu authored Jun 7, 2024
2 parents cf58707 + bcfc15c commit 6516669
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 32 deletions.
68 changes: 43 additions & 25 deletions lib/sg_fargate_rails/event_bridge_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class EventBridgeSchedule

attr_reader :name

def initialize(name, cron, command, container_type)
def initialize(name:, cron:, command:, container_type: 'small', storage_size_gb: nil, use_bundler: true)
@name = name
@cron = cron
@command = command
@container_type = container_type
@storage_size_gb = storage_size_gb # sizeInGiB
@use_bundler = use_bundler
end

def create_run_task(group_name:, cluster_arn:, task_definition_arn:, network_configuration:)
Expand Down Expand Up @@ -48,35 +50,38 @@ def create_run_task(group_name:, cluster_arn:, task_definition_arn:, network_con

def input_overrides_json
type = convert_container_type
if type
{
**type,
"containerOverrides": [
{
"name": "rails",
**type,
"command": container_command,
}
]
}.to_json
else
{
"containerOverrides": [
{
"name": "rails",
"command": container_command,
}
]
}.to_json
end
size = convert_storage_size
{
**type,
**size,
"containerOverrides": [
{
"name": "rails",
**type,
"command": container_command,
}
]
}.to_json
end

def convert_container_type
@container_type ? CONTAINER_TYPES.fetch(@container_type) : nil
CONTAINER_TYPES.fetch(@container_type)
end

def convert_storage_size
@storage_size_gb.present? ? { "ephemeralStorage": { "sizeInGiB": @storage_size_gb } } : {}
end

def container_command
%w[bundle exec] + @command.split(' ')
if use_bundler?
%w[bundle exec] + splitted_command
else
splitted_command
end
end

def use_bundler?
!!@use_bundler
end

private
Expand All @@ -94,9 +99,22 @@ def client
self.class.client
end

def splitted_command
if @command.is_a?(Array)
@command
else
@command.split(' ')
end
end

class << self
def convert(schedules)
schedules.to_h.map { |name, info| EventBridgeSchedule.new(name.to_s, info[:cron], info[:command], info[:container_type]) }
schedules.to_h.map { |name, info|
EventBridgeSchedule.new(
name: name.to_s,
**info.slice(:cron, :command, :container_type, :storage_size_gb, :use_bundler)
)
}
end

def delete_all!(group_name)
Expand Down
46 changes: 39 additions & 7 deletions spec/sg_fargate_rails/event_bridge_schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@
describe '.input_overrides_json' do
let(:cron) { 'cron(30 16 * * ? *)' }
let(:command) { 'jobmon --estimate-time=3000 stat' }
let(:schedule) { SgFargateRails::EventBridgeSchedule.new('name', cron, command, container_type) }

subject { schedule.input_overrides_json }

context 'container_typeが指定されている場合' do
let(:container_type) { 'small' }
let(:storage_size_gb) { 64 }
let(:container_type) { 'medium' }
let(:schedule) { SgFargateRails::EventBridgeSchedule.new(name: 'name', cron: cron, command: command, container_type: container_type, storage_size_gb: storage_size_gb) }

it 'cpuやmemoryの情報が補完されること' do
is_expected.to eq(
{
"cpu": "512",
"memory": "1024",
"cpu": "1024",
"memory": "2048",
"ephemeralStorage": { "sizeInGiB": 64 },
"containerOverrides": [
{
"name": "rails",
"cpu": "512",
"memory": "1024",
"cpu": "1024",
"memory": "2048",
"command": %w[bundle exec jobmon --estimate-time=3000 stat],
}
]
Expand All @@ -31,14 +33,18 @@
end

context 'container_typeが指定されていない場合' do
let(:container_type) { nil }
let(:schedule) { SgFargateRails::EventBridgeSchedule.new(name: 'name', cron: cron, command: command) }

it do
is_expected.to eq(
{
"cpu": "512",
"memory": "1024",
"containerOverrides": [
{
"name": "rails",
"cpu": "512",
"memory": "1024",
"command": %w[bundle exec jobmon --estimate-time=3000 stat],
}
]
Expand Down Expand Up @@ -71,4 +77,30 @@
end
end
end

describe '#container_command' do
context 'use_bundler に false が指定された場合' do
let(:schedule) { SgFargateRails::EventBridgeSchedule.new(name: 'test-task-name', cron: 'cron(30 16 * * ? *)', command: 'echo "Hello"', use_bundler: false) }

it 'containerOverrides の command に bundle exec がつかない' do
expect(schedule.container_command).to eq ['echo', '"Hello"']
end
end

context 'command に配列が指定された場合' do
let(:schedule) { SgFargateRails::EventBridgeSchedule.new(name: 'test-task-name', cron: 'cron(30 16 * * ? *)', command: ['rails', '-v']) }

it 'containerOverrides の command に指定した配列が追加される' do
expect(schedule.container_command).to eq ['bundle', 'exec', 'rails', '-v']
end
end

context 'use_bundler に false, command に配列が指定された場合' do
let(:schedule) { SgFargateRails::EventBridgeSchedule.new(name: 'test-task-name', cron: 'cron(30 16 * * ? *)', command: ['/bin/sh', '-c', 'echo', '"Hello World"'], use_bundler: false) }

it 'containerOverrides の command に指定した配列がそのまま出力される' do
expect(schedule.container_command).to eq ['/bin/sh', '-c', 'echo', '"Hello World"']
end
end
end
end

0 comments on commit 6516669

Please sign in to comment.