Skip to content

Commit

Permalink
Migrate more explicit thread usage to jobs #746
Browse files Browse the repository at this point in the history
  • Loading branch information
thesecretmaster committed Sep 9, 2021
1 parent d2c4502 commit 46a58bd
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 39 deletions.
4 changes: 1 addition & 3 deletions app/channels/topbar_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
class TopbarChannel < ApplicationCable::Channel
def subscribed
stream_from 'topbar'
Thread.new do
ActionCable.server.broadcast 'topbar', commit: CurrentCommit
end
ActionCable.server.broadcast 'topbar', commit: CurrentCommit
end

def unsubscribed
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/flag_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def update
# we want to re-validate all existing FlagConditions
# and disable them if they aren't in compliance with the
# new settings
Thread.new do
FlagCondition.revalidate_all
end
RevalidateFlagConditionsJob.perform_later
end

format.html { redirect_to flag_settings_path, notice: 'Flag setting was successfully updated.' }
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/review_queues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ def reviews
end

def recheck_items
Thread.new do
@queue.items.includes(:reviewable).each do |i|
i.update(completed: true) if i.reviewable.should_dq?(@queue)
end
end
RecheckReviewQueueJob.perform_later(@queue)
flash[:info] = 'Checking started in background.'
redirect_back fallback_location: review_queues_path
end
Expand Down
10 changes: 4 additions & 6 deletions app/controllers/status_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ def status_update

@smoke_detector.save!

Thread.new do
ActionCable.server.broadcast 'status', status_channel_data
ActionCable.server.broadcast 'status_blacklist_manager', status_channel_data.merge(failover_link: failover_link)
ActionCable.server.broadcast 'topbar', last_ping: @smoke_detector.last_ping.to_f
ActionCable.server.broadcast 'smokey_pings', smokey: @smoke_detector.as_json
end
ActionCable.server.broadcast 'status', status_channel_data
ActionCable.server.broadcast 'status_blacklist_manager', status_channel_data.merge(failover_link: failover_link)
ActionCable.server.broadcast 'topbar', last_ping: @smoke_detector.last_ping.to_f
ActionCable.server.broadcast 'smokey_pings', smokey: @smoke_detector.as_json

respond_to do |format|
format.json do
Expand Down
9 changes: 9 additions & 0 deletions app/jobs/recheck_review_queue_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RecheckReviewQueueJob < ApplicationJob
queue_as :default

def perform(queue)
queue.items.includes(:reviewable).each do |i|
i.update(completed: true) if i.reviewable.should_dq?(queue)
end
end
end
16 changes: 16 additions & 0 deletions app/jobs/revalidate_flag_conditions_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class RevalidateFlagConditionsJob < ApplicationJob
queue_as :default

def perform(*args)
FlagCondition.where(flags_enabled: true).find_each do |fc|
unless fc.validate
failures = fc.errors.full_messages
fc.flags_enabled = false
fc.save(validate: false)
ActionCable.server.broadcast 'smokedetector_messages',
message: "@#{fc.user&.username&.tr(' ', '')} " \
"Your flag condition was disabled: #{failures.join(',')}"
end
end
end
end
10 changes: 10 additions & 0 deletions app/jobs/validate_eligable_flaggers_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ValidateEligableFlaggersJob < ApplicationJob
queue_as :default

def perform(post)
sys = User.find(-1)
post.eligible_flaggers.each do |u|
FlagCondition.validate_for_user(u, sys)
end
end
end
13 changes: 4 additions & 9 deletions app/models/feedback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,10 @@ def self.populate_redis_meta
message = "fp feedback on autoflagged post: [#{post.title}](#{post.link}) \\[[MS](//metasmoke.erwaysoftware.com/post/#{post_id})]"
ActionCable.server.broadcast 'smokedetector_messages', autoflag_fp: { message: message, site: post.site.site_domain }

Thread.new do
names = post.flaggers.map { |u| '@' + u.username.tr(' ', '') }
user_msg = "Autoflagged FP: flagged by #{names.join(', ')}"
ActionCable.server.broadcast 'smokedetector_messages', autoflag_fp: { message: user_msg, site: post.site.site_domain }
sys = User.find(-1)
post.eligible_flaggers.each do |u|
FlagCondition.validate_for_user(u, sys)
end
end
names = post.flaggers.map { |u| '@' + u.username.tr(' ', '') }
user_msg = "Autoflagged FP: flagged by #{names.join(', ')}"
ActionCable.server.broadcast 'smokedetector_messages', autoflag_fp: { message: user_msg, site: post.site.site_domain }
ValidateEligableFlaggersJob.perform_later(post)
end

post.stack_exchange_user&.unblacklist_user if post.is_fp
Expand Down
13 changes: 0 additions & 13 deletions app/models/flag_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,6 @@ def accuracy
true_positive_count.to_f * 100 / post_feedback_results.count.to_f
end

def self.revalidate_all
FlagCondition.where(flags_enabled: true).find_each do |fc|
unless fc.validate
failures = fc.errors.full_messages
fc.flags_enabled = false
fc.save(validate: false)
ActionCable.server.broadcast 'smokedetector_messages',
message: "@#{fc.user&.username&.tr(' ', '')} " \
"Your flag condition was disabled: #{failures.join(',')}"
end
end
end

def self.overall_accuracy(user)
query = File.read(Rails.root.join('lib/queries/overall_accuracy.sql'))
sanitized = ActiveRecord::Base.sanitize_sql([query, user_id: user.id])
Expand Down

0 comments on commit 46a58bd

Please sign in to comment.