REFACTOR: Move `queue_jobs` out of `SiteSetting`

approved

#1

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
diff --git a/app/jobs/base.rb b/app/jobs/base.rb
index 746f325..b57abae 100644
--- a/app/jobs/base.rb
+++ b/app/jobs/base.rb
@@ -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]
diff --git a/config/environments/test.rb b/config/environments/test.rb
index df30b74..9186e92 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -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
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 9155d12..123da3e 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1395,9 +1395,6 @@ developer:
   port:
     hidden: true
     default: ""
-  queue_jobs:
-    hidden: true
-    default: true
   enable_long_polling:
     client: true
     default: true
diff --git a/lib/generators/plugin/templates/controller_spec.rb.erb b/lib/generators/plugin/templates/controller_spec.rb.erb
index c73e0a8..15472e1 100644
--- a/lib/generators/plugin/templates/controller_spec.rb.erb
+++ b/lib/generators/plugin/templates/controller_spec.rb.erb
@@ -2,7 +2,7 @@ require 'rails_helper'
 
 describe <%= name %>::ActionsController do
   before do
-    run_jobs_synchronously!
+    Jobs.run_immediately!
   end
 
   it 'can list' do
diff --git a/plugins/discourse-local-dates/spec/models/post_spec.rb b/plugins/discourse-local-dates/spec/models/post_spec.rb
index 9591607..b472e98 100644
--- a/plugins/discourse-local-dates/spec/models/post_spec.rb
+++ b/plugins/discourse-local-dates/spec/models/post_spec.rb
@@ -3,7 +3,7 @@ require 'rails_helper'
 describe Post do
 
   before do
-    run_jobs_synchronously!
+    Jobs.run_immediately!
   end
 
   describe '#local_dates' do
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
index 91b0c15..4b44326 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
@@ -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
 
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb
index 7ecc727..7377d37 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb
@@ -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
 
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
index 4d76b06..b023b93 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
@@ -37,7 +37,7 @@ describe DiscourseNarrativeBot::TrackSelector do
   end
 
   before do
-    run_jobs_synchronously!
+    Jobs.run_immediately!
   end
 
   describe '#select' do
diff --git a/plugins/discourse-narrative-bot/spec/user_spec.rb b/plugins/discourse-narrative-bot/spec/user_spec.rb
index de52f0e..07eaee4 100644
--- a/plugins/discourse-narrative-bot/spec/user_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/user_spec.rb
@@ -13,7 +13,7 @@ describe User do
   end
 
   before do
-    run_jobs_synchronously!
+    Jobs.run_immediately!
     SiteSetting.discourse_narrative_bot_enabled = true
   end
 
diff --git a/script/profile_db_generator.rb b/script/profile_db_generator.rb
index 37f7571..f75ba49 100644
--- a/script/profile_db_generator.rb
+++ b/script/profile_db_generator.rb
@@ -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"
diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb
index 0e609c9..c82aae8 100644
--- a/spec/components/post_creator_spec.rb
+++ b/spec/components/post_creator_spec.rb
@@ -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)
diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb
index d5da412..6971688 100644
--- a/spec/components/post_destroyer_spec.rb
+++ b/spec/components/post_destroyer_spec.rb
@@ -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 {
diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb
index d721692..1fc26a6 100644
--- a/spec/components/post_revisor_spec.rb
+++ b/spec/components/post_revisor_spec.rb
@@ -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
diff --git a/spec/integration/watched_words_spec.rb b/spec/integration/watched_words_spec.rb
index 7620237..646084d 100644
--- a/spec/integration/watched_words_spec.rb
+++ b/spec/integration/watched_words_spec.rb
@@ -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)
diff --git a/spec/jobs/jobs_spec.rb b/spec/jobs/jobs_spec.rb

[... diff too long, it was truncated ...]

GitHub sha: fa5a1586


DEV: Run pull_hotlinked_images onebox specs without synchronous jobs
Approved #2

Followed Up #3

DEV: Run pull_hotlinked_images onebox specs without synchronous jobs