@@ -785,6 +785,8 @@ def log_setup(mod_path, level, file = nil)
785
785
"engine" => { }
786
786
} . freeze
787
787
788
+ LOCK_FILE_EXT = ".lock"
789
+
788
790
# @!method public_rest_interface?
789
791
# @!method public_rest_interface=(flag)
790
792
#
@@ -883,6 +885,8 @@ def log_setup(mod_path, level, file = nil)
883
885
# Display the backtrace of all running threads on abort
884
886
attr_predicate :display_all_threads_state_on_abort? , true
885
887
888
+ attr_reader :lock_file
889
+
886
890
def initialize ( plan : ExecutablePlan . new )
887
891
@plan = plan
888
892
@argv_set = [ ]
@@ -955,6 +959,8 @@ def initialize(plan: ExecutablePlan.new)
955
959
@robots = App ::RobotNames . new
956
960
957
961
@base_setup_done = false
962
+
963
+ @lock_file = nil
958
964
end
959
965
960
966
# Loads the base configuration
@@ -1032,6 +1038,7 @@ def base_setup
1032
1038
load_base_config
1033
1039
unless @log_dir
1034
1040
find_and_create_log_dir
1041
+ lock_log_dir
1035
1042
end
1036
1043
setup_loggers ( redirections : true )
1037
1044
@@ -1041,6 +1048,33 @@ def base_setup
1041
1048
@base_setup_done = true
1042
1049
end
1043
1050
1051
+ def lock_log_dir
1052
+ return if log_dir_locked?
1053
+
1054
+ temp_lock_file = File . join ( log_dir , "#{ LOCK_FILE_EXT } .tmp" )
1055
+ final_lock_file = File . join ( log_dir , LOCK_FILE_EXT )
1056
+
1057
+ @lock_file = File . open ( temp_lock_file , File ::RDWR | File ::CREAT , 0o644 )
1058
+ if @lock_file . flock ( File ::LOCK_EX | File ::LOCK_NB )
1059
+ File . rename ( temp_lock_file , final_lock_file )
1060
+ @lock_file . close
1061
+ @lock_file = File . open ( final_lock_file , File ::RDWR | File ::CREAT , 0o644 )
1062
+ end
1063
+ end
1064
+
1065
+ def unlock_log_dir
1066
+ return unless log_dir_locked?
1067
+
1068
+ @lock_file . close if @lock_file
1069
+ @lock_file = nil
1070
+ end
1071
+
1072
+ def log_dir_locked?
1073
+ return unless @lock_file
1074
+
1075
+ @lock_file . path . end_with? ( LOCK_FILE_EXT )
1076
+ end
1077
+
1044
1078
# The inverse of #base_setup
1045
1079
def base_cleanup
1046
1080
unless base_setup_done?
@@ -1564,6 +1598,7 @@ def log_dir
1564
1598
1565
1599
# Reset the current log dir so that {#setup} picks a new one
1566
1600
def reset_log_dir
1601
+ unlock_log_dir
1567
1602
@log_dir = nil
1568
1603
end
1569
1604
0 commit comments