Skip to content

Commit ac40327

Browse files
committed
Add a fast unlock computing implementation
1 parent 5348ee6 commit ac40327

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ gem 'newrelic_rpm', '~> 4.8', '>= 4.8.0.341'
1717
gem 'paperclip', '~> 5.0'
1818
gem 'pdfkit', '~> 0.8.2'
1919
gem 'pg', '~> 0.19.0'
20+
gem 'activerecord-import', '~> 0.28.1'
2021
gem 'rack-cors'
2122
gem 'rake'
2223
gem 'responders', '~> 2.0'

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ GEM
3030
activemodel (= 4.2.10)
3131
activesupport (= 4.2.10)
3232
arel (~> 6.0)
33+
activerecord-import (0.28.1)
34+
activerecord (>= 3.2)
3335
activerecord-session_store (1.0.0)
3436
actionpack (>= 4.0, < 5.1)
3537
activerecord (>= 4.0, < 5.1)
@@ -367,6 +369,7 @@ PLATFORMS
367369
ruby
368370

369371
DEPENDENCIES
372+
activerecord-import (~> 0.28.1)
370373
activerecord-session_store (~> 1.0.0)
371374
andand
372375
bootstrap (~> 4.0.0.beta2.1)

app/models/unlock.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,26 @@ def self.refresh_unlocks_impl(course, user, user_unlocks_by_exercise_name)
6565
end
6666
end
6767
end
68+
69+
def self.fast_refresh_unlocks_impl(course, user, user_unlocks_by_exercise_name)
70+
# We can share the calculation between the exercises that have the same condition
71+
new_unlocks = []
72+
course.exercises.enabled.group_by { |o| o.unlock_spec }.each do |_spec, exercises|
73+
exercise = exercises.first
74+
existing = user_unlocks_by_exercise_name[exercise.name]
75+
exists = !!existing
76+
may_exist = exercise.requires_unlock? && exercise.unlock_spec_obj.permits_unlock_for?(user)
77+
next unless !exists && may_exist && !exercise.requires_explicit_unlock?
78+
79+
exercises.select { |e| !user_unlocks_by_exercise_name[e.name] }.each do |exercise|
80+
new_unlocks << Unlock.new(
81+
user: user,
82+
course: course,
83+
exercise: exercise,
84+
valid_after: exercise.unlock_spec_obj.valid_after
85+
)
86+
end
87+
end
88+
Unlock.import new_unlocks
89+
end
6890
end

script/background_daemon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Daemons.run_proc('background_daemon', dir_mode: :normal, dir: pid_dir, log_dir:
2121
require root_dir + '/config/environment'
2222
Rails.logger = Logger.new(Rails.root.join('log', 'background_daemon.log'))
2323
Rails.logger.auto_flushing = true if Rails.logger.respond_to? :auto_flushing=
24-
ActiveRecord::Base.connection_config[:pool] = 20
24+
ActiveRecord::Base.connection_config[:pool] = 25
2525

2626
# Tasks may be longrunning so we want each one in a thread so one task won't
2727
# block other ones.

0 commit comments

Comments
 (0)