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

Rewrite using Rails.cache.

Pavel Balashou416cab6badb6
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 1b0e813d660..607f190088a 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,23 +30,19 @@ module Storages
class ManageNextcloudIntegrationEventsJob < ApplicationJob
include ManageNextcloudIntegrationJobMixin
- THREAD_DEBOUNCE_TIME = 4.seconds.freeze
- JOB_DEBOUNCE_TIME = 5.seconds.freeze
- THREAD_KEY = :manage_nextcloud_integration_events_job_debounce_happend_at
+ SINGLE_THREAD_DEBOUNCE_TIME = 4.seconds.freeze
+ MULTI_THREAD_DEBOUNCE_TIME = 5.seconds.freeze
+ KEY = :manage_nextcloud_integration_events_job_debounce_happend_at
queue_with_priority :above_normal
def self.debounce
- 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 <= ?', JOB_DEBOUNCE_TIME.from_now)
- .delete_all
- Rails.logger.info("deleted: #{count} jobs")
- RequestStore.store[THREAD_KEY] = Time.current
- set(wait: JOB_DEBOUNCE_TIME).perform_later
+ last_debounce_happend_at = RequestStore.store[KEY]
+ if last_debounce_happend_at.blank? || Time.current > (last_debounce_happend_at + SINGLE_THREAD_DEBOUNCE_TIME)
+ Rails.cache.fetch(KEY, expires_in: MULTI_THREAD_DEBOUNCE_TIME) do
+ set(wait: MULTI_THREAD_DEBOUNCE_TIME).perform_later
+ RequestStore.store[KEY] = Time.current
+ end
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 26bd4fc498d..4994a693a75 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,41 +39,30 @@ RSpec.describe Storages::ManageNextcloudIntegrationEventsJob, type: :job do
end
describe '.debounce' do
- it 'debounces a job within a certain timeframe' do
- ActiveJob::Base.disable_test_adapter
-
- other_handler = Storages::ManageNextcloudIntegrationCronJob.perform_later.provider_job_id
- same_handler_within_timeframe1 = described_class.set(wait: 1.second).perform_later.provider_job_id
- same_handler_within_timeframe2 = described_class.set(wait: 2.seconds).perform_later.provider_job_id
- same_handler_within_timeframe3 = described_class.set(wait: 3.seconds).perform_later.provider_job_id
- same_handler_out_of_timeframe = described_class.set(wait: 1.minute).perform_later.provider_job_id
- same_handler_within_timeframe_in_progress = described_class.set(wait: 18.seconds).perform_later.tap do |job|
- # simulate in progress state
- Delayed::Job.where(id: job.provider_job_id).update_all(locked_at: Time.current, locked_by: "test_process #{Process.pid}")
- end.provider_job_id
-
- expect(Delayed::Job.count).to eq(6)
+ context 'when has been debounced by other thread' do
+ before do
+ Rails.cache.write(described_class::KEY, Time.current)
+ end
+
+ it 'does nothing' do
+ expect { described_class.debounce }.not_to change(enqueued_jobs, :count)
+ end
+ end
- allow(described_class).to receive(:set).and_call_original
+ context 'when has not been debounced by other thread' do
+ it 'schedules a job' do
+ expect { described_class.debounce }.to change(enqueued_jobs, :count).from(0).to(1)
+ end
- 1000.times { described_class.debounce }
+ it 'hits cache once when called 1000 times in a short period of time' do
+ allow(Rails.cache).to receive(:fetch).and_call_original
- # check if debouncing within a thread really works.
- expect(described_class).to have_received(:set).once
+ expect do
+ 1000.times { described_class.debounce }
+ end.to change(enqueued_jobs, :count).from(0).to(1)
- expect(Delayed::Job.count).to eq(4)
- expect(Delayed::Job.pluck(:id)).to include(other_handler,
- same_handler_out_of_timeframe,
- same_handler_within_timeframe_in_progress)
- expect(Delayed::Job.pluck(:id)).not_to include(same_handler_within_timeframe1,
- same_handler_within_timeframe2,
- same_handler_within_timeframe3)
- expect(
- Delayed::Job
- .where("handler LIKE ?", "%job_class: #{described_class}%")
- .last
- .run_at
- ).to be_within(3.seconds).of(described_class::JOB_DEBOUNCE_TIME.from_now)
+ expect(Rails.cache).to have_received(:fetch).once
+ end
end
end