Skip to content

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.rb
index 14255c53310..0f671c95a55 100644
--- a/app/jobs/base.rb
+++ b/app/jobs/base.rb
@@ -197,26 +197,29 @@ module Jobs
@db_duration || 0
end
- 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
+
+ retval
end
+ 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 Jobs
DB.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 Jobs
begin
until queue.empty?
queued_klass, queued_opts = queue.pop(true)
- queued_klass.new.perform(queued_opts)
+ queued_klass.new.perform_immediately(queued_opts)
end
ensure
Thread.current[:discourse_nested_job_queue] = nil
diff --git a/spec/jobs/create_linked_topic_spec.rb b/spec/jobs/create_linked_topic_spec.rb
index 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 do
it "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_error
end
context "with a post" do
diff --git a/spec/jobs/jobs_base_spec.rb b/spec/jobs/jobs_base_spec.rb
index 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 do
it "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")
end
it "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")
end
context "with fake jobs" do
diff --git a/spec/jobs/jobs_spec.rb b/spec/jobs/jobs_spec.rb
index c20f23a89dd..c26af358cfe 100644
--- a/spec/jobs/jobs_spec.rb
+++ b/spec/jobs/jobs_spec.rb
@@ -101,8 +101,9 @@ RSpec.describe Jobs do
it "executes the job right away" do
Jobs::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)
end
diff --git a/spec/jobs/process_post_spec.rb b/spec/jobs/process_post_spec.rb
index 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 do
it "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_error
end
context "with a post" do