@@ -2,25 +2,45 @@ open Base
2
2
open Common
3
3
open Devkit
4
4
5
+ type t = {
6
+ state : State_t .state ;
7
+ lock : Lwt_mutex .t ;
8
+ }
9
+
5
10
let empty_repo_state () : State_t.repo_state = { pipeline_statuses = StringMap. empty }
6
11
7
- let empty () : State_t.state = { repos = Stringtbl. empty () ; bot_user_id = None }
12
+ let empty () : t =
13
+ let state = State_t. { repos = Stringtbl. empty () ; bot_user_id = None } in
14
+ { state; lock = Lwt_mutex. create () }
15
+
16
+ let find_or_add_repo' state repo_url = Stringtbl. find_or_add state.State_t. repos repo_url ~default: empty_repo_state
17
+
18
+ let set_repo_state { state; lock } repo_url repo_state =
19
+ Lwt_mutex. with_lock lock @@ fun () ->
20
+ Stringtbl. set state.repos ~key: repo_url ~data: repo_state;
21
+ Lwt. return_unit
8
22
9
- let find_or_add_repo ( state : State_t.state ) repo_url =
10
- Stringtbl. find_or_add state.repos repo_url ~default: empty_repo_state
23
+ let find_or_add_repo { state; lock } repo_url =
24
+ Lwt_mutex. with_lock lock @@ fun () -> find_or_add_repo' state repo_url |> Lwt. return
11
25
12
- let set_repo_pipeline_status ( state : State_t.state ) repo_url ~pipeline ~(branches : Github_t.branch list ) ~status =
26
+ let set_repo_pipeline_status { state; lock } repo_url ~pipeline ~(branches : Github_t.branch list ) ~status =
13
27
let set_branch_status branch_statuses =
14
28
let new_statuses = List. map branches ~f: (fun b -> b.name, status) in
15
29
let init = Option. value branch_statuses ~default: (Map. empty (module String )) in
16
30
List. fold_left new_statuses ~init ~f: (fun m (key , data ) -> Map. set m ~key ~data )
17
31
in
18
- let repo_state = find_or_add_repo state repo_url in
19
- repo_state.pipeline_statuses < - Map. update repo_state.pipeline_statuses pipeline ~f: set_branch_status
32
+ Lwt_mutex. with_lock lock @@ fun () ->
33
+ let repo_state = find_or_add_repo' state repo_url in
34
+ repo_state.pipeline_statuses < - Map. update repo_state.pipeline_statuses pipeline ~f: set_branch_status;
35
+ Lwt. return_unit
36
+
37
+ let set_bot_user_id { state; _ } user_id = state.State_t. bot_user_id < - Some user_id
38
+
39
+ let get_bot_user_id { state; _ } = state.State_t. bot_user_id
20
40
21
41
let log = Log. from " state"
22
42
23
- let save state path =
43
+ let save { state; _ } path =
24
44
let data = State_j. string_of_state state |> Yojson.Basic. from_string |> Yojson.Basic. pretty_to_string in
25
45
match write_to_local_file ~data path with
26
46
| Ok () -> Lwt. return @@ Ok ()
0 commit comments