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] Improve user groups performace.

Pavel Balashou13a3a18a8fca
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 99247f93a3f..16feb6b2502 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
@@ -31,17 +31,22 @@ module Storages
include ManageNextcloudIntegrationJobMixin
DEBOUNCE_TIME = 5.seconds.freeze
+ THREAD_KEY = :manage_nextcloud_integration_events_job_debounce_happend_at
queue_with_priority :above_normal
def self.debounce
- count = Delayed::Job
- .where("handler LIKE ?", "%job_class: #{self}%")
- .where(locked_at: nil)
- .where('run_at <= ?', DEBOUNCE_TIME.from_now)
- .delete_all
- Rails.logger.info("deleted: #{count} jobs")
- set(wait: DEBOUNCE_TIME).perform_later
+ last_debounce_happend_at = Thread.current[THREAD_KEY]
+ if last_debounce_happend_at.blank? || Time.current > (last_debounce_happend_at + DEBOUNCE_TIME)
+ count = Delayed::Job
+ .where("handler LIKE ?", "%job_class: #{self}%")
+ .where(locked_at: nil)
+ .where('run_at <= ?', DEBOUNCE_TIME.from_now)
+ .delete_all
+ Rails.logger.info("deleted: #{count} jobs")
+ Thread.current[THREAD_KEY] = Time.current
+ set(wait: DEBOUNCE_TIME).perform_later
+ end
end
def perform
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 8f87a9295a2..c9b71ed150a 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 1 minute timeframe' do
+ it 'debounces job with 5 seconds timeframe' do
ActiveJob::Base.disable_test_adapter
other_handler = Storages::ManageNextcloudIntegrationCronJob.perform_later.provider_job_id
@@ -54,7 +54,7 @@ RSpec.describe Storages::ManageNextcloudIntegrationEventsJob, type: :job do
expect(Delayed::Job.count).to eq(6)
- described_class.debounce
+ 1000.times { described_class.debounce }
expect(Delayed::Job.count).to eq(4)
expect(Delayed::Job.pluck(:id)).to include(other_handler,
diff --git a/spec/lib/open_project/events_spec.rb b/spec/lib/open_project/events_spec.rb
index 806304e51d0..f07150f0520 100644
--- a/spec/lib/open_project/events_spec.rb
+++ b/spec/lib/open_project/events_spec.rb
@@ -36,6 +36,10 @@ RSpec.describe OpenProject::Events do
)
end
+ before do
+ allow(Storages::ManageNextcloudIntegrationEventsJob).to receive(:debounce)
+ end
+
%w[
PROJECT_STORAGE_CREATED
PROJECT_STORAGE_UPDATED
@@ -48,20 +52,17 @@ RSpec.describe OpenProject::Events do
let(:payload) { {} }
it do
- expect { subject }.not_to change(enqueued_jobs, :count)
+ subject
+ expect(Storages::ManageNextcloudIntegrationEventsJob).not_to have_received(:debounce)
end
end
- context 'when payload contains automatic project_folder_mpde' do
+ context 'when payload contains automatic project_folder_mode' do
let(:payload) { { project_folder_mode: :automatic } }
- it do
- expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)
- end
-
it do
subject
- expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)
+ expect(Storages::ManageNextcloudIntegrationEventsJob).to have_received(:debounce)
end
it do
@@ -90,13 +91,9 @@ RSpec.describe OpenProject::Events do
let(:payload) { {} }
- it do
- expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)
- end
-
it do
subject
- expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)
+ expect(Storages::ManageNextcloudIntegrationEventsJob).to have_received(:debounce)
end
end
end
@@ -108,20 +105,17 @@ RSpec.describe OpenProject::Events do
let(:payload) { {} }
it do
- expect { subject }.not_to change(enqueued_jobs, :count)
+ subject
+ expect(Storages::ManageNextcloudIntegrationEventsJob).not_to have_received(:debounce)
end
end
context 'when payload contains storage integration type' do
let(:payload) { { integration_type: 'Storages::Storage' } }
- it do
- expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)
- end
-
it do
subject
- expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)
+ expect(Storages::ManageNextcloudIntegrationEventsJob).to have_received(:debounce)
end
end
end
@@ -133,20 +127,17 @@ RSpec.describe OpenProject::Events do
let(:payload) { {} }
it do
- expect { subject }.not_to change(enqueued_jobs, :count)
+ subject
+ expect(Storages::ManageNextcloudIntegrationEventsJob).not_to have_received(:debounce)
end
end
context 'when payload contains some nextcloud related permissions as a diff' do
let(:payload) { { permissions_diff: [:read_files] } }
- it do
- expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)
- end
-
it do
subject
- expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)
+ expect(Storages::ManageNextcloudIntegrationEventsJob).to have_received(:debounce)
end
end
end
@@ -158,20 +149,17 @@ RSpec.describe OpenProject::Events do
let(:payload) { {} }
it do
- expect { subject }.not_to change(enqueued_jobs, :count)
+ subject
+ expect(Storages::ManageNextcloudIntegrationEventsJob).not_to have_received(:debounce)
end
end
context 'when payload contains some nextcloud related permissions' do
let(:payload) { { permissions: [:read_files] } }
- it do
- expect { subject }.to change(enqueued_jobs, :count).from(0).to(1)
- end
-
it do
subject
- expect(enqueued_jobs[0][:job]).to eq(Storages::ManageNextcloudIntegrationEventsJob)
+ expect(Storages::ManageNextcloudIntegrationEventsJob).to have_received(:debounce)
end
end
end