Skip to content

Commit

Permalink
Add way to run a single group
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxagon committed Apr 1, 2024
1 parent aa76304 commit 7c7b0ad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
10 changes: 8 additions & 2 deletions app/services/rakes/run_matchmaking.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/tasks/matchmaking.rake
Original file line number Diff line number Diff line change
@@ -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
6 changes: 1 addition & 5 deletions lib/tasks/scheduler.rake
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions test/services/rakes/run_matchmaking_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7c7b0ad

Please sign in to comment.