A platform for community discussion. Free, open, simple.
DEV: Fix threading error when running jobs immediately in system tests (#19811)
Alan Guo Xiang Tan8a7b62b12611
diff --git a/app/jobs/base.rb b/app/jobs/base.rbindex 14255c53310..0f671c95a55 100644--- a/app/jobs/base.rb+++ b/app/jobs/base.rb@@ -197,26 +197,29 @@ module Jobs@db_duration || 0end- def perform(*args)+ def perform_immediately(*args)opts = args.extract_options!.with_indifferent_access- Sidekiq.redis { |r| r.set("last_job_perform_at", Time.now.to_i) } if ::Jobs.run_later?-- if opts.delete(:sync_exec)- if opts.has_key?(:current_site_id) &&- opts[:current_site_id] != RailsMultisite::ConnectionManagement.current_db- raise ArgumentError.new(- "You can't connect to another database when executing a job synchronously.",- )- else- begin- retval = execute(opts)- rescue => exc- Discourse.handle_job_exception(exc, error_context(opts))- end- return retval+ if opts.has_key?(:current_site_id) &&+ opts[:current_site_id] != RailsMultisite::ConnectionManagement.current_db+ raise ArgumentError.new(+ "You can't connect to another database when executing a job synchronously.",+ )+ else+ begin+ retval = execute(opts)+ rescue => exc+ Discourse.handle_job_exception(exc, error_context(opts))end++ retvalend+ end++ def perform(*args)+ opts = args.extract_options!.with_indifferent_access++ Sidekiq.redis { |r| r.set("last_job_perform_at", Time.now.to_i) } if ::Jobs.run_later?dbs =if opts[:current_site_id]@@ -334,9 +337,6 @@ module JobsDB.after_commit { klass.client_push(hash) }else- # Otherwise execute the job right away- opts["sync_exec"] = true-if Rails.env == "development"Scheduler::Defer.later("job") { klass.new.perform(opts) }else@@ -358,7 +358,7 @@ module Jobsbeginuntil queue.empty?queued_klass, queued_opts = queue.pop(true)- queued_klass.new.perform(queued_opts)+ queued_klass.new.perform_immediately(queued_opts)endensureThread.current[:discourse_nested_job_queue] = nildiff --git a/spec/jobs/create_linked_topic_spec.rb b/spec/jobs/create_linked_topic_spec.rbindex 973492bbcd9..d2c6244de50 100644--- a/spec/jobs/create_linked_topic_spec.rb+++ b/spec/jobs/create_linked_topic_spec.rb@@ -2,7 +2,7 @@RSpec.describe Jobs::CreateLinkedTopic doit "returns when the post cannot be found" do- expect { Jobs::CreateLinkedTopic.new.perform(post_id: 1, sync_exec: true) }.not_to raise_error+ expect { Jobs::CreateLinkedTopic.new.execute(post_id: 1) }.not_to raise_errorendcontext "with a post" dodiff --git a/spec/jobs/jobs_base_spec.rb b/spec/jobs/jobs_base_spec.rbindex 19899879bfa..a76fed09e3b 100644--- a/spec/jobs/jobs_base_spec.rb+++ b/spec/jobs/jobs_base_spec.rb@@ -59,12 +59,12 @@ RSpec.describe ::Jobs::Base doit "delegates the process call to execute" do::Jobs::Base.any_instance.expects(:execute).with({ "hello" => "world" })- ::Jobs::Base.new.perform("hello" => "world", "sync_exec" => true)+ ::Jobs::Base.new.perform("hello" => "world")endit "converts to an indifferent access hash" do::Jobs::Base.any_instance.expects(:execute).with(instance_of(HashWithIndifferentAccess))- ::Jobs::Base.new.perform("hello" => "world", "sync_exec" => true)+ ::Jobs::Base.new.perform("hello" => "world")endcontext "with fake jobs" dodiff --git a/spec/jobs/jobs_spec.rb b/spec/jobs/jobs_spec.rbindex c20f23a89dd..c26af358cfe 100644--- a/spec/jobs/jobs_spec.rb+++ b/spec/jobs/jobs_spec.rb@@ -101,8 +101,9 @@ RSpec.describe Jobs doit "executes the job right away" doJobs::ProcessPost.any_instance- .expects(:perform)- .with({ "post_id" => 1, "sync_exec" => true, "current_site_id" => "default" })+ .expects(:perform_immediately)+ .with({ "post_id" => 1, "current_site_id" => "default" })+Jobs.enqueue(:process_post, post_id: 1)enddiff --git a/spec/jobs/process_post_spec.rb b/spec/jobs/process_post_spec.rbindex d7f35b3ff14..20c1b1c46fd 100644--- a/spec/jobs/process_post_spec.rb+++ b/spec/jobs/process_post_spec.rb@@ -2,7 +2,7 @@RSpec.describe Jobs::ProcessPost doit "returns when the post cannot be found" do- expect { Jobs::ProcessPost.new.perform(post_id: 1, sync_exec: true) }.not_to raise_error+ expect { Jobs::ProcessPost.new.execute(post_id: 1) }.not_to raise_errorendcontext "with a post" do