Skip to content

Commit 605153a

Browse files
committed
Added whenever to update slug tags every day and sync user names with Accounts
1 parent 28ca991 commit 605153a

File tree

11 files changed

+65
-10
lines changed

11 files changed

+65
-10
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ gem 'activerecord-import'
154154
# Get env variables from .env file
155155
gem 'dotenv-rails'
156156

157+
# Cron job scheduling
158+
gem 'whenever'
159+
157160
group :development, :test do
158161
# Run specs in parallel
159162
gem 'parallel_tests'

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ GEM
119119
cheat (1.3.3)
120120
pager (~> 1.0)
121121
choice (0.2.0)
122+
chronic (0.10.2)
122123
codecov (0.2.12)
123124
json
124125
simplecov
@@ -524,6 +525,8 @@ GEM
524525
websocket-driver (0.7.5)
525526
websocket-extensions (>= 0.1.0)
526527
websocket-extensions (0.1.5)
528+
whenever (1.0.0)
529+
chronic (>= 0.6.3)
527530
zeitwerk (2.5.1)
528531

529532
PLATFORMS
@@ -607,6 +610,7 @@ DEPENDENCIES
607610
vcr
608611
web-console
609612
webmock
613+
whenever
610614

611615
BUNDLED WITH
612616
2.3.7

app/models/exercise.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class Exercise < ApplicationRecord
9696
scope :can_release_to_a15k, -> { where(release_to_a15k: true) }
9797
scope :not_released_to_a15k, -> { where(a15k_identifier: nil) }
9898

99-
before_validation :set_context, :set_slug_tags
99+
before_validation :set_context, :set_slug_tags!
100100

101101
def content_equals?(other_exercise)
102102
return false unless other_exercise.is_a? ActiveRecord::Base
@@ -247,7 +247,7 @@ def set_context(archive_version: nil)
247247
return
248248
end
249249

250-
def set_slug_tags
250+
def set_slug_tags!
251251
existing_book_slug_tags, other_tags = tags.partition do |tag|
252252
tag.name.starts_with? 'book-slug:'
253253
end
@@ -268,10 +268,10 @@ def set_slug_tags
268268
"module-slug:#{slug[:book]}:#{slug[:page]}"
269269
end
270270

271-
kept_book_slug_tags = existing_book_slug_tags.filter do |tag|
271+
kept_book_slug_tags, removed_book_slug_tags = existing_book_slug_tags.partition do |tag|
272272
desired_book_slugs.include? tag.name
273273
end
274-
kept_page_slug_tags = existing_page_slug_tags.filter do |tag|
274+
kept_page_slug_tags, removed_page_slug_tags = existing_page_slug_tags.partition do |tag|
275275
desired_page_slugs.include? tag.name
276276
end
277277

@@ -284,5 +284,11 @@ def set_slug_tags
284284
self.tags = non_slug_tags +
285285
kept_book_slug_tags + kept_page_slug_tags +
286286
new_book_slugs + new_page_slugs
287+
288+
# Return whether or not any tags changed
289+
!removed_book_slug_tags.empty? ||
290+
!removed_page_slug_tags.empty? ||
291+
!new_book_slugs.empty? ||
292+
!new_page_slugs.empty?
287293
end
288294
end

app/routines/update_slugs.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class UpdateSlugs
2+
lev_routine transaction: :no_transaction
3+
4+
protected
5+
6+
def exec
7+
Exercise.preload(:publication).in_batches(of: 100, load: true) do |exercises|
8+
Exercise.transaction do
9+
updated_exercise_ids = exercises.filter(&:set_slug_tags!).map(&:id)
10+
next if updated_exercise_ids.empty?
11+
12+
Exercise.where(id: updated_exercise_ids).update_all updated_at: Time.current
13+
end
14+
end
15+
end
16+
end

config/schedule.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Server time is UTC
2+
# Times below are interpreted that way
3+
4+
every(1.minute) { rake 'cron:minute' }
5+
every(1.day, at: '8 AM') { rake 'cron:day' } # Midnight-1AM Pacific/2-3AM Central/3-4AM Eastern

db/migrate/20220302162344_create_slug_tags.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class CreateSlugTags < ActiveRecord::Migration[6.1]
44
def up
55
Exercise.preload(:publication).in_batches(of: 100, load: true) do |exercises|
66
Exercise.transaction do
7-
exercises.each(&:set_slug_tags)
7+
exercises.each(&:set_slug_tags!)
88
exercises.update_all updated_at: Time.current
99
end
1010
end

lib/tasks/cron/day.rake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace :cron do
2+
task day: :log_to_stdout do
3+
Rails.logger.debug 'Starting daily cron'
4+
5+
Rails.logger.info 'UpdateSlugs.call'
6+
OpenStax::RescueFrom.this { UpdateSlugs.call }
7+
8+
Rails.logger.debug 'Finished daily cron'
9+
end
10+
end

lib/tasks/cron/minute.rake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace :cron do
2+
task minute: :log_to_stdout do
3+
Rails.logger.debug 'Starting minute cron'
4+
5+
Rails.logger.info 'rake openstax:accounts:sync:accounts'
6+
OpenStax::RescueFrom.this { Rake::Task['openstax:accounts:sync:accounts'].invoke }
7+
8+
Rails.logger.debug 'Finished minute cron'
9+
end
10+
end

spec/models/exercise_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
it { is_expected.to have_many(:exercise_tags).dependent(:destroy) }
99

1010
it 'automatically sets the context based on tags and rewrites image links' do
11-
expect_any_instance_of(Exercise).to receive(:set_slug_tags).twice
11+
# Disable set_slug_tags!
12+
expect_any_instance_of(Exercise).to receive(:set_slug_tags!).twice
1213

1314
exercise.tags = [
1415
'context-cnxmod:4ee317f2-cc23-4075-b377-51ee4d11bb61',

spec/routines/exercises/tag/xlsx_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
let!(:exercises) { (1..6).map { |ii| FactoryBot.create(:publication, number: ii).publishable } }
2222

23-
# Disable set_slug_tags
24-
before { allow_any_instance_of(Exercise).to receive(:set_slug_tags) }
23+
# Disable set_slug_tags!
24+
before { allow_any_instance_of(Exercise).to receive(:set_slug_tags!) }
2525

2626
it 'tags exercises with the sample spreadsheet' do
2727
expect { described_class.call(filename: fixture_path) }.to change { ExerciseTag.count }.by(20)

0 commit comments

Comments
 (0)