Class: Soba::Infrastructure::LockManager
- Inherits:
-
Object
- Object
- Soba::Infrastructure::LockManager
- Defined in:
- lib/soba/infrastructure/lock_manager.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
seconds
5- DEFAULT_STALE_THRESHOLD =
5 minutes
300- RETRY_INTERVAL =
seconds
0.1
Instance Method Summary collapse
- #acquire_lock(resource_name, timeout: 0, stale_threshold: DEFAULT_STALE_THRESHOLD) ⇒ Object
- #cleanup_stale_locks(threshold: DEFAULT_STALE_THRESHOLD) ⇒ Object
-
#initialize(lock_directory: nil) ⇒ LockManager
constructor
A new instance of LockManager.
- #locked?(resource_name) ⇒ Boolean
- #release_lock(resource_name) ⇒ Object
- #with_lock(resource_name, timeout: DEFAULT_TIMEOUT, stale_threshold: DEFAULT_STALE_THRESHOLD) ⇒ Object
Constructor Details
#initialize(lock_directory: nil) ⇒ LockManager
Returns a new instance of LockManager.
15 16 17 18 |
# File 'lib/soba/infrastructure/lock_manager.rb', line 15 def initialize(lock_directory: nil) @lock_directory = lock_directory || default_lock_directory ensure_lock_directory_exists end |
Instance Method Details
#acquire_lock(resource_name, timeout: 0, stale_threshold: DEFAULT_STALE_THRESHOLD) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/soba/infrastructure/lock_manager.rb', line 20 def acquire_lock(resource_name, timeout: 0, stale_threshold: DEFAULT_STALE_THRESHOLD) lock_file = lock_file_path(resource_name) deadline = Time.now + timeout if timeout > 0 loop do # Check for stale lock if File.exist?(lock_file) && stale_threshold > 0 begin if Time.now - File.mtime(lock_file) > stale_threshold # Remove stale lock File.delete(lock_file) end rescue Errno::ENOENT # File was deleted by another process, continue rescue # Other errors, ignore and continue end end # Try to acquire lock begin File.open(lock_file, File::WRONLY | File::CREAT | File::EXCL) do |f| f.write(Process.pid.to_s) end return true rescue Errno::EEXIST # Lock already exists if timeout > 0 && Time.now < deadline sleep RETRY_INTERVAL next else return false end end end end |
#cleanup_stale_locks(threshold: DEFAULT_STALE_THRESHOLD) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/soba/infrastructure/lock_manager.rb', line 96 def cleanup_stale_locks(threshold: DEFAULT_STALE_THRESHOLD) return [] unless Dir.exist?(@lock_directory) removed = [] Dir.glob(File.join(@lock_directory, '*.lock')).each do |lock_file| if Time.now - File.mtime(lock_file) > threshold begin File.delete(lock_file) rescue nil end removed << File.basename(lock_file, '.lock') end end removed end |
#locked?(resource_name) ⇒ Boolean
91 92 93 94 |
# File 'lib/soba/infrastructure/lock_manager.rb', line 91 def locked?(resource_name) lock_file = lock_file_path(resource_name) File.exist?(lock_file) end |
#release_lock(resource_name) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/soba/infrastructure/lock_manager.rb', line 57 def release_lock(resource_name) lock_file = lock_file_path(resource_name) return false unless File.exist?(lock_file) # Check if we own the lock begin pid = File.read(lock_file).strip.to_i if pid == Process.pid File.delete(lock_file) return true end rescue Errno::ENOENT # File was already deleted return false rescue StandardError # Error reading file end false end |
#with_lock(resource_name, timeout: DEFAULT_TIMEOUT, stale_threshold: DEFAULT_STALE_THRESHOLD) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/soba/infrastructure/lock_manager.rb', line 79 def with_lock(resource_name, timeout: DEFAULT_TIMEOUT, stale_threshold: DEFAULT_STALE_THRESHOLD) unless acquire_lock(resource_name, timeout: timeout, stale_threshold: stale_threshold) raise LockTimeoutError, "Failed to acquire lock for #{resource_name} within #{timeout} seconds" end begin yield ensure release_lock(resource_name) end end |