-
Notifications
You must be signed in to change notification settings - Fork 0
feat: support shoryuken processing groups #111
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'aws/sqs/configurator' | ||
| require 'yaml' | ||
| require 'erb' | ||
| require 'turtle/logger' | ||
|
|
||
| module Turtle | ||
| class Group | ||
| class << self | ||
| def to_h | ||
| @to_h ||= begin | ||
| queues = AWS::SQS::Configurator.queues! | ||
| config_groups.each_with_object({}) do |(name, attrs), groups| | ||
| groups[name] = attrs.except(:queues).merge(queues: queue_list(Array(attrs[:queues]), queues)) | ||
| queue_names = groups[name][:queues].map(&:first).join(', ') | ||
| Logger.info("Group added: #{name} concurrency: #{groups[name][:concurrency]} queues: #{queue_names}") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def to_json(*) | ||
| to_h.to_json | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def config_groups | ||
| merged = config_files.select { |f| File.exist?(f) }.each_with_object({}) do |file, groups| | ||
| groups.merge!(YAML.safe_load(ERB.new(File.read(file)).result).to_h.fetch('groups', {})) | ||
| end | ||
| merged.transform_values { |attrs| attrs.transform_keys(&:to_sym) } | ||
| end | ||
|
|
||
| def config_files | ||
| Dir[AWS::SQS::Configurator::Reader::DIR_FILES] << AWS::SQS::Configurator::Reader::MAIN_FILE | ||
| end | ||
|
|
||
| def queue_list(names, queues) | ||
| queues.select { |q| names.include?(q.name) } | ||
|
fdotoliveira marked this conversation as resolved.
|
||
| .map { |q| [q.name_formatted, q.metadata[:priority] || 1] } | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --- | ||
| default: | ||
| general: | ||
| region: 'us-east-1' | ||
| prefix: 'app_name' | ||
| environment: 'production' | ||
| queue: | ||
| dead_letter_queue: false | ||
| queues: | ||
| - name: 'grouped_solo' | ||
| metadata: | ||
| priority: 1 | ||
| - name: 'grouped_multi_a' | ||
| metadata: | ||
| priority: 2 | ||
| - name: 'grouped_multi_b' | ||
| metadata: | ||
| priority: 2 | ||
| groups: | ||
| solo_group: | ||
| concurrency: 1 | ||
| queues: | ||
| - grouped_solo | ||
| multi_group: | ||
| concurrency: 2 | ||
| queues: | ||
| - grouped_multi_a | ||
| - grouped_multi_b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| default: | ||
| general: | ||
| region: 'us-east-1' | ||
| prefix: 'app_name' | ||
| environment: 'production' | ||
| queue: | ||
| dead_letter_queue: false | ||
| queues: | ||
| - name: 'regular_a' | ||
| metadata: | ||
| priority: 5 | ||
| - name: 'regular_b' | ||
| metadata: | ||
| priority: 3 | ||
| - name: 'batch_solo' | ||
| metadata: | ||
| priority: 1 | ||
| - name: 'batch_multi_a' | ||
| metadata: | ||
| priority: 2 | ||
| - name: 'batch_multi_b' | ||
| metadata: | ||
| priority: 2 | ||
| groups: | ||
| batch_solo: | ||
| concurrency: 1 | ||
| queues: | ||
| - batch_solo | ||
| batch_multi: | ||
| concurrency: 3 | ||
| delay: 30 | ||
| polling_strategy: 'WeightedRoundRobin' # Default, declaration optional | ||
| queues: | ||
| - batch_multi_a | ||
| - batch_multi_b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Turtle::Group, type: :model do | ||
| after { described_class.instance_variable_set(:@to_h, nil) } | ||
|
|
||
| describe '.to_h' do | ||
| subject { described_class.to_h } | ||
|
|
||
| context 'without groups section' do | ||
| before { stub_const('AWS::SQS::Configurator::Reader::MAIN_FILE', './spec/fixtures/configs/queues.yml') } | ||
|
|
||
| it { is_expected.to eq({}) } | ||
| end | ||
|
|
||
| context 'with groups section' do | ||
| before { stub_const('AWS::SQS::Configurator::Reader::MAIN_FILE', './spec/fixtures/configs/with_shoryuken_groups.yml') } | ||
|
|
||
| it 'returns groups with resolved queues and attributes' do | ||
| is_expected.to eq( | ||
| 'batch_solo' => { concurrency: 1, queues: [['app_name_production_batch_solo', 1]] }, | ||
| 'batch_multi' => { | ||
| concurrency: 3, | ||
| delay: 30, | ||
| polling_strategy: 'WeightedRoundRobin', | ||
| queues: [['app_name_production_batch_multi_a', 2], ['app_name_production_batch_multi_b', 2]] | ||
| } | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'with groups in DIR_FILES' do | ||
| before do | ||
| stub_const('AWS::SQS::Configurator::Reader::DIR_FILES', './spec/fixtures/configs/with_shoryuken_groups.yml') | ||
| stub_const('AWS::SQS::Configurator::Reader::MAIN_FILE', './spec/fixtures/configs/nonexistent.yml') | ||
| end | ||
|
|
||
| it 'returns groups resolved from directory config files' do | ||
| is_expected.to eq( | ||
| 'batch_solo' => { concurrency: 1, queues: [['app_name_production_batch_solo', 1]] }, | ||
| 'batch_multi' => { | ||
| concurrency: 3, | ||
| delay: 30, | ||
| polling_strategy: 'WeightedRoundRobin', | ||
| queues: [['app_name_production_batch_multi_a', 2], ['app_name_production_batch_multi_b', 2]] | ||
| } | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'with all queues grouped' do | ||
| before { stub_const('AWS::SQS::Configurator::Reader::MAIN_FILE', './spec/fixtures/configs/with_all_queues_grouped.yml') } | ||
|
|
||
| it 'returns all groups with their attributes and queues' do | ||
| is_expected.to eq( | ||
| 'solo_group' => { concurrency: 1, queues: [['app_name_production_grouped_solo', 1]] }, | ||
| 'multi_group' => { concurrency: 2, queues: [['app_name_production_grouped_multi_a', 2], ['app_name_production_grouped_multi_b', 2]] } | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '.to_h logging' do | ||
| before { stub_const('AWS::SQS::Configurator::Reader::MAIN_FILE', './spec/fixtures/configs/with_shoryuken_groups.yml') } | ||
|
|
||
| it 'logs each group with name, concurrency and queues' do | ||
| expect(Turtle::Logger).to receive(:info).with('Group added: batch_solo concurrency: 1 queues: app_name_production_batch_solo') | ||
| expect(Turtle::Logger).to receive(:info).with('Group added: batch_multi concurrency: 3 queues: app_name_production_batch_multi_a, app_name_production_batch_multi_b') | ||
| described_class.to_h | ||
| end | ||
| end | ||
|
|
||
| describe '.to_json' do | ||
| subject { described_class.to_json } | ||
|
|
||
| before { stub_const('AWS::SQS::Configurator::Reader::MAIN_FILE', './spec/fixtures/configs/with_shoryuken_groups.yml') } | ||
|
|
||
| it 'returns groups as a valid JSON string' do | ||
| parsed = JSON.parse(subject) | ||
| expect(parsed['batch_solo']).to eq('concurrency' => 1, 'queues' => [['app_name_production_batch_solo', 1]]) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.