REFACTOR: Move queue_jobs out of SiteSetting

It is not a setting, and only relevant in specs. The new API is:

```
Jobs.run_later!        # jobs will be thrown on the queue
Jobs.run_immediately!  # jobs will run right away, avoid the queue
```
This commit is contained in:
Robin Ward 2019-03-14 10:47:38 -04:00
parent f3c76ad482
commit fa5a158683
40 changed files with 75 additions and 72 deletions

View File

@ -4,6 +4,22 @@ module Jobs
Sidekiq::Stats.new.enqueued
end
def self.run_later?
!@run_immediately
end
def self.run_immediately?
!!@run_immediately
end
def self.run_immediately!
@run_immediately = true
end
def self.run_later!
@run_immediately = false
end
def self.last_job_performed_at
Sidekiq.redis do |r|
int = r.get('last_job_perform_at')
@ -169,7 +185,7 @@ module Jobs
def perform(*args)
opts = args.extract_options!.with_indifferent_access
if SiteSetting.queue_jobs?
if Jobs.run_later?
Sidekiq.redis do |r|
r.set('last_job_perform_at', Time.now.to_i)
end
@ -272,7 +288,7 @@ module Jobs
end
# If we are able to queue a job, do it
if SiteSetting.queue_jobs?
if Jobs.run_later?
hash = {
'class' => klass,
'args' => [opts]

View File

@ -63,14 +63,12 @@ Discourse::Application.configure do
s.set_regardless_of_locale(:crawl_images, false)
s.set_regardless_of_locale(:download_remote_images_to_local, false)
s.set_regardless_of_locale(:unique_posts_mins, 0)
# Running jobs are expensive and most of our tests are not concern with
# code that runs inside jobs
s.set_regardless_of_locale(:queue_jobs, true)
# disable plugins
if ENV['LOAD_PLUGINS'] == '1'
s.set_regardless_of_locale(:discourse_narrative_bot_enabled, false)
end
end
SiteSetting.refresh!
end
end

View File

@ -1395,9 +1395,6 @@ developer:
port:
hidden: true
default: ""
queue_jobs:
hidden: true
default: true
enable_long_polling:
client: true
default: true

View File

@ -2,7 +2,7 @@ require 'rails_helper'
describe <%= name %>::ActionsController do
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it 'can list' do

View File

@ -3,7 +3,7 @@ require 'rails_helper'
describe Post do
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
describe '#local_dates' do

View File

@ -22,7 +22,7 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do
let(:reset_trigger) { DiscourseNarrativeBot::TrackSelector.reset_trigger }
before do
run_jobs_synchronously!
Jobs.run_immediately!
SiteSetting.discourse_narrative_bot_enabled = true
end

View File

@ -25,7 +25,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
let(:reset_trigger) { DiscourseNarrativeBot::TrackSelector.reset_trigger }
before do
run_jobs_synchronously!
Jobs.run_immediately!
SiteSetting.discourse_narrative_bot_enabled = true
end

View File

@ -37,7 +37,7 @@ describe DiscourseNarrativeBot::TrackSelector do
end
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
describe '#select' do

View File

@ -13,7 +13,7 @@ describe User do
end
before do
run_jobs_synchronously!
Jobs.run_immediately!
SiteSetting.discourse_narrative_bot_enabled = true
end

View File

@ -55,7 +55,7 @@ end
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
SiteSetting.queue_jobs = false
Jobs.run_immediately!
unless Rails.env == "profile"
puts "This script should only be used in the profile environment"

View File

@ -902,7 +902,7 @@ describe PostCreator do
end
it 'can post to a group correctly' do
run_jobs_synchronously!
Jobs.run_immediately!
expect(post.topic.archetype).to eq(Archetype.private_message)
expect(post.topic.topic_allowed_users.count).to eq(1)

View File

@ -617,7 +617,7 @@ describe PostDestroyer do
context '@mentions' do
it 'removes notifications when deleted' do
run_jobs_synchronously!
Jobs.run_immediately!
user = Fabricate(:evil_trout)
post = create_post(raw: 'Hello @eviltrout')
expect {

View File

@ -608,7 +608,7 @@ describe PostRevisor do
let(:mentioned_user) { Fabricate(:user) }
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it "generates a notification for a mention" do

View File

@ -163,7 +163,7 @@ describe WatchedWord do
end
it "flags on revisions" do
run_jobs_synchronously!
Jobs.run_immediately!
post = Fabricate(:post, topic: Fabricate(:topic, user: tl2_user), user: tl2_user)
expect {
PostRevisor.new(post).revise!(post.user, { raw: "Want some #{flag_word.word} for cheap?" }, revised_at: post.updated_at + 10.seconds)

View File

@ -5,9 +5,9 @@ describe Jobs do
describe 'enqueue' do
describe 'when queue_jobs is true' do
describe 'run_later!' do
before do
SiteSetting.expects(:queue_jobs?).at_least_once.returns(true)
Jobs.run_later!
end
it 'enqueues a job in sidekiq' do
@ -74,9 +74,9 @@ describe Jobs do
end
end
describe 'when queue_jobs is false' do
describe 'run_immediately!' do
before do
SiteSetting.expects(:queue_jobs?).at_least_once.returns(false)
Jobs.run_immediately!
end
it "doesn't enqueue in sidekiq" do

View File

@ -32,7 +32,7 @@ describe Jobs::PullHotlinkedImages do
describe '#execute' do
before do
run_jobs_synchronously!
Jobs.run_immediately!
FastImage.expects(:size).returns([100, 100]).at_least_once
end
@ -72,7 +72,6 @@ describe Jobs::PullHotlinkedImages do
let(:api_url) { "https://en.wikipedia.org/w/api.php?action=query&titles=#{media}&prop=imageinfo&iilimit=50&iiprop=timestamp|user|url&iiurlwidth=500&format=json" }
before do
SiteSetting.queue_jobs = true
stub_request(:head, url)
stub_request(:get, url).to_return(body: '')
stub_request(:get, api_url).to_return(body: "{

View File

@ -31,7 +31,7 @@ RSpec.describe UploadRecovery do
before do
SiteSetting.authorized_extensions = 'png|pdf'
run_jobs_synchronously!
Jobs.run_immediately!
end
after do

View File

@ -68,7 +68,7 @@ describe CategoryUser do
context 'integration' do
before do
run_jobs_synchronously!
Jobs.run_immediately!
NotificationEmailer.enable
end

View File

@ -8,7 +8,7 @@ describe DiscourseSingleSignOn do
SiteSetting.sso_url = @sso_url
SiteSetting.enable_sso = true
SiteSetting.sso_secret = @sso_secret
run_jobs_synchronously!
Jobs.run_immediately!
end
def make_sso

View File

@ -1072,7 +1072,7 @@ describe PostAction do
end
it "should create a notification in the related topic" do
run_jobs_synchronously!
Jobs.run_immediately!
post = Fabricate(:post)
user = Fabricate(:user)
action = PostAction.act(user, post, PostActionType.types[:spam], message: "WAT")
@ -1089,7 +1089,7 @@ describe PostAction do
end
it "should not add a moderator post when post is flagged via private message" do
run_jobs_synchronously!
Jobs.run_immediately!
post = Fabricate(:post)
user = Fabricate(:user)
action = PostAction.act(user, post, PostActionType.types[:notify_user], message: "WAT")

View File

@ -41,7 +41,7 @@ describe PostMover do
before do
SiteSetting.tagging_enabled = true
run_jobs_synchronously!
Jobs.run_immediately!
p1.replies << p3
p2.replies << p4
UserActionCreator.enable
@ -570,7 +570,7 @@ describe PostMover do
before do
SiteSetting.tagging_enabled = true
run_jobs_synchronously!
Jobs.run_immediately!
p1.replies << p3
p2.replies << p4
UserActionCreator.enable

View File

@ -995,7 +995,7 @@ describe Post do
end
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
describe 'when user can not mention a group' do

View File

@ -2,7 +2,7 @@ require 'rails_helper'
describe QuotedPost do
it 'correctly extracts quotes' do
run_jobs_synchronously!
Jobs.run_immediately!
topic = Fabricate(:topic)
post1 = create_post(topic: topic, post_number: 1, raw: "foo bar")
@ -34,7 +34,7 @@ describe QuotedPost do
end
it "doesn't count quotes from the same post" do
run_jobs_synchronously!
Jobs.run_immediately!
topic = Fabricate(:topic)
post = create_post(topic: topic, post_number: 1, raw: "foo bar")

View File

@ -75,7 +75,7 @@ describe TagUser do
context "with some tag notification settings" do
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
let :watched_post do

View File

@ -1280,7 +1280,7 @@ describe Topic do
describe 'user that is watching the new category' do
it 'should generate the notification for the topic' do
run_jobs_synchronously!
Jobs.run_immediately!
topic.posts << Fabricate(:post)
@ -1602,7 +1602,7 @@ describe Topic do
let(:topic) { Fabricate(:topic, category: category) }
it "should be able to override category's default auto close" do
run_jobs_synchronously!
Jobs.run_immediately!
expect(topic.topic_timers.first.duration).to eq(4)

View File

@ -190,7 +190,7 @@ RSpec.describe TopicTimer, type: :model do
end
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it 'should close the topic' do
@ -219,7 +219,7 @@ RSpec.describe TopicTimer, type: :model do
end
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it 'should open the topic' do

View File

@ -451,7 +451,7 @@ describe TopicUser do
it "will receive email notification for every topic" do
user1 = Fabricate(:user)
run_jobs_synchronously!
Jobs.run_immediately!
SiteSetting.default_email_mailing_list_mode = true
SiteSetting.default_email_mailing_list_mode_frequency = 1

View File

@ -1088,7 +1088,7 @@ describe User do
context "with a reply" do
before do
run_jobs_synchronously!
Jobs.run_immediately!
PostCreator.new(Fabricate(:user),
raw: 'whatever this is a raw post',
topic_id: topic.id,

View File

@ -176,6 +176,11 @@ RSpec.configure do |config|
$test_cleanup_callbacks.reverse_each(&:call)
$test_cleanup_callbacks = nil
end
# Running jobs are expensive and most of our tests are not concern with
# code that runs inside jobs. run_later! means they are put on the redis
# queue and never processed.
Jobs.run_later!
end
config.before(:each, type: :multisite) do

View File

@ -37,7 +37,7 @@ RSpec.describe Admin::FlagsController do
context '#agree' do
it 'should raise a reasonable error if a flag was deferred and then someone else agreed' do
run_jobs_synchronously!
Jobs.run_immediately!
_post_action = PostAction.act(user, post_1, PostActionType.types[:spam], message: 'bad')
@ -52,7 +52,7 @@ RSpec.describe Admin::FlagsController do
end
it 'should be able to agree and keep content' do
run_jobs_synchronously!
Jobs.run_immediately!
post_action = PostAction.act(user, post_1, PostActionType.types[:spam], message: 'bad')
@ -69,7 +69,7 @@ RSpec.describe Admin::FlagsController do
it 'should be able to hide spam' do
SiteSetting.allow_user_locale = true
run_jobs_synchronously!
Jobs.run_immediately!
post_action = PostAction.act(user, post_1, PostActionType.types[:spam], message: 'bad')
admin.update!(locale: 'ja')
@ -90,7 +90,7 @@ RSpec.describe Admin::FlagsController do
end
it 'should not delete category topic' do
run_jobs_synchronously!
Jobs.run_immediately!
category.update_column(:topic_id, first_post.topic_id)
PostAction.act(user, first_post, PostActionType.types[:spam], message: 'bad')

View File

@ -78,7 +78,7 @@ RSpec.describe Admin::GroupsController do
let(:user2) { Fabricate(:user, trust_level: 4) }
it "can assign users to a group by email or username" do
run_jobs_synchronously!
Jobs.run_immediately!
put "/admin/groups/bulk.json", params: {
group_id: group.id, users: [user.username.upcase, user2.email, 'doesnt_exist']

View File

@ -78,7 +78,7 @@ describe CategoriesController do
describe "logged in" do
before do
run_jobs_synchronously!
Jobs.run_immediately!
sign_in(admin)
end
@ -226,7 +226,7 @@ describe CategoriesController do
context '#update' do
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it "requires the user to be logged in" do

View File

@ -74,7 +74,7 @@ describe EmbedController do
let(:headers) { { 'REFERER' => embed_url } }
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it "raises an error with no referer" do

View File

@ -298,10 +298,6 @@ describe InvitesController do
end
context '.post_process_invite' do
before do
SiteSetting.queue_jobs = true
end
it 'sends a welcome message if set' do
user.send_welcome_message = true
put "/invites/show/#{invite.invite_key}.json"
@ -466,7 +462,6 @@ describe InvitesController do
end
it "resends the invite" do
SiteSetting.queue_jobs = true
post "/invites/reinvite.json", params: { email: invite.email }
expect(response.status).to eq(200)
expect(Jobs::InviteEmail.jobs.size).to eq(1)
@ -496,7 +491,6 @@ describe InvitesController do
end
it "allows admin to bulk invite" do
SiteSetting.queue_jobs = true
sign_in(Fabricate(:admin))
post "/invites/upload_csv.json", params: { file: file, name: filename }
expect(response.status).to eq(200)

View File

@ -730,7 +730,7 @@ describe PostsController do
end
it 'allows to create posts in import_mode' do
run_jobs_synchronously!
Jobs.run_immediately!
NotificationEmailer.enable
post_1 = Fabricate(:post)
user = Fabricate(:user)

View File

@ -4,7 +4,7 @@ RSpec.describe GroupMentionsUpdater do
let(:post) { Fabricate(:post) }
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
describe '.update' do

View File

@ -213,7 +213,7 @@ describe PostAlerter do
let(:linking_post) { create_post(raw: "my magic topic\n##{Discourse.base_url}#{post1.url}") }
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it "will notify correctly on linking" do
@ -289,7 +289,7 @@ describe PostAlerter do
let(:topic) { mention_post.topic }
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it 'notifies a user' do
@ -591,7 +591,7 @@ describe PostAlerter do
end
it "correctly pushes notifications if configured correctly" do
run_jobs_synchronously!
Jobs.run_immediately!
SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push"
2.times do |i|
@ -935,7 +935,7 @@ describe PostAlerter do
before do
SiteSetting.tagging_enabled = true
SiteSetting.queue_jobs = false
Jobs.run_immediately!
end
it "triggers a notification" do

View File

@ -136,7 +136,7 @@ describe UserAnonymizer do
end
it "updates the avatar in posts" do
run_jobs_synchronously!
Jobs.run_immediately!
upload = Fabricate(:upload, user: user)
user.user_avatar = UserAvatar.new(user_id: user.id, custom_upload_id: upload.id)
user.uploaded_avatar_id = upload.id # chosen in user preferences
@ -214,7 +214,7 @@ describe UserAnonymizer do
context "executes job" do
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
it "removes invites" do
@ -302,7 +302,7 @@ describe UserAnonymizer do
end
it "exhaustively replaces all user ips" do
run_jobs_synchronously!
Jobs.run_immediately!
link = IncomingLink.create!(current_user_id: user.id, ip_address: old_ip, post_id: post.id)
screened_email = ScreenedEmail.create!(email: user.email, ip_address: old_ip)

View File

@ -2,7 +2,7 @@ require 'rails_helper'
describe UsernameChanger do
before do
run_jobs_synchronously!
Jobs.run_immediately!
end
describe '#change' do

View File

@ -5,12 +5,6 @@ module Helpers
@next_seq = (@next_seq || 0) + 1
end
# If you don't `queue_jobs` it means you want to run them synchronously. This method
# makes that more clear in tests. It is automatically reset after every test.
def run_jobs_synchronously!
SiteSetting.queue_jobs = false
end
def log_in(fabricator = nil)
user = Fabricate(fabricator || :user)
log_in_user(user)