mirror of
https://github.com/meineerde/rackstash.git
synced 2026-02-01 01:37:12 +00:00
Refactor exclusive file locking and include error handling
This commit is contained in:
parent
769b27dbdb
commit
7f5337c0e2
@ -200,11 +200,7 @@ module Rackstash
|
||||
|
||||
@mutex.synchronize do
|
||||
rotate_file
|
||||
|
||||
with_exclusive_lock = lock?
|
||||
@file.flock(::File::LOCK_EX) if with_exclusive_lock
|
||||
@file.syswrite(line)
|
||||
@file.flock(::File::LOCK_UN) if with_exclusive_lock
|
||||
with_flock { @file.syswrite(line) }
|
||||
end
|
||||
nil
|
||||
end
|
||||
@ -296,6 +292,17 @@ module Rackstash
|
||||
suffix = ".#{suffix}"
|
||||
@base_path.sub(/\A(.*?)(\.[^.\/]+)?\z/) { "#{$1}#{suffix}#{$2}" }
|
||||
end
|
||||
|
||||
def with_flock
|
||||
return yield unless lock?
|
||||
|
||||
begin
|
||||
@file.flock(::File::LOCK_EX)
|
||||
yield
|
||||
ensure
|
||||
@file.flock(::File::LOCK_UN)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user