Skip to content

Commit a4f217a

Browse files
committed
Refactor lock warning to eliminate code duplication
Extract the warning message generation into a helper function to avoid repeating the same logic for both the timeout and no-timeout cases. This makes the code more maintainable and easier to read. Signed-off-by: sabine <[email protected]>
1 parent 440c622 commit a4f217a

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

src/dune_util/global_lock.ml

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ module Lock_held_by = struct
9090
;;
9191
end
9292

93+
let emit_lock_warning lock_held_by =
94+
let pid_suffix =
95+
match lock_held_by with
96+
| Lock_held_by.Unknown -> ""
97+
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid
98+
in
99+
let condition =
100+
match lock_held_by with
101+
| Pid_from_lockfile _ -> "If this process is no longer running"
102+
| Unknown -> "If no other dune process is running"
103+
in
104+
User_warning.emit
105+
[ Pp.textf
106+
"Build directory is locked by another dune process%s. Waiting for the lock to be \
107+
released..."
108+
pid_suffix
109+
; Pp.textf "%s, delete the lock file: %s" condition (Path.Build.to_string lock_file)
110+
]
111+
;;
112+
93113
let lock ~timeout =
94114
match Config.(get global_lock) with
95115
| `Disabled -> Ok ()
@@ -103,26 +123,7 @@ let lock ~timeout =
103123
(match Lock.lock () with
104124
| `Success -> `Success
105125
| `Failure ->
106-
let lock_held_by = Lock_held_by.read_lock_file () in
107-
User_warning.emit
108-
([ Pp.textf
109-
"Build directory is locked by another dune process%s. Waiting for the \
110-
lock to be released..."
111-
(match lock_held_by with
112-
| Unknown -> ""
113-
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
114-
]
115-
@ (match lock_held_by with
116-
| Pid_from_lockfile _ ->
117-
[ Pp.textf
118-
"If this process is no longer running, delete the lock file: %s"
119-
(Path.Build.to_string lock_file)
120-
]
121-
| Unknown ->
122-
[ Pp.textf
123-
"If no other dune process is running, delete the lock file: %s"
124-
(Path.Build.to_string lock_file)
125-
]));
126+
emit_lock_warning (Lock_held_by.read_lock_file ());
126127
`Failure)
127128
| Some timeout ->
128129
let warned = ref false in
@@ -131,30 +132,11 @@ let lock ~timeout =
131132
match Lock.lock () with
132133
| `Success -> `Stop
133134
| `Failure ->
134-
if not !warned
135-
then (
136-
warned := true;
137-
let lock_held_by = Lock_held_by.read_lock_file () in
138-
User_warning.emit
139-
([ Pp.textf
140-
"Build directory is locked by another dune process%s. Waiting for \
141-
the lock to be released..."
142-
(match lock_held_by with
143-
| Unknown -> ""
144-
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
145-
]
146-
@ (match lock_held_by with
147-
| Pid_from_lockfile _ ->
148-
[ Pp.textf
149-
"If this process is no longer running, delete the lock file: %s"
150-
(Path.Build.to_string lock_file)
151-
]
152-
| Unknown ->
153-
[ Pp.textf
154-
"If no other dune process is running, delete the lock file: %s"
155-
(Path.Build.to_string lock_file)
156-
])));
157-
`Continue)
135+
if not !warned
136+
then (
137+
warned := true;
138+
emit_lock_warning (Lock_held_by.read_lock_file ()));
139+
`Continue)
158140
with
159141
| `Timed_out -> `Failure
160142
| `Success -> `Success)

0 commit comments

Comments
 (0)