Skip to content

Commit 6af39b8

Browse files
committed
Migrate whenever schedule to SolidQueue recurring tasks
1 parent 31ce462 commit 6af39b8

File tree

5 files changed

+24
-59
lines changed

5 files changed

+24
-59
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ gem 'terser', require: false
5555
gem 'tubesock', github: 'openhpi/tubesock'
5656
gem 'turbolinks'
5757
gem 'webauthn'
58-
gem 'whenever', require: false
5958
gem 'zxcvbn-ruby', require: 'zxcvbn'
6059

6160
# Error Tracing

Gemfile.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ GEM
131131
charlock_holmes (0.7.9)
132132
childprocess (5.1.0)
133133
logger (~> 1.5)
134-
chronic (0.10.2)
135134
concurrent-ruby (1.3.5)
136135
connection_pool (2.5.3)
137136
cose (1.3.1)
@@ -649,8 +648,6 @@ GEM
649648
base64
650649
websocket-extensions (>= 0.1.0)
651650
websocket-extensions (0.1.5)
652-
whenever (1.0.0)
653-
chronic (>= 0.6.3)
654651
will_paginate (4.0.1)
655652
xpath (3.2.0)
656653
nokogiri (~> 1.8)
@@ -753,7 +750,6 @@ DEPENDENCIES
753750
web-console
754751
webauthn
755752
webmock
756-
whenever
757753
zxcvbn-ruby
758754

759755
CHECKSUMS
@@ -791,7 +787,6 @@ CHECKSUMS
791787
cbor (0.5.9.8) sha256=9ee097fc58d9bc5e406d112cd2d4e112c7354ec16f8b6ff34e4732c1e44b4eb7
792788
charlock_holmes (0.7.9) sha256=b49e8a11ce1921e2c5b65511bb864ae51720ce9bd1c336ccf0e89e6c8ae62db0
793789
childprocess (5.1.0) sha256=9a8d484be2fd4096a0e90a0cd3e449a05bc3aa33f8ac9e4d6dcef6ac1455b6ec
794-
chronic (0.10.2) sha256=766f2fcce6ac3cc152249ed0f2b827770d3e517e2e87c5fba7ed74f4889d2dc3
795790
concurrent-ruby (1.3.5) sha256=813b3e37aca6df2a21a3b9f1d497f8cbab24a2b94cab325bffe65ee0f6cbebc6
796791
connection_pool (2.5.3) sha256=cfd74a82b9b094d1ce30c4f1a346da23ee19dc8a062a16a85f58eab1ced4305b
797792
cose (1.3.1) sha256=d5d4dbcd6b035d513edc4e1ab9bc10e9ce13b4011c96e3d1b8fe5e6413fd6de5
@@ -1008,7 +1003,6 @@ CHECKSUMS
10081003
websocket (1.2.11) sha256=b7e7a74e2410b5e85c25858b26b3322f29161e300935f70a0e0d3c35e0462737
10091004
websocket-driver (0.7.7) sha256=056d99f2cd545712cfb1291650fde7478e4f2661dc1db6a0fa3b966231a146b4
10101005
websocket-extensions (0.1.5) sha256=1c6ba63092cda343eb53fc657110c71c754c56484aad42578495227d717a8241
1011-
whenever (1.0.0) sha256=d9b71417c43960f5eb125b77207174cba4691091cedae0d282a29cfc72eaa246
10121006
will_paginate (4.0.1) sha256=107b226ebe1d393d274575956a7c472e1eefdd97d8828e01b72d425d15a875b9
10131007
xpath (3.2.0) sha256=6dfda79d91bb3b949b947ecc5919f042ef2f399b904013eb3ef6d20dd3a4082e
10141008
zeitwerk (2.7.2) sha256=842e067cb11eb923d747249badfb5fcdc9652d6f20a1f06453317920fdcd4673
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
# frozen_string_literal: true
22

3-
namespace :detect_exercise_anomalies do
4-
# uncomment for debug logging:
5-
# logger = Logger.new($stdout)
6-
# logger.level = Logger::DEBUG
7-
# Rails.logger = logger
3+
class DetectExerciseAnomaliesJob < ApplicationJob
4+
include TimeHelper
85

