lib/rflow/pid_file.rb in rflow-1.0.1 vs lib/rflow/pid_file.rb in rflow-1.1.0

- old
+ new

@@ -8,21 +8,33 @@ @path = path end def read return nil unless File.exist? path - File.read(path).to_i + contents = File.read(path) + if contents.empty? + RFlow.logger.warn "Ignoring empty PID file #{path}" + nil + else + contents.to_i + end end def write(pid = $$) return unless validate? - RFlow.logger.debug "Writing PID #{pid} file '#{to_s}'" + RFlow.logger.debug "Writing PID #{pid} to file '#{to_s}'" + tmp_path = File.join(File.dirname(path), ".#{File.basename(path)}") + if File.exist? tmp_path + RFlow.logger.warn "Deleting stale temp PID file #{tmp_path}" + File.delete(tmp_path) + end pid_fp = begin - tmp_path = File.join(File.dirname(path), ".#{File.basename(path)}") File.open(tmp_path, File::RDWR|File::CREAT|File::EXCL, 0644) - rescue Errno::EEXIST - retry + rescue Errno::EACCES => e + RFlow.logger.fatal "Access error while writing temp PID file '#{tmp_path}'" + RFlow.logger.fatal "Exception #{e.class}: #{e.message}" + abort end pid_fp.syswrite("#{pid}\n") File.rename(pid_fp.path, path) pid_fp.close