-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathevent.ex
More file actions
43 lines (38 loc) · 1.4 KB
/
event.ex
File metadata and controls
43 lines (38 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
defmodule CodeCorps.GitHub.Event do
@moduledoc ~S"""
In charge of marking `GithubEvent` records as "processing", "processed" or
"errored", based on the outcome of processing a webhook event payload.
"""
alias CodeCorps.{GithubEvent, Repo}
alias Ecto.Changeset
@type error :: atom | Changeset.t
@doc ~S"""
Sets record status to "processing", marking it as being processed at this
moment. Our webhook handling should skip processing payloads for events which
are already being processed.
"""
@spec start_processing(GithubEvent.t) :: {:ok, GithubEvent.t}
def start_processing(%GithubEvent{} = event) do
event
|> Changeset.change(%{status: "processing"})
|> Repo.update()
end
@doc ~S"""
Sets record status to "processed" or "errored" based on the first element of
first argument, which is the result tuple. The first element of the result
tuple should always be either `:ok`, or `:error`. Any number of elements in
the tuple is suported.
"""
@spec stop_processing(tuple, GithubEvent.t) :: {:ok, GithubEvent.t}
def stop_processing({:ok, _data}, %GithubEvent{} = event) do
event
|> Changeset.change(%{status: "processed"})
|> Repo.update()
end
def stop_processing({:error, reason}, %GithubEvent{} = event) do
changes = %{status: "errored", failure_reason: reason |> Atom.to_string}
event
|> Changeset.change(changes)
|> Repo.update()
end
end