Skip to content

Commit ab6945d

Browse files
committed
state: atomic, no lwt
1 parent ab1886b commit ab6945d

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

lib/action.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
136136
let current_status = n.state in
137137
let rules = cfg.status_rules.rules in
138138
let action_on_match (branches : branch list) ~notify_channels ~notify_dm =
139-
let%lwt () = State.set_repo_pipeline_status ctx.state repo.url ~pipeline ~branches ~status:current_status in
139+
State.set_repo_pipeline_status ctx.state repo.url ~pipeline ~branches ~status:current_status;
140140
let%lwt direct_message =
141141
if notify_dm then begin
142142
match%lwt Slack_api.lookup_user ~ctx ~cfg ~email:n.commit.commit.author.email () with
@@ -172,7 +172,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
172172
Lwt.return (direct_message @ chans)
173173
in
174174
if Context.is_pipeline_allowed ctx repo.url ~pipeline then begin
175-
let%lwt repo_state = State.find_or_add_repo ctx.state repo.url in
175+
let repo_state = State.find_or_add_repo ctx.state repo.url in
176176
match Rule.Status.match_rules ~rules n with
177177
| Some (Ignore, _, _) | None -> Lwt.return []
178178
| Some (Allow, notify_channels, notify_dm) -> action_on_match n.branches ~notify_channels ~notify_dm
@@ -342,10 +342,9 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
342342
( match ctx.state_filepath with
343343
| None -> Lwt.return_unit
344344
| Some path ->
345-
( match%lwt State.save ctx.state path with
346-
| Ok () -> Lwt.return_unit
347-
| Error e -> action_error e
348-
)
345+
match State.save ctx.state path with
346+
| Ok () -> Lwt.return_unit
347+
| Error e -> action_error e
349348
)
350349
)
351350
)
@@ -369,7 +368,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
369368
ctx.state_filepath
370369
|> Option.map_default
371370
(fun path ->
372-
match%lwt State.save ctx.state path with
371+
match State.save ctx.state path with
373372
| Ok () -> Lwt.return_unit
374373
| Error msg ->
375374
log#warn "failed to save state file %s : %s" path msg;

lib/context.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ let refresh_state ctx =
100100
| Error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
101101
| Ok file ->
102102
(* todo: extract state related parts to state.ml *)
103-
let state = { ctx.state with state = State_j.state_of_string file } in
103+
let state = { State.state = State_j.state_of_string file } in
104104
Ok { ctx with state }
105105
end
106106
else Ok ctx

lib/state.ml

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
open Common
22
open Devkit
33

4-
type t = {
5-
state : State_t.state;
6-
lock : Lwt_mutex.t; (** protect access to mutable string map `pipeline_statuses` *)
7-
}
4+
let log = Log.from "state"
5+
6+
type t = { state : State_t.state }
87

98
let empty_repo_state () : State_t.repo_state = { pipeline_statuses = StringMap.empty }
109

1110
let empty () : t =
1211
let state = State_t.{ repos = Stringtbl.empty (); bot_user_id = None } in
13-
{ state; lock = Lwt_mutex.create () }
12+
{ state }
1413

1514
let find_or_add_repo' state repo_url =
1615
match Stringtbl.find_opt state.State_t.repos repo_url with
@@ -20,31 +19,23 @@ let find_or_add_repo' state repo_url =
2019
Stringtbl.add state.State_t.repos repo_url new_repo;
2120
new_repo
2221

23-
let set_repo_state { state; lock } repo_url repo_state =
24-
Lwt_mutex.with_lock lock @@ fun () ->
25-
Stringtbl.replace state.repos repo_url repo_state;
26-
Lwt.return_unit
22+
let set_repo_state { state } repo_url repo_state = Stringtbl.replace state.repos repo_url repo_state
23+
let find_or_add_repo { state } repo_url = find_or_add_repo' state repo_url
2724

28-
let find_or_add_repo { state; lock } repo_url =
29-
Lwt_mutex.with_lock lock @@ fun () -> find_or_add_repo' state repo_url |> Lwt.return
30-
31-
let set_repo_pipeline_status { state; lock } repo_url ~pipeline ~(branches : Github_t.branch list) ~status =
25+
let set_repo_pipeline_status { state } repo_url ~pipeline ~(branches : Github_t.branch list) ~status =
3226
let set_branch_status branch_statuses =
3327
let new_statuses = List.map (fun (b : Github_t.branch) -> b.name, status) branches in
3428
let init = Option.default StringMap.empty branch_statuses in
3529
Some (List.fold_left (fun m (key, data) -> StringMap.add key data m) init new_statuses)
3630
in
37-
Lwt_mutex.with_lock lock @@ fun () ->
3831
let repo_state = find_or_add_repo' state repo_url in
39-
repo_state.pipeline_statuses <- StringMap.update pipeline set_branch_status repo_state.pipeline_statuses;
40-
Lwt.return_unit
32+
repo_state.pipeline_statuses <- StringMap.update pipeline set_branch_status repo_state.pipeline_statuses
4133

4234
let set_bot_user_id { state; _ } user_id = state.State_t.bot_user_id <- Some user_id
4335
let get_bot_user_id { state; _ } = state.State_t.bot_user_id
44-
let log = Log.from "state"
4536

4637
let save { state; _ } path =
4738
let data = State_j.string_of_state state |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
4839
match write_to_local_file ~data path with
49-
| Ok () -> Lwt.return @@ Ok ()
50-
| Error e -> Lwt.return @@ fmt_error "error while writing to local file %s: %s\nfailed to save state" path e
40+
| Ok () -> Ok ()
41+
| Error e -> fmt_error "error while writing to local file %s: %s\nfailed to save state" path e

test/test.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let process_gh_payload ~(secrets : Config_t.secrets) ~config (kind, path, state_
4040
let secrets = { secrets with repos = [ { url = repo.url; gh_token = None; gh_hook_secret = None } ] } in
4141
let ctx = Context.make () in
4242
ctx.secrets <- Some secrets;
43-
let%lwt _ = State.find_or_add_repo ctx.state repo.url in
43+
let (_ : State_t.repo_state) = State.find_or_add_repo ctx.state repo.url in
4444
match state_path with
4545
| None ->
4646
Context.set_repo_config ctx repo.url config;
@@ -52,7 +52,7 @@ let process_gh_payload ~(secrets : Config_t.secrets) ~config (kind, path, state_
5252
Lwt.return ctx
5353
| Ok file ->
5454
let repo_state = State_j.repo_state_of_string file in
55-
let%lwt () = State.set_repo_state ctx.state repo.url repo_state in
55+
State.set_repo_state ctx.state repo.url repo_state;
5656
Context.set_repo_config ctx repo.url config;
5757
Lwt.return ctx
5858
in

0 commit comments

Comments
 (0)