Skip to content

Commit

Permalink
Merge pull request #14437 from opf/bug/51316-poor-performance-of-user…
Browse files Browse the repository at this point in the history
…-groups-when-i-remove-user-from-group-+-500-error

[#51316] Poor performance of user groups when I remove user from group + 500 error
  • Loading branch information
ba1ash authored Dec 19, 2023
2 parents 314982e + 13a3a18 commit 59520ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ class ManageNextcloudIntegrationEventsJob < ApplicationJob
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
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
Expand All @@ -54,7 +54,7 @@

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,
Expand Down
48 changes: 18 additions & 30 deletions spec/lib/open_project/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def fire_event(event_constant_name)
)
end

before do
allow(Storages::ManageNextcloudIntegrationEventsJob).to receive(:debounce)
end

%w[
PROJECT_STORAGE_CREATED
PROJECT_STORAGE_UPDATED
Expand All @@ -48,20 +52,17 @@ def fire_event(event_constant_name)
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
Expand Down Expand Up @@ -90,13 +91,9 @@ def fire_event(event_constant_name)

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
Expand All @@ -108,20 +105,17 @@ def fire_event(event_constant_name)
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
Expand All @@ -133,20 +127,17 @@ def fire_event(event_constant_name)
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
Expand All @@ -158,20 +149,17 @@ def fire_event(event_constant_name)
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
Expand Down

0 comments on commit 59520ff

Please sign in to comment.