-
Notifications
You must be signed in to change notification settings - Fork 1
/
workflow_controller.ex
65 lines (55 loc) · 2.48 KB
/
workflow_controller.ex
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
defmodule Designator.WorkflowController do
use Designator.Web, :controller
plug BasicAuth, [callback: &Designator.Controllers.Helpers.authenticate/3] when action in [:reload, :unlock, :remove]
def show(conn, %{"id" => workflow_id} = params) do
{workflow_id, _} = Integer.parse(workflow_id)
user_id = get_integer_param(params, "user_id", nil)
subject_set_id = get_integer_param(params, "subject_set_id", nil)
limit = get_integer_param(params, "limit", 5)
subjects = Designator.Selection.select(
workflow_id,
user_id,
[ subject_set_id: subject_set_id, limit: limit ]
)
Logger.metadata([selected_ids: Enum.join(subjects, ",")])
render conn, "show.json", subjects: subjects
end
def reload(conn, %{"id" => workflow_id}) do
{workflow_id, _} = Integer.parse(workflow_id)
do_full_reload(workflow_id)
send_resp(conn, 204, [])
end
def unlock(conn, %{"id" => workflow_id}) do
{workflow_id, _} = Integer.parse(workflow_id)
Designator.WorkflowCache.get(workflow_id).subject_set_ids
|> Enum.each(fn subject_set_id -> Designator.SubjectSetCache.unlock({workflow_id, subject_set_id}) end)
send_resp(conn, 204, [])
end
# subject_id can be a URL query param (byte sequence)
def remove(conn, %{"id" => workflow_id, "subject_id" => subject_id}) when is_binary(subject_id) do
{workflow_id, _} = Integer.parse(workflow_id)
{subject_id, _} = Integer.parse(subject_id)
do_remove(conn, workflow_id, subject_id)
end
# subject_id can be come from POST JSON payload
def remove(conn, %{"id" => workflow_id, "subject_id" => subject_id}) when is_integer(subject_id) do
{workflow_id, _} = Integer.parse(workflow_id)
do_remove(conn, workflow_id, subject_id)
end
defp do_remove(conn, workflow_id, subject_id) do
Designator.RecentlyRetired.add(workflow_id, subject_id)
# Refresh the current known state from the source datastore
# to ensure the system is operating efficiently on updated lists of
# available subject_ids and retired subject_ids
if MapSet.size(Designator.RecentlyRetired.get(workflow_id).subject_ids) > 50 do
do_full_reload(workflow_id)
end
send_resp(conn, 204, [])
end
defp do_full_reload(workflow_id) do
Designator.RecentlyRetired.clear(workflow_id)
Designator.WorkflowCache.reload(workflow_id)
Designator.WorkflowCache.get(workflow_id).subject_set_ids
|> Enum.each(fn subject_set_id -> Designator.SubjectSetCache.reload({workflow_id, subject_set_id}) end)
end
end