Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/routines/warm_up_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class WarmUpCache
lev_routine transaction: :no_transaction

protected

def exec
Exercise.chainable_latest.find_in_batches(batch_size: 100) do |exercises|
Api::V1::Exercises::SearchRepresenter.new(items: exercises).to_hash(
user_options: { can_view_solutions: true }
)
end

VocabTerm.chainable_latest.find_in_batches(batch_size: 100) do |vocab_terms|
Api::V1::Vocabs::TermSearchRepresenter.new(items: vocab_terms).to_hash
end
end
end
3 changes: 3 additions & 0 deletions lib/tasks/cron/day.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace :cron do
Rails.logger.info 'UpdateSlugs.call'
OpenStax::RescueFrom.this { UpdateSlugs.call }

Rails.logger.info 'WarmUpCache.call'
OpenStax::RescueFrom.this { WarmUpCache.call }

Rails.logger.debug 'Finished daily cron'
end
end
3 changes: 2 additions & 1 deletion spec/lib/tasks/cron/day.rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

before { Rake::Task['cron:day'].reenable }

it 'calls the UpdateSlugs lev routine' do
it 'calls the UpdateSlugs and WarmUpCache lev routines' do
expect(UpdateSlugs).to receive(:call)
expect(WarmUpCache).to receive(:call)

Rake.application.invoke_task 'cron:day'
end
Expand Down
22 changes: 22 additions & 0 deletions spec/routines/warm_up_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rails_helper'

RSpec.describe WarmUpCache, type: :routine do
before do
5.times do
FactoryBot.create :exercise
FactoryBot.create :exercise, :published
FactoryBot.create :vocab_term, vocab_distractors_count: 1
FactoryBot.create :vocab_term, :published, vocab_distractors_count: 1
end
end

it 'caches all exercises and vocab terms' do
expect { described_class.call }.to(
change { Rails.cache.instance_variable_get(:@data).count }.by(60)
)

expect { described_class.call }.not_to(
change { Rails.cache.instance_variable_get(:@data).count }
)
end
end