Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#51316] Use RequestStore instead of Thread.current.

Pavel Balashou6d9886d583f3
diff --git a/modules/storages/app/workers/storages/manage_nextcloud_integration_events_job.rb b/modules/storages/app/workers/storages/manage_nextcloud_integration_events_job.rb
index 16feb6b2502..1b0e813d660 100644
--- a/modules/storages/app/workers/storages/manage_nextcloud_integration_events_job.rb
+++ b/modules/storages/app/workers/storages/manage_nextcloud_integration_events_job.rb
@@ -30,22 +30,23 @@ module Storages
class ManageNextcloudIntegrationEventsJob < ApplicationJob
include ManageNextcloudIntegrationJobMixin
- DEBOUNCE_TIME = 5.seconds.freeze
+ THREAD_DEBOUNCE_TIME = 4.seconds.freeze
+ JOB_DEBOUNCE_TIME = 5.seconds.freeze
THREAD_KEY = :manage_nextcloud_integration_events_job_debounce_happend_at
queue_with_priority :above_normal
def self.debounce
- last_debounce_happend_at = Thread.current[THREAD_KEY]
- if last_debounce_happend_at.blank? || Time.current > (last_debounce_happend_at + DEBOUNCE_TIME)
+ last_debounce_happend_at = RequestStore.store[THREAD_KEY]
+ if last_debounce_happend_at.blank? || Time.current > (last_debounce_happend_at + THREAD_DEBOUNCE_TIME)
count = Delayed::Job
.where("handler LIKE ?", "%job_class: #{self}%")
.where(locked_at: nil)
- .where('run_at <= ?', DEBOUNCE_TIME.from_now)
+ .where('run_at <= ?', JOB_DEBOUNCE_TIME.from_now)
.delete_all
Rails.logger.info("deleted: #{count} jobs")
- Thread.current[THREAD_KEY] = Time.current
- set(wait: DEBOUNCE_TIME).perform_later
+ RequestStore.store[THREAD_KEY] = Time.current
+ set(wait: JOB_DEBOUNCE_TIME).perform_later
end
end
diff --git a/modules/storages/spec/workers/storages/manage_nextcloud_integration_events_job_spec.rb b/modules/storages/spec/workers/storages/manage_nextcloud_integration_events_job_spec.rb
index c9b71ed150a..26bd4fc498d 100644
--- a/modules/storages/spec/workers/storages/manage_nextcloud_integration_events_job_spec.rb
+++ b/modules/storages/spec/workers/storages/manage_nextcloud_integration_events_job_spec.rb
@@ -39,7 +39,7 @@ RSpec.describe Storages::ManageNextcloudIntegrationEventsJob, type: :job do
end
describe '.debounce' do
- it 'debounces job with 5 seconds timeframe' do
+ it 'debounces a job within a certain timeframe' do
ActiveJob::Base.disable_test_adapter
other_handler = Storages::ManageNextcloudIntegrationCronJob.perform_later.provider_job_id
@@ -54,8 +54,13 @@ RSpec.describe Storages::ManageNextcloudIntegrationEventsJob, type: :job do
expect(Delayed::Job.count).to eq(6)
+ allow(described_class).to receive(:set).and_call_original
+
1000.times { described_class.debounce }
+ # check if debouncing within a thread really works.
+ expect(described_class).to have_received(:set).once
+
expect(Delayed::Job.count).to eq(4)
expect(Delayed::Job.pluck(:id)).to include(other_handler,
same_handler_out_of_timeframe,
@@ -68,7 +73,7 @@ RSpec.describe Storages::ManageNextcloudIntegrationEventsJob, type: :job do
.where("handler LIKE ?", "%job_class: #{described_class}%")
.last
.run_at
- ).to be_within(3.seconds).of(described_class::DEBOUNCE_TIME.from_now)
+ ).to be_within(3.seconds).of(described_class::JOB_DEBOUNCE_TIME.from_now)
end
end