Skip to content

Commit

Permalink
feat(bookings/grant_area_access): minimize event processing
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Jan 20, 2025
1 parent 8920862 commit 1514fee
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions drivers/place/bookings/grant_area_access.cr
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,23 @@ class Place::Bookings::GrantAreaAccess < PlaceOS::Driver
security
end

@check_mutex : Mutex = Mutex.new
@performing_check : Bool = false
@check_queued : Bool = false

def ensure_booking_access
errors = [] of String

@check_mutex.synchronize do
if @performing_check
@check_queued = true
return
end

@performing_check = true
@check_queued = false
end

@mutex.synchronize do
now = Time.local(timezone).at_beginning_of_day
end_of_day = 3.days.from_now.in(timezone).at_end_of_day
Expand All @@ -158,13 +172,16 @@ class Place::Bookings::GrantAreaAccess < PlaceOS::Driver

# apply access this access to the system, need to find the differences
allocations = @allocations
return if allocations == access_required
logger.debug { "found #{allocations.size} users that need access" }

if allocations == access_required
logger.debug { "no access changes are required" }
return
end

remove = Hash(String, Array(String)).new { |hash, key| hash[key] = [] of String }
add = Hash(String, Array(String)).new { |hash, key| hash[key] = [] of String }

logger.debug { "found #{allocations.size} users that need access changes" }

# Collect all keys from both hashes
all_keys = allocations.keys.concat(access_required.keys)
all_keys.each do |key|
Expand Down Expand Up @@ -242,6 +259,11 @@ class Place::Bookings::GrantAreaAccess < PlaceOS::Driver

# save the newly applied access permissions
define_setting(:permissions_allocated, access_required)
ensure
@check_mutex.synchronize do
@performing_check = false
spawn { ensure_booking_access } if @check_queued
end
end

self[:sync_errors] = errors
Expand Down

0 comments on commit 1514fee

Please sign in to comment.