job.ml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *)
  26. (* streamline wording with https://v2.ocaml.org/api/Queue.html *)
  27. type queue = Queue of string
  28. type slot = Slot of string
  29. type task = Task of string
  30. type f = queue -> (queue, string) result Lwt.t
  31. let qn = Queue "app/var/spool/job/"
  32. let cur = Slot "cur/"
  33. let err = Slot "err/"
  34. let new_ = Slot "new/"
  35. let run = Slot "run/"
  36. let tmp = Slot "tmp/"
  37. let wait = Slot "wait/"
  38. module P = struct
  39. let Slot cur' = cur
  40. let Slot err' = err
  41. let Slot new_' = new_
  42. let Slot run' = run
  43. let Slot tmp' = tmp
  44. let Slot wait' = wait
  45. end
  46. let fn_ (Queue q) (Slot s) (Task j) = q ^ s ^ j
  47. let pp_s ppf (Slot s) = Format.pp_print_string ppf s
  48. let pp_t ppf (Task j) = Format.pp_print_string ppf j
  49. (** exponentially growing delay.
  50. 0 is zero. Has https://encore.dev/blog/retries#jitter ?*)
  51. let do_wait ?(now = Ptime_clock.now ()) ?(jitter = (Random.float 0.1) -. 0.05) i =
  52. assert (jitter >= -0.05);
  53. assert (jitter <= 0.05);
  54. assert (i >= 0);
  55. let f = 60 * (i |> Int.shift_left 1 |> pred)
  56. |> float_of_int in
  57. Logr.debug (fun m -> m "%s.%s jitter %f %% = %.2fs" "Job" "do_wait" jitter (f *. jitter));
  58. let f = f *. (1.0 +. jitter) in
  59. match f
  60. |> Ptime.Span.of_float_s
  61. |> Option.get
  62. |> Ptime.add_span now with
  63. | None -> now
  64. | Some t -> t
  65. let rfc3339 t =
  66. let (y, m, d), ((hh, mm, ss), tz_s) = Ptime.to_date_time t in
  67. assert (tz_s = 0);
  68. Printf.sprintf "%04d-%02d-%02dT%02d%02d%02dZ" y m d hh mm ss
  69. let move que job src dst =
  70. Logr.debug (fun m -> m "%s.%s %a -> %a %a" "Job" "move" pp_s src pp_s dst pp_t job);
  71. Unix.rename (fn_ que src job) (fn_ que dst job)
  72. let compute_nonce byt =
  73. byt
  74. |> Mcdb.hash32_byt
  75. |> Optint.to_string
  76. let compute_fn due n nonce =
  77. let due = due |> rfc3339 in
  78. Task (Printf.sprintf "%s.%d.%s.s" due n nonce)
  79. (** similar Queue.add *)
  80. let enqueue ?(due = Ptime_clock.now ()) q' n byt =
  81. Logr.debug (fun m -> m "%s.%s %s" "Job" "enqueue" (due |> rfc3339));
  82. let nonce = compute_nonce byt in
  83. let fn = compute_fn due n nonce in
  84. let tmp' = fn_ q' tmp fn in
  85. let new' = fn_ q' new_ fn in
  86. Logr.debug (fun m -> m "%s.%s %s" "Job" "enqueue" new');
  87. let perm = 0o444 in
  88. (* @TODO rather an exclusive create *)
  89. tmp' |> File.out_channel_append ~perm (fun oc -> byt |> output_bytes oc);
  90. move q' fn tmp new_;
  91. Ok new_
  92. let p_true _ = true
  93. let any ?(pred = p_true) (Slot qn) (Queue qb) =
  94. (* Logr.debug (fun m -> m "%s.%s %s" "Job" "find_first" qn); *)
  95. let pred fn = St.is_suffix ~affix:".s" fn && pred fn in
  96. Option.bind
  97. (File.any pred (qb ^ qn))
  98. (fun v -> Some (Task v))
  99. let any_due ?(due = Ptime_clock.now ()) ?(wait = wait) q =
  100. let due = rfc3339 due in
  101. let pred fn =
  102. match fn |> String.split_on_char '.' with
  103. | [t; _; _; "s"] -> String.compare t due <= 0
  104. | _ -> false
  105. in
  106. (* Logr.debug (fun m -> m "%s.%s %s" "Job" "find_any_due" due); *)
  107. any ~pred wait q
  108. let wait_or_err ?(wait = wait) q s j =
  109. let maxtries = 13 in
  110. let Slot s' = s in
  111. let Task j' = j in
  112. Logr.debug (fun m -> m "%s.%s %s %s" "Job" "wait_or_err" s' j');
  113. assert (2 == (s' |> String.split_on_char '/' |> List.length));
  114. assert (1 == (j' |> String.split_on_char '/' |> List.length));
  115. match j' |> String.split_on_char '.' with
  116. | [t0; n; nonce; "s"] ->
  117. let n = n |> int_of_string |> succ in
  118. if n > maxtries
  119. then move q j s err
  120. else
  121. let now = match t0 |> Ptime.of_rfc3339 with
  122. | Ok (t,_,_) -> t
  123. | _ -> Ptime_clock.now () in
  124. let t = n |> do_wait ~now in
  125. let jn' = compute_fn t n nonce in
  126. Unix.rename (fn_ q s j) (fn_ q wait jn')
  127. | _ ->
  128. Logr.err (fun m -> m "%s invalid job '%s'" E.e1015 j');
  129. move q j s err