-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpy-sup.lfe
89 lines (70 loc) · 2.11 KB
/
py-sup.lfe
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(defmodule py-sup
(behaviour supervisor)
(export all))
(defun start_link ()
(supervisor:start_link
`#(local ,(MODULE))
(MODULE)
'()))
(defun init (_)
`#(ok #(,(get-supervisor-spec)
,(get-children-specs))))
(defun get-supervisor-spec ()
`#(one_for_one ,(py-config:get-max-restarts)
,(py-config:get-restart-threshold)))
(defun get-children-specs ()
(lists:map #'get-child-spec/1 (py-util:get-worker-names)))
(defun get-child-spec ()
(get-child-spec 'py))
(defun get-child-spec (child-id)
`#(,child-id #(py start_link (,child-id))
permanent
,(py-config:get-shutdown-timeout)
worker
(py)))
(defun get-pid ()
(let ((pid (erlang:whereis (MODULE))))
(sys:statistics pid 'true)
pid))
(defun add-server (child-id)
(add-server (get-pid) child-id))
(defun add-server (sup-pid child-id)
(supervisor:start_child sup-pid (get-child-spec child-id)))
(defun get-status ()
;; We want to get statisitics here, too -- so use (get-pid) which checks
;; to make sure that the supervisor process has statisics enabled
(sys:get_status (get-pid)))
(defun get-state ()
(sys:get_state (MODULE)))
(defun get-children ()
(lists:map
(match-lambda ((`#(,name ,pid ,_ ,_))
`#(,pid ,name)))
(supervisor:which_children (MODULE))))
(defun get-child-pids ()
(lists:map
(lambda (x)
(element 2 x))
(get-children)))
(defun get-stats ()
(let ((`#(ok ,stats) (sys:statistics (get-pid) 'get)))
(++ `(#(,(MODULE) ,stats))
(get-child-stats))))
(defun get-child-stats ()
(lists:map
(match-lambda ((`#(,_ ,name))
(sys:statistics name 'true)
(let ((`#(ok ,stats) (sys:statistics name 'get)))
`#(,name ,stats))))
(get-children)))
(defun get-child-stats (key)
(lists:map
(match-lambda ((`#(,name ,stats))
`#(,(proplists:get_value key stats) ,name)))
(get-child-stats)))
(defun get-child-reductions ()
(get-child-stats 'reductions))
(defun get-child-messages-in ()
(get-child-stats 'messages_in))
(defun get-child-start-time ()
(get-child-stats 'start_time))