From c7ff322303c98722e7b37579a270001a6eacb40a Mon Sep 17 00:00:00 2001 From: martinahansen Date: Wed, 10 Oct 2012 13:20:55 +0000 Subject: [PATCH] added some file locking to biopieces.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@1962 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/lib/maasha/biopieces.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/code_ruby/lib/maasha/biopieces.rb b/code_ruby/lib/maasha/biopieces.rb index 6c54658..8d030df 100644 --- a/code_ruby/lib/maasha/biopieces.rb +++ b/code_ruby/lib/maasha/biopieces.rb @@ -567,6 +567,7 @@ class Status time0 = Time.new.strftime("%Y-%m-%d %X") File.open(path, "w") do |fh| + fh.flock(File::LOCK_EX) fh.puts [time0, ARGV.join(" ")].join(";") end end @@ -576,12 +577,14 @@ class Status status = "" File.open(path, "r") do |fh| + fh.flock(File::LOCK_SH) status = fh.read.chomp end status = "#{status};#{tmpdir_path}\n" File.open(path, "w") do |fh| + fh.flock(File::LOCK_EX) fh << status end end @@ -590,6 +593,7 @@ class Status # and return this or nil if not found. def get_tmpdir File.open(path, "r") do |fh| + fh.flock(File::LOCK_SH) tmpdir_path = fh.read.chomp.split(";").last return tmpdir_path if File.directory?(tmpdir_path) end @@ -603,15 +607,22 @@ class Status user = ENV["USER"] script = File.basename($0) - stream = File.open(path) - time0, args = stream.first.split(";") - stream.close + time0 = nil + args = nil + + File.open(path, "r") do |fh| + fh.flock(File::LOCK_SH) + time0, args = fh.first.split(";") + end elap = time_diff(time0, time1) command = [script, args].join(" ") log_file = File.join(ENV["BP_LOG"], "biopieces.log") - File.open(log_file, "a") { |file| file.puts [time0, time1, elap, user, exit_status, command].join("\t") } + File.open(log_file, "a") do |fh| + fh.flock(File::LOCK_EX) + fh.puts [time0, time1, elap, user, exit_status, command].join("\t") + end end # Delete status file. -- 2.39.5