Skip to content

Commit d496db1

Browse files
committed
formatting: Use let@ and match statements.
Uses `let@` and `match` statements to avoid nesting. This should increase code readability. Signed-off-by: Gabriel Buica <[email protected]>
1 parent 1e619ab commit d496db1

File tree

1 file changed

+79
-85
lines changed

1 file changed

+79
-85
lines changed

ocaml/xapi/taskHelper.ml

Lines changed: 79 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type t = API.ref_task
3030
(* creates a new task *)
3131
let make ~__context ~http_other_config ?(description = "") ?session_id
3232
?subtask_of label : t * t Uuidx.t =
33-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
33+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
3434
let uuid = Uuidx.make () in
3535
let uuid_str = Uuidx.to_string uuid in
3636
let ref = Ref.make () in
@@ -61,7 +61,7 @@ let rbac_assert_permission_fn = ref None
6161
(* required to break dep-cycle with rbac.ml *)
6262

6363
let assert_op_valid ?(ok_if_no_session_in_context = false) ~__context task_id =
64-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
64+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
6565
let assert_permission_task_op_any () =
6666
match !rbac_assert_permission_fn with
6767
| None ->
@@ -109,15 +109,15 @@ let assert_op_valid ?(ok_if_no_session_in_context = false) ~__context task_id =
109109
assert_permission_task_op_any ()
110110

111111
let get_name ~__context =
112-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
112+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
113113
let task_id = Context.get_task_id __context in
114114
if Ref.is_dummy task_id then
115115
Ref.name_of_dummy task_id
116116
else
117117
Db.Task.get_name_label ~__context ~self:task_id
118118

