Parcourir la source

Clean-up lock module

Leo il y a 8 ans
Parent
commit
d6207e9bce
3 fichiers modifiés avec 11 ajouts et 6 suppressions
  1. 3 0
      src/const.ml
  2. 1 0
      src/const.mli
  3. 7 6
      src/lock.ml

+ 3 - 0
src/const.ml

@@ -131,5 +131,8 @@ let tmp_file =
   get_var ~default (lazy "OC_TMP")
   |> Lazy.force
 ;;
+(* Name of lock file *)
+let lock_file =  "/tmp/.ocl.lock";;
+
 (* Default max number for launch *)
 let default_launch = 1;; (* TODO set it in rc file *)

+ 1 - 0
src/const.mli

@@ -47,6 +47,7 @@ val no_color : bool ref
 val rc_file_old : string Lazy.t
 val rc_file : string Lazy.t ref
 val tmp_file : string
+val lock_file : string
 (* Conf *)
 val default_launch : int
 

+ 7 - 6
src/lock.ml

@@ -44,13 +44,10 @@ type lock_status =
   | Free
   | Err
 ;;
-(* Name of the lock file *)
-(* TODO Put it in Const *)
-let lock_name = "/tmp/.ocl.lock";;
 
 (* Create lock file *)
 let lock () =
-  try Out_channel.write_all lock_name ~data:"OcLaunch is running and did not finish."
+  try Out_channel.write_all Const.lock_file ~data:"OcLaunch is running and did not finish."
   with Sys_error msg ->
     Messages.debug "Couldn't write in lock file.";
     Messages.debug "Due to";
@@ -59,7 +56,7 @@ let lock () =
 
 (* To know if we are locked, return None if there is no locker,  *)
 let status () =
-  match Sys.file_exists lock_name with
+  match Sys.file_exists Const.lock_file with
     `Yes -> Locked
   | `No -> Free
   | `Unknown -> Err
@@ -67,7 +64,11 @@ let status () =
 
 (* Remove the lock file *)
 let remove () =
-  Sys.remove lock_name
+  let open Sys in
+  file_exists Const.lock_file
+  |> function
+  | `Yes -> remove Const.lock_file
+  | _ -> Messages.debug "No lock file"
 ;;
 
 (* Pause the program until lock file is removed, until argument is the second *)