lib/frontkick/command.rb in frontkick-0.3.3 vs lib/frontkick/command.rb in frontkick-0.3.4
- old
+ new
@@ -6,11 +6,11 @@
def self.exec(cmd, opts = {})
stdout, stderr, exit_code, duration = nil
stdin, out, err, wait_thr, pid = nil
cmd_array = cmd.kind_of?(Array) ? cmd : [cmd]
- lock_fd = file_lock(opts[:exclusive]) if opts[:exclusive]
+ lock_fd = file_lock(opts[:exclusive], opts[:exclusive_blocking]) if opts[:exclusive]
begin
timeout(opts[:timeout]) do # nil is for no timeout
duration = Benchmark.realtime do
stdin, out, err, wait_thr = Open3.popen3(*cmd_array)
stdin.close
@@ -48,17 +48,22 @@
end
# Use file lock to perfome exclusive operation
#
# @param lock_file file path used to lock
+ # @param blocking blocking or non-blocking. default is nil (false)
# @return file descriptor
# @raise Fontkick::Locked if locked
- def self.file_lock(lock_file)
+ def self.file_lock(lock_file, blocking = nil)
lock_fd = File.open(lock_file, File::RDWR|File::CREAT, 0644)
- success = lock_fd.flock(File::LOCK_EX|File::LOCK_NB)
- unless success
- lock_fd.flock(File::LOCK_UN)
- raise Frontkick::Locked
+ if blocking
+ lock_fd.flock(File::LOCK_EX)
+ else
+ success = lock_fd.flock(File::LOCK_EX|File::LOCK_NB)
+ unless success
+ lock_fd.flock(File::LOCK_UN)
+ raise Frontkick::Locked
+ end
end
lock_fd
end
end
end