119119
let destroy ~__context task_id =
120-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
120+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
121121
if not (Ref.is_dummy task_id) then (
122122
assert_op_valid ~ok_if_no_session_in_context:true ~__context task_id ;
123123
Db_actions.DB_Action.Task.destroy ~__context ~self:task_id
@@ -133,40 +133,36 @@ let init () =
133133
Context.__make_task := make
134134

135135
let operate_on_db_task ~__context f =
136-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
136+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
137137
if Context.task_in_database __context then
138138
f (Context.get_task_id __context)
139139

140140
let set_description ~__context value =
141-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
142-
operate_on_db_task ~__context (fun self ->
143-
Db_actions.DB_Action.Task.set_name_description ~__context ~self ~value
144-
)
141+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
142+
let@ self = operate_on_db_task ~__context in
143+
Db_actions.DB_Action.Task.set_name_description ~__context ~self ~value
145144

146145
let add_to_other_config ~__context key value =
147-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
148-
operate_on_db_task ~__context (fun self ->
149-
Db_actions.DB_Action.Task.remove_from_other_config ~__context ~self ~key ;
150-
Db_actions.DB_Action.Task.add_to_other_config ~__context ~self ~key ~value
151-
)
146+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
147+
let@ self = operate_on_db_task ~__context in
148+
Db_actions.DB_Action.Task.remove_from_other_config ~__context ~self ~key ;
149+
Db_actions.DB_Action.Task.add_to_other_config ~__context ~self ~key ~value
152150

153151
let set_progress ~__context value =
154-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
155-
operate_on_db_task ~__context (fun self ->
156-
Db_actions.DB_Action.Task.set_progress ~__context ~self ~value
157-
)
152+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
153+
let@ self = operate_on_db_task ~__context in
154+
Db_actions.DB_Action.Task.set_progress ~__context ~self ~value
158155

159156
let set_external_pid ~__context pid =
160-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
161-
operate_on_db_task ~__context (fun self ->
162-
Db_actions.DB_Action.Task.set_externalpid ~__context ~self
163-
~value:(Int64.of_int pid)
164-
)
157+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
158+
let@ self = operate_on_db_task ~__context in
159+
Db_actions.DB_Action.Task.set_externalpid ~__context ~self
160+
~value:(Int64.of_int pid)
165161

166162
let clear_external_pid ~__context = set_external_pid ~__context (-1)
167163

168164
let set_result_on_task ~__context task_id result =
169-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
165+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
170166
match result with
171167
| None ->
172168
()
@@ -176,8 +172,9 @@ let set_result_on_task ~__context task_id result =
176172

177173
(** Only set the result without completing the task. Useful for vm import *)
178174
let set_result ~__context result =
179-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
180-
operate_on_db_task ~__context (fun t -> set_result_on_task ~__context t result)
175+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
176+
let@ self = operate_on_db_task ~__context in
177+
set_result_on_task ~__context self result
181178

182179
let status_to_string = function
183180
| `pending ->
@@ -196,38 +193,35 @@ let status_is_completed task_status =
196193

197194
let complete ~__context result =
198195
let@ () = finally_complete_tracing __context in
199-
operate_on_db_task ~__context (fun self ->
200-
let status = Db_actions.DB_Action.Task.get_status ~__context ~self in
201-
if status = `pending then (
202-
Db_actions.DB_Action.Task.set_allowed_operations ~__context ~self
203-
~value:[] ;
204-
Db_actions.DB_Action.Task.set_finished ~__context ~self
205-
~value:(Date.now ()) ;
206-
Db_actions.DB_Action.Task.set_progress ~__context ~self ~value:1. ;
207-
set_result_on_task ~__context self result ;
208-
Db_actions.DB_Action.Task.set_status ~__context ~self ~value:`success
209-
) else
210-
debug "the status of %s is: %s; cannot set it to `success"
211-
(Ref.really_pretty_and_small self)
212-
(status_to_string status)
213-
)
196+
let@ self = operate_on_db_task ~__context in
197+
let status = Db_actions.DB_Action.Task.get_status ~__context ~self in
198+
match status with
199+
| `pending ->
200+
Db_actions.DB_Action.Task.set_allowed_operations ~__context ~self
201+
~value:[] ;
202+
Db_actions.DB_Action.Task.set_finished ~__context ~self
203+
~value:(Date.now ()) ;
204+
Db_actions.DB_Action.Task.set_progress ~__context ~self ~value:1. ;
205+
set_result_on_task ~__context self result ;
206+
Db_actions.DB_Action.Task.set_status ~__context ~self ~value:`success
207+
| _ ->
208+
debug "the status of %s is: %s; cannot set it to `success"
209+
(Ref.really_pretty_and_small self)
210+
(status_to_string status)
214211

215212
let set_cancellable ~__context =
216-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
217-
operate_on_db_task ~__context (fun self ->
218-
Db_actions.DB_Action.Task.set_allowed_operations ~__context ~self
219-
~value:[`cancel]
220-
)
213+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
214+
let@ self = operate_on_db_task ~__context in
215+
Db_actions.DB_Action.Task.set_allowed_operations ~__context ~self
216+
~value:[`cancel]
221217

222218
let set_not_cancellable ~__context =
223-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
224-
operate_on_db_task ~__context (fun self ->
225-
Db_actions.DB_Action.Task.set_allowed_operations ~__context ~self
226-
~value:[]
227-
)
219+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
220+
let@ self = operate_on_db_task ~__context in
221+
Db_actions.DB_Action.Task.set_allowed_operations ~__context ~self ~value:[]
228222

229223
let is_cancelling ~__context =
230-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
224+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
231225
Context.task_in_database __context
232226
&&
233227
let l =
@@ -237,12 +231,12 @@ let is_cancelling ~__context =
237231
List.exists (fun (_, x) -> x = `cancel) l
238232

239233
let raise_cancelled ~__context =
240-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
234+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
241235
let task_id = Context.get_task_id __context in
242236
raise Api_errors.(Server_error (task_cancelled, [Ref.string_of task_id]))
243237

244238
let exn_if_cancelling ~__context =
245-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
239+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
246240
if is_cancelling ~__context then
247241
raise_cancelled ~__context
248242

@@ -261,40 +255,40 @@ let cancel_this ~__context ~self =
261255
(status_to_string status)
262256

263257
let cancel ~__context =
264-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
265-
operate_on_db_task ~__context (fun self -> cancel_this ~__context ~self)
258+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
259+
let@ self = operate_on_db_task ~__context in
260+
cancel_this ~__context ~self
266261

267262
let failed ~__context exn =
268263
let backtrace = Printexc.get_backtrace () in
269264
let@ () = finally_complete_tracing ~error:(exn, backtrace) __context in
270265
let code, params = ExnHelper.error_of_exn exn in
271-
operate_on_db_task ~__context (fun self ->
272-
let status = Db_actions.DB_Action.Task.get_status ~__context ~self in
273-
if status = `pending then (
274-
Db_actions.DB_Action.Task.set_progress ~__context ~self ~value:1. ;
275-
Db_actions.DB_Action.Task.set_error_info ~__context ~self
276-
~value:(code :: params) ;
277-
Db_actions.DB_Action.Task.set_backtrace ~__context ~self
278-
~value:(Sexplib.Sexp.to_string Backtrace.(sexp_of_t (get exn))) ;
279-
Db_actions.DB_Action.Task.set_finished ~__context ~self
280-
~value:(Date.now ()) ;
281-
Db_actions.DB_Action.Task.set_allowed_operations ~__context ~self
282-
~value:[] ;
283-
if code = Api_errors.task_cancelled then
284-
Db_actions.DB_Action.Task.set_status ~__context ~self
285-
~value:`cancelled
286-
else
287-
Db_actions.DB_Action.Task.set_status ~__context ~self ~value:`failure
288-
) else
289-
debug "the status of %s is %s; cannot set it to %s"
290-
(Ref.really_pretty_and_small self)
291-
(status_to_string status)
292-
( if code = Api_errors.task_cancelled then
293-
"`cancelled"
294-
else
295-
"`failure"
296-
)
297-
)
266+
let@ self = operate_on_db_task ~__context in
267+
let status = Db_actions.DB_Action.Task.get_status ~__context ~self in
268+
match status with
269+
| `pending ->
270+
Db_actions.DB_Action.Task.set_progress ~__context ~self ~value:1. ;
271+
Db_actions.DB_Action.Task.set_error_info ~__context ~self
272+
~value:(code :: params) ;
273+
Db_actions.DB_Action.Task.set_backtrace ~__context ~self
274+
~value:(Sexplib.Sexp.to_string Backtrace.(sexp_of_t (get exn))) ;
275+
Db_actions.DB_Action.Task.set_finished ~__context ~self
276+
~value:(Date.now ()) ;
277+
Db_actions.DB_Action.Task.set_allowed_operations ~__context ~self
278+
~value:[] ;
279+
if code = Api_errors.task_cancelled then
280+
Db_actions.DB_Action.Task.set_status ~__context ~self ~value:`cancelled
281+
else
282+
Db_actions.DB_Action.Task.set_status ~__context ~self ~value:`failure
283+
| _ ->
284+
debug "the status of %s is %s; cannot set it to %s"
285+
(Ref.really_pretty_and_small self)
286+
(status_to_string status)
287+
( if code = Api_errors.task_cancelled then
288+
"`cancelled"
289+
else
290+
"`failure"
291+
)
298292

299293
type id = Sm of string | Xenops of string * string
300294

@@ -313,7 +307,7 @@ let task_to_id_exn task =
313307
with_lock task_tbl_m (fun () -> Hashtbl.find task_to_id_tbl task)
314308

315309
let register_task __context ?(cancellable = true) id =
316-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
310+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
317311
let task = Context.get_task_id __context in
318312
with_lock task_tbl_m (fun () ->
319313
Hashtbl.replace id_to_task_tbl id task ;
@@ -329,7 +323,7 @@ let register_task __context ?(cancellable = true) id =
329323
()
330324

331325
let unregister_task __context id =
332-
Context.with_tracing ~__context __FUNCTION__ @@ fun __context ->
326+
let@ __context = Context.with_tracing ~__context __FUNCTION__ in
333327
(* The rest of the XenAPI Task won't be cancellable *)
334328
set_not_cancellable ~__context ;
335329
with_lock task_tbl_m (fun () ->

0 commit comments

Comments
 (0)