9-
# rubocop:disable Lint/ConstantDefinitionInBlock, Style/MutableConstant
106
# These factors determine if an exercise is an anomaly, given the average working time (avg):
117
# (avg * MIN_TIME_FACTOR) <= working_time <= (avg * MAX_TIME_FACTOR)
128
MIN_TIME_FACTOR = 0.1
@@ -18,20 +14,14 @@
1814
# Determines margin below which contributor working times will be considered data errors (e.g. copy/paste solutions)
1915
MIN_CONTRIBUTOR_WORKING_TIME = 0.0
2016

21-
# Cache exercise working times, because queries are expensive and values do not change between collections
22-
WORKING_TIME_CACHE = {}
23-
AVERAGE_WORKING_TIME_CACHE = {}
24-
# rubocop:enable Lint/ConstantDefinitionInBlock, Style/MutableConstant
25-
26-
task :with_at_least, %i[number_of_exercises number_of_contributors] => :environment do |_task, args|
27-
include TimeHelper
17+
def perform(number_of_exercises:, number_of_contributors:)
18+
# Cache exercise working times, because queries are expensive and values do not change between collections
19+
@working_time_cache = {}
20+
@average_working_time_cache = {}
2821

2922
# Set intervalstyle to iso_8601 to avoid problems with time parsing.
3023
ApplicationRecord.connection.exec_query("SET intervalstyle = 'iso_8601';")
3124

32-
number_of_exercises = args[:number_of_exercises]
33-
number_of_contributors = args[:number_of_contributors]
34-
3525
log "Searching for exercise collections with at least #{number_of_exercises} exercises and #{number_of_contributors} contributors."
3626
# Get all exercise collections that have at least the specified amount of exercises and at least the specified
3727
# number of contributors AND are flagged for anomaly detection
@@ -52,7 +42,7 @@
5242
end
5343

5444
def log(message = '', indent_level = 0, prefix = '')
55-
puts(("\t" * indent_level) + "#{prefix}#{message}")
45+
Rails.logger.debug { ("\t" * indent_level) + "#{prefix}#{message}" }
5646
end
5747

5848
def get_collections(number_of_exercises, number_of_solutions)
@@ -92,23 +82,23 @@ def find_anomalies(collection)
9282
end
9383

9484
def get_average_working_time(exercise)
95-
unless AVERAGE_WORKING_TIME_CACHE.key?(exercise.id)
85+
unless @average_working_time_cache.key?(exercise.id)
9686
seconds = time_to_f exercise.average_working_time
97-
AVERAGE_WORKING_TIME_CACHE[exercise.id] = seconds
87+
@average_working_time_cache[exercise.id] = seconds
9888
end
99-
AVERAGE_WORKING_TIME_CACHE[exercise.id]
89+
@average_working_time_cache[exercise.id]
10090
end
10191

10292
def get_contributor_working_times(exercise)
103-
unless WORKING_TIME_CACHE.key?(exercise.id)
93+
unless @working_time_cache.key?(exercise.id)
10494
exercise.retrieve_working_time_statistics
105-
WORKING_TIME_CACHE[exercise.id] = exercise.working_time_statistics.flat_map do |contributor_type, contributor_id_with_result|
95+
@working_time_cache[exercise.id] = exercise.working_time_statistics.flat_map do |contributor_type, contributor_id_with_result|
10696
contributor_id_with_result.flat_map do |contributor_id, result|
10797
{[contributor_type, contributor_id] => result}
10898
end
10999
end.inject(:merge)
110100
end
111-
WORKING_TIME_CACHE[exercise.id]
101+
@working_time_cache[exercise.id]
112102
end
113103

114104
def notify_collection_author(collection, anomalies)

config/recurring.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,14 @@
77
# command: "SoftDeletedRecord.due.delete_all"
88
# priority: 2
99
# schedule: at 5am every day
10+
11+
detect_exercise_anomalies_job:
12+
class: DetectExerciseAnomaliesJob
13+
args:
14+
- number_of_exercises: 10
15+
number_of_contributors: 50
16+
schedule: at 3am every day
17+
18+
clear_finished_jobs:
19+
command: "SolidQueue::Job.clear_finished_in_batches"
20+
schedule: at 3am every day

config/schedule.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)