Browse Source

Prevent locker from staying after the program shutdown

Leo 9 years ago
parent
commit
075c5b7074
2 changed files with 29 additions and 4 deletions
  1. 19 4
      src/lock.ml
  2. 10 0
      src/oclaunch.ml

+ 19 - 4
src/lock.ml

@@ -38,6 +38,12 @@ open Core.Std;;
 
 
 (* Module to mange locking, when another instance of OcLaunch is running *)
 (* Module to mange locking, when another instance of OcLaunch is running *)
 
 
+(* Status of the locker *)
+type lock_status =
+    Locked
+  | Free
+  | Error
+;;
 (* Name of the lock file *)
 (* Name of the lock file *)
 (* TODO Put it in Const *)
 (* TODO Put it in Const *)
 let lock_name = "/tmp/.ocl.lock";;
 let lock_name = "/tmp/.ocl.lock";;
@@ -48,16 +54,25 @@ let lock () =
     with Sys_error msg -> Messages.debug "Couldn't write in lock file."
     with Sys_error msg -> Messages.debug "Couldn't write in lock file."
 ;;
 ;;
 
 
+(* To know if we are locked, return None if there is no locker,  *)
+let status () =
+    match Sys.file_exists lock_name with
+      `Yes -> Locked
+    | `No -> Free
+    | `Unknown -> Error
+;;
+
 (* Pause the program until lock file is removed, until argument is in second *)
 (* Pause the program until lock file is removed, until argument is in second *)
 (* TODO Implement recursion *)
 (* TODO Implement recursion *)
 let rec wait ?(until=10) ?(delay=1) () =
 let rec wait ?(until=10) ?(delay=1) () =
-    match Sys.file_exists lock_name with
-     `Yes -> Unix.sleep delay; wait ()
-    | `No -> lock ()
-    | `Unknown -> failwith "Problem with lock file"
+    match status () with
+      Locked -> Unix.sleep delay; wait ()
+    | Free -> lock ()
+    | Error -> failwith "Problem with lock file"
 ;;
 ;;
 
 
 (* Remove the lock file *)
 (* Remove the lock file *)
 let remove () =
 let remove () =
     Sys.remove lock_name
     Sys.remove lock_name
 ;;
 ;;
+

+ 10 - 0
src/oclaunch.ml

@@ -48,6 +48,16 @@ let build_info = ( "Build with OCaml version " ^ (Sys.ocaml_version) ^ " on " ^
 let () =
 let () =
   Command.run ~version:version_number ~build_info:build_info
   Command.run ~version:version_number ~build_info:build_info
   Command_def.commands;
   Command_def.commands;
+
+  (* Unlock, should be done before *)
+  Lock.(status ()
+    |> (function
+        Locked ->
+          Messages.warning "Removing lockfile, should be removed before. \
+          It's a bug!"; remove ()
+      | Free -> ()
+      | Error -> Messages.warning "Error with lockfile"
+  ));
   (* Reset display *)
   (* Reset display *)
   Messages.reset ()
   Messages.reset ()
 ;;
 ;;