]> git.donarmstrong.com Git - biopieces.git/commitdiff
added some file locking to biopieces.rb
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 10 Oct 2012 13:20:55 +0000 (13:20 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 10 Oct 2012 13:20:55 +0000 (13:20 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1962 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/biopieces.rb

index 6c54658ff6de280f0e050ce7ba7602e56bb5f3f7..8d030dfa80d08edc0aa32cac82e497196b3d51de 100644 (file)
@@ -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.