Skip to content

Commit

Permalink
modified sync games once after 3 hours from start_at
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso committed Oct 26, 2023
1 parent 0cc007c commit c3de0d1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
### Modified
- sync games once after 3 hours from start_at

## [1.0.3] - 2023-10-25
### Added
- links to other player's fantasy teams
Expand Down
9 changes: 4 additions & 5 deletions app/jobs/games/import_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ module Games
class ImportJob < ApplicationJob
queue_as :default

def perform(game_id:)
game = Game.find_by(id: game_id)
return unless game

Games::ImportService.call(game: game)
def perform(game_ids:)
Game.where(id: game_ids).each do |game|
Games::ImportService.call(game: game)
end
end
end
end
25 changes: 14 additions & 11 deletions app/jobs/scheduler_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ def change_weeks
end

def import_games
# fetch game statistics 2 times, after 2-2.5 hours and 3-3.5 hours after game start
# game started at 5:00 will be fetched at 7:00 and 8:00
# game started at 5:30 will be fetched at 8:00 and 9:00
Game
.joins(:week)
.where(weeks: { status: Week::ACTIVE })
.where('start_at > ? AND start_at < ?', 100.minutes.ago, 220.minutes.after)
.pluck(:id)
.each do |game_id|
Games::ImportJob.perform_later(game_id: game_id)
end
# fetch game statistics 1 time, 3-3.5 hours after game start and no points before
# game started at 5:00 will be fetched at 8:00
# game started at 5:30 will be fetched at 9:00
Season.active.pluck(:id).each do |season_id|
game_ids =
Game
.joins(:week)
.where(weeks: { status: Week::ACTIVE, season_id: season_id })
.where('start_at < ?', 165.minutes.ago)
.where(points: [])
.pluck(:id)

Games::ImportJob.perform_later(game_ids: game_ids) if game_ids.any?
end
end
end
6 changes: 3 additions & 3 deletions spec/jobs/games/import_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

describe Games::ImportJob, type: :service do
subject(:job_call) { described_class.perform_now(game_id: game_id) }
subject(:job_call) { described_class.perform_now(game_ids: game_ids) }

let!(:game) { create :game }

Expand All @@ -10,7 +10,7 @@
end

context 'for unexisting game' do
let(:game_id) { 'unexisting' }
let(:game_ids) { ['unexisting'] }

it 'does not call service' do
job_call
Expand All @@ -20,7 +20,7 @@
end

context 'for existing game' do
let(:game_id) { game.id }
let(:game_ids) { [game.id] }

it 'calls service' do
job_call
Expand Down
26 changes: 24 additions & 2 deletions spec/jobs/scheduler_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let!(:week2) {
create :week, status: Week::INACTIVE, season: season, position: 2, deadline_at: DateTime.new(2023, 1, 14, 0, 10, 0)
}
let!(:game) { create :game, week: week1, start_at: DateTime.new(2023, 1, 8, 2, 0, 0) }
let!(:game) { create :game, week: week1, start_at: DateTime.new(2023, 1, 7, 21, 0, 0) }

before do
allow(Weeks::ChangeService).to receive(:call)
Expand Down Expand Up @@ -70,7 +70,29 @@
job_call

expect(Weeks::ChangeService).not_to have_received(:call)
expect(Games::ImportJob).to have_received(:perform_later).with(game_id: game.id)
expect(Games::ImportJob).to have_received(:perform_later).with(game_ids: [game.id])
end

context 'when game has points' do
before { game.update!(points: [1, 2]) }

it 'does not call game import job', :aggregate_failures do
job_call

expect(Weeks::ChangeService).not_to have_received(:call)
expect(Games::ImportJob).not_to have_received(:perform_later)
end
end

context 'when game is not finished yet' do
before { game.update!(start_at: DateTime.new(2023, 1, 7, 22, 0, 0)) }

it 'does not call game import job', :aggregate_failures do
job_call

expect(Weeks::ChangeService).not_to have_received(:call)
expect(Games::ImportJob).not_to have_received(:perform_later)
end
end
end
end

0 comments on commit c3de0d1

Please sign in to comment.