diff --git a/app/services/rakes/run_matchmaking.rb b/app/services/rakes/run_matchmaking.rb index 0916691..fa762f6 100644 --- a/app/services/rakes/run_matchmaking.rb +++ b/app/services/rakes/run_matchmaking.rb @@ -1,16 +1,18 @@ module Rakes class RunMatchmaking - def initialize(stdout:, stderr:) + def initialize(stdout:, stderr:, only: nil) @stdout = stdout @stderr = stderr + @only = only || [] + @identifies_nearest_date = IdentifiesNearestDate.new @collect_groups = CollectGroups.new @establish_matches_for_group = Matchmaking::EstablishMatchesForGroup.new end def call - @collect_groups.call.each do |group| + groups.each do |group| next unless should_run_today?(group.schedule) unless group.active? @@ -29,6 +31,10 @@ def call private + def groups + @collect_groups.call.select { |group| @only.any? ? @only.include?(group.name) : true } + end + def should_run_today?(schedule) @identifies_nearest_date.call(schedule).today? end diff --git a/lib/tasks/matchmaking.rake b/lib/tasks/matchmaking.rake new file mode 100644 index 0000000..4fd7018 --- /dev/null +++ b/lib/tasks/matchmaking.rake @@ -0,0 +1,19 @@ +namespace :matchmaking do + desc "Run matchmaking tasks" + task run: :environment do + [ + Rakes::RunMatchmaking.new(stdout: $stdout, stderr: $stdout), + Rakes::SendPendingNotifications.new(stdout: $stdout, stderr: $stdout), + Rakes::RemovesOldMatches.new + ].each(&:call) + end + + desc "Run single matchmaking task" + task :run_single, [:group_name] => :environment do |_task, args| + [ + Rakes::RunMatchmaking.new(stdout: $stdout, stderr: $stdout, only: [args[:group_name]]), + Rakes::SendPendingNotifications.new(stdout: $stdout, stderr: $stdout), + Rakes::RemovesOldMatches.new + ].each(&:call) + end +end diff --git a/lib/tasks/scheduler.rake b/lib/tasks/scheduler.rake index e123b6a..34be2d7 100644 --- a/lib/tasks/scheduler.rake +++ b/lib/tasks/scheduler.rake @@ -1,7 +1,3 @@ task create_groups: :environment do - [ - Rakes::RunMatchmaking.new(stdout: $stdout, stderr: $stdout), - Rakes::SendPendingNotifications.new(stdout: $stdout, stderr: $stdout), - Rakes::RemovesOldMatches.new - ].each(&:call) + Rake::Task["matchmaking:run"].invoke end diff --git a/test/services/rakes/run_matchmaking_test.rb b/test/services/rakes/run_matchmaking_test.rb index 99b1b52..22f37e8 100644 --- a/test/services/rakes/run_matchmaking_test.rb +++ b/test/services/rakes/run_matchmaking_test.rb @@ -81,5 +81,26 @@ class RunMatchmakingTest < ActiveSupport::TestCase assert_no_match(/Starting matchmaking for 'test'/, output) assert_match(/Matchmaking successfully completed/, output) end + + test "only runs specified groups" do + groups = [ + group_with(name: "test1", slack_channel_name: "group-test1", target_size: 2, schedule: :daily), + group_with(name: "test2", slack_channel_name: "group-test2", target_size: 3, schedule: :daily), + group_with(name: "test3", slack_channel_name: "group-test3", target_size: 4, schedule: :daily) + ] + + stubs { @collect_groups.call }.with { groups } + stubs(times: 2) { @identifies_nearest_date.call("daily") }.with { @jan_5 } + stubs(times: 2) { |m| @establish_matches_for_group.call(m.is_a?(MatchmakingGroup)) } + + @subject.new(stdout: stdout, stderr: stderr, only: ["test1", "test3"]).call + + output = read_output! + assert_match(/Starting matchmaking for 'test1'/, output) + assert_no_match(/Starting matchmaking for 'test2'/, output) + assert_match(/Starting matchmaking for 'test3'/, output) + assert_match(/Matchmaking successfully completed/, output) + assert_empty read_errors! + end end end