-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmonitor_trigger.erl
More file actions
186 lines (167 loc) · 7.14 KB
/
Copy pathmonitor_trigger.erl
File metadata and controls
186 lines (167 loc) · 7.14 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
%%%---------------------------------------------------------------------
%%% Description module monitor_trigger
%%%---------------------------------------------------------------------
%%% A monitor which can be used to trigger external scripts
%%%---------------------------------------------------------------------
%%% Exports
%%%---------------------------------------------------------------------
%%% start(Command)
%%% Initializes a new monitor which executes Command for each received
%%% dataset.
%%% start(Command, format)
%%% Initializes a new monitor which executes Command for each received
%%% dataset.
%%% The Command can use the modifier of io_lib:format/2, the given
%%% Data are Age, Fitness and Population, where Population is a list
%%% of {Pid, Fitness}
%%% start(Command, Divisor)
%%% Initializes a new monitor which executes Command for each received
%%% dataset, where the Age is divisible by Divisor.
%%% start(Command, Divisor, format)
%%% Initializes a new monitor which executes Command for each received
%%% dataset, where the Age is divisible by Divisor.
%%% The Command can use the modifier of io_lib:format/2, the given
%%% Data are Age, Fitness and Population, where Population is a list
%%% of {Pid, Fitness}
%%% state()
%%% Returns internal states
%%% stop()
%%% Stops the monitor
%%% init([Commans, Divisor])
%%% Interface for the behaviour gen_server.
%%% handle_call(state, From, State)
%%% Interface for the behaviour gen_server. Returns internal states
%%% handle_cast({population, Age, Population}, State)
%%% Interface for the behaviour gen_server. Collects data
%%% handle_cast(stop, State)
%%% Interface for the behaviour gen_server. Stops the monitor
%%% handle_info(Message, State)
%%% Interface for the behaviour gen_server. Dummy w/o functionality
%%% terminate(Reason, State)
%%% Interface for the behaviour gen_server. Kills the individual
%%% code_change(OldVersion, State, Extra)
%%% Interface for the behaviour gen_server. Dummy w/o functionality
%%%---------------------------------------------------------------------
-module(monitor_trigger).
-author('GEEK1 <erlang@geek1.de>').
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start/1, start/2, start/3, state/0, stop/0]).
-include("records.hrl").
%%----------------------------------------------------------------------
%% Function: start/1
%% Purpose: Calls gen_server:start_link/3 with the given command and the
%% default divisor
%% Args: Command, the command which should be executed
%% Returns: {ok,Pid} | ignore | {error,Error}
%%----------------------------------------------------------------------
start(Command) ->
gen_server:start_link({global, monitor}, ?MODULE, [Command, 1, false],
[]).
%%----------------------------------------------------------------------
%% Function: start/2
%% Purpose: Calls gen_server:start_link/3 with the given command and the
%% default divisor
%% Args: Command, the command which should be executed
%% Returns: {ok,Pid} | ignore | {error,Error}
%%----------------------------------------------------------------------
start(Command, format) ->
gen_server:start_link({global, monitor}, ?MODULE, [Command, 1, true],
[]);
%%----------------------------------------------------------------------
%% Function: start/2
%% Purpose: Calls gen_server:start_link/3 with the given command and the
%% given divisor
%% Args: Command, the command which should be executed
%% and
%% Returns: {ok,Pid} | ignore | {error,Error}
%%----------------------------------------------------------------------
start(Command, Divisor) when is_integer(Divisor) ->
gen_server:start_link({global, monitor}, ?MODULE, [Command, Divisor,
false], []).
%%----------------------------------------------------------------------
%% Function: start/3
%% Purpose: Calls gen_server:start_link/3 with the given command and the
%% given divisor
%% Args: Command, the command which should be executed
%% and
%% Returns: {ok,Pid} | ignore | {error,Error}
%%----------------------------------------------------------------------
start(Command, Divisor, format) when is_integer(Divisor) ->
gen_server:start_link({global, monitor}, ?MODULE, [Command, Divisor,
true], []).
%%----------------------------------------------------------------------
%% Function: state/0
%% Purpose: Returns internal state
%% Args: -
%% Returns: {state, State}
%%----------------------------------------------------------------------
state() -> gen_server:call({global, monitor}, state).
%%----------------------------------------------------------------------
%% Function: stop/0
%% Purpose: Stops the monitor
%% Args: -
%% Returns: ok.
%%----------------------------------------------------------------------
stop() -> gen_server:cast({global, monitor}, stop).
%%----------------------------------------------------------------------
%% Function: init/1
%% Purpose: Initializes the monitor with a given command and divisor
%% Args: Command, the command which should be executed
%% and Divisor, the divisor which should be used
%% and Format, a boolean to decide if io_lib:format/2 should be
%% called
%% Returns: {ok, State}.
%%----------------------------------------------------------------------
init([Command, Divisor, Format]) ->
State = #monitorState{command = Command, divisor = Divisor,
call_format = Format},
{ok, State}.
%%----------------------------------------------------------------------
%% Function: handle_call/3
%% Purpose: Returns internal state
%% Args: -
%% Returns: {reply, {state, State}, State}.
%%----------------------------------------------------------------------
handle_call(state, _From, State) -> {reply, {state, State}, State}.
%%----------------------------------------------------------------------
%% Function: handle_cast/2
%% Purpose: Triggers the Command
%% Args: -
%% Returns: {noreply, State}.
%%----------------------------------------------------------------------
handle_cast({population, Age, Population}, State) ->
FunMap = fun(Individual) ->
{_Pid, Fitness} = Individual,
Fitness
end,
if
Age rem State#monitorState.divisor == 0 ->
Fitness = lists:sum(lists:map(FunMap, Population)),
if
State#monitorState.call_format ->
Command = io_lib:format(State#monitorState.command, [Age, Fitness,
Population]);
true ->
Command = State#monitorState.command
end,
os:cmd(Command);
true ->
ok
end,
{noreply, State};
%%----------------------------------------------------------------------
%% Function: handle_cast/2
%% Purpose: Stops the monitor
%% Args: -
%% Returns: {stop, normal, State}.
%%----------------------------------------------------------------------
handle_cast(stop, State) -> {stop, normal, State}.
%%----------------------------------------------------------------------
%% Function: *
%% Purpose: Dummy functions for the behaviour gen_server
%%----------------------------------------------------------------------
terminate(normal, _State) -> ok;
terminate(_Reason, _State) -> ok.
handle_info(_Message, State) -> {noreply, State}.
code_change(_OldVersion, State, _Extra) -> {ok, State}.