Skip to content

Commit

Permalink
Adds a scope on Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mereghost committed Jun 27, 2024
1 parent c2d68d2 commit bd26047
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 2 additions & 0 deletions modules/storages/app/models/storages/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class Storage < ApplicationRecord

scope :automatic_management_enabled, -> { where("provider_fields->>'automatically_managed' = 'true'") }

scope :in_project, ->(project_id) { joins(project_storages: :project).where(project_storages: { project_id: }) }

enum health_status: {
pending: "pending",
healthy: "healthy",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
# frozen_string_literal: true

#-- copyright
#++

module Storages
class AutomaticallyManagedStorageSyncJob < ApplicationJob
include GoodJob::ActiveJobExtensions::Concurrency
queue_with_priority :above_normal

SINGLE_THREAD_DEBOUNCE_TIME = 4.seconds
class << self
def debounce(storage)
key = "sync-#{storage.short_provider_type}-#{storage.id}"
timestamp = RequestStore.store[key]

return false if timestamp.present? && (timestamp + SINGLE_THREAD_DEBOUNCE_TIME) > Time.current

result = set(wait: 5.seconds).perform_later(storage)
RequestStore.store[key] = Time.current
result
end
end

good_job_control_concurrency_with(
total_limit: 2,
Expand All @@ -36,6 +27,19 @@ def debounce(storage)
end
end

class << self
def debounce(storage)
key = "sync-#{storage.short_provider_type}-#{storage.id}"
timestamp = RequestStore.store[key]

return false if timestamp.present? && (timestamp + SINGLE_THREAD_DEBOUNCE_TIME) > Time.current

result = set(wait: 5.seconds).perform_later(storage)
RequestStore.store[key] = Time.current
result
end
end

def perform(storage)
return unless storage.configured? && storage.automatically_managed?

Expand Down
6 changes: 2 additions & 4 deletions modules/storages/lib/open_project/storages/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def self.permissions
OpenProject::Events::MEMBER_DESTROYED
].each do |event|
OpenProject::Notifications.subscribe(event) do |payload|
::Storages::Storage.joins(project_storages: :project)
.where(project_storages: { project_id: payload[:member].project_id }).find_each do |storage|
::Storages::Storage.in_project(payload[:member].project_id).find_each do |storage|
::Storages::AutomaticallyManagedStorageSyncJob.debounce(storage)
end
end
Expand All @@ -70,8 +69,7 @@ def self.permissions
OpenProject::Events::PROJECT_ARCHIVED,
OpenProject::Events::PROJECT_UNARCHIVED].each do |event|
OpenProject::Notifications.subscribe(event) do |payload|
::Storages::Storage.joins(project_storages: :project)
.where(project_storages: { project: payload[:project] }).find_each do |storage|
::Storages::Storage.in_project(payload[:project].id).find_each do |storage|
::Storages::AutomaticallyManagedStorageSyncJob.debounce(storage)
end
end
Expand Down

0 comments on commit bd26047

Please sign in to comment.