capricorn 0.2.25 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. data/LICENSE +19 -0
  2. data/README.md +1 -0
  3. data/bin/capricorn-app-scaffolder +27 -0
  4. data/bin/capricorn-gem-spec +33 -0
  5. data/bin/capricornctl +12 -0
  6. data/bin/capricornd +31 -0
  7. data/erlang/lib/bert/doc/overview.edoc +4 -0
  8. data/erlang/lib/bert/ebin/bert.app +9 -0
  9. data/erlang/lib/bert/rebar.config +3 -0
  10. data/erlang/lib/bert/src/bert.erl +69 -0
  11. data/erlang/lib/bertio/doc/overview.edoc +4 -0
  12. data/erlang/lib/bertio/ebin/bertio.app +9 -0
  13. data/erlang/lib/bertio/rebar.config +3 -0
  14. data/erlang/lib/bertio/src/bertio.erl +28 -0
  15. data/erlang/lib/bertrpc/doc/overview.edoc +4 -0
  16. data/erlang/lib/bertrpc/ebin/bertrpc.app +12 -0
  17. data/erlang/lib/bertrpc/include/bertrpc.hrl +1 -0
  18. data/erlang/lib/bertrpc/rebar.config +5 -0
  19. data/erlang/lib/bertrpc/src/bertrpc.erl +471 -0
  20. data/erlang/lib/bertrpc/src/bertrpc_hello_world.erl +148 -0
  21. data/erlang/lib/bertrpc/src/fd_tcp.erl +376 -0
  22. data/erlang/lib/capricorn/doc/overview.edoc +4 -0
  23. data/erlang/lib/capricorn/ebin/capricorn.app +56 -0
  24. data/erlang/lib/capricorn/include/capricorn.hrl +76 -0
  25. data/erlang/lib/capricorn/rebar.config +3 -0
  26. data/erlang/lib/capricorn/src/cap_application.erl +170 -0
  27. data/erlang/lib/capricorn/src/cap_cluster.erl +121 -0
  28. data/erlang/lib/capricorn/src/cap_cluster_gems.erl +422 -0
  29. data/erlang/lib/capricorn/src/cap_config.erl +25 -0
  30. data/erlang/lib/capricorn/src/cap_dets_updater.erl +26 -0
  31. data/erlang/lib/capricorn/src/cap_event_sup.erl +35 -0
  32. data/erlang/lib/capricorn/src/cap_events.erl +24 -0
  33. data/erlang/lib/capricorn/src/cap_external_api.erl +87 -0
  34. data/erlang/lib/capricorn/src/cap_external_apps_api.erl +125 -0
  35. data/erlang/lib/capricorn/src/cap_external_gems_api.erl +85 -0
  36. data/erlang/lib/capricorn/src/cap_external_machines_api.erl +18 -0
  37. data/erlang/lib/capricorn/src/cap_gem_utils.erl +111 -0
  38. data/erlang/lib/capricorn/src/cap_internal_api.erl +69 -0
  39. data/erlang/lib/capricorn/src/cap_internal_apps_api.erl +38 -0
  40. data/erlang/lib/capricorn/src/cap_log.erl +113 -0
  41. data/erlang/lib/capricorn/src/cap_machine.erl +338 -0
  42. data/erlang/lib/capricorn/src/cap_machine_apps.erl +410 -0
  43. data/erlang/lib/capricorn/src/cap_machine_apps_sup.erl +57 -0
  44. data/erlang/lib/capricorn/src/cap_sup.erl +200 -0
  45. data/erlang/lib/capricorn/src/cap_util.erl +278 -0
  46. data/erlang/lib/capricorn/src/capricorn.erl +45 -0
  47. data/erlang/lib/capricorn/src/capricorn_app.erl +36 -0
  48. data/erlang/lib/emq/ebin/emq.app +17 -0
  49. data/erlang/lib/emq/src/emq.erl +261 -0
  50. data/erlang/lib/emq/src/emq_app.erl +11 -0
  51. data/erlang/lib/emq/src/emq_pool.erl +178 -0
  52. data/erlang/lib/emq/src/emq_queue.erl +332 -0
  53. data/erlang/lib/emq/src/emq_status.erl +148 -0
  54. data/erlang/lib/emq/src/emq_sup.erl +132 -0
  55. data/erlang/lib/gcd/ebin/gcd.app +18 -0
  56. data/erlang/lib/gcd/include/gcd.hrl +5 -0
  57. data/erlang/lib/gcd/rebar.config +3 -0
  58. data/erlang/lib/gcd/src/gcd.erl +51 -0
  59. data/erlang/lib/gcd/src/gcd_app.erl +13 -0
  60. data/erlang/lib/gcd/src/gcd_event.erl +39 -0
  61. data/erlang/lib/gcd/src/gcd_server.erl +30 -0
  62. data/erlang/lib/gcd/src/gcd_srv.erl +186 -0
  63. data/erlang/lib/gcd/src/gcd_sup.erl +18 -0
  64. data/erlang/rebar +0 -0
  65. data/erlang/rebar.config +9 -0
  66. data/erlang/rel/overlay/bin/capricornd +146 -0
  67. data/erlang/rel/overlay/erts-vsn/bin/erl +34 -0
  68. data/erlang/rel/overlay/erts-vsn/bin/nodetool +80 -0
  69. data/erlang/rel/overlay/etc/capricorn/app.config +59 -0
  70. data/erlang/rel/overlay/etc/capricorn/cluster-vm.args +21 -0
  71. data/erlang/rel/overlay/etc/capricorn/machine-vm.args +21 -0
  72. data/erlang/rel/reltool.config +43 -0
  73. data/ext/Makefile +2 -0
  74. data/ext/extconf.rb +1 -0
  75. data/lib/capricorn-client.rb +86 -0
  76. data/lib/capricorn-client/cli/applications.rb +256 -0
  77. data/lib/capricorn-client/cli/gems.rb +57 -0
  78. data/lib/capricorn-client/cli/machines.rb +9 -0
  79. data/lib/capricorn-client/helpers.rb +62 -0
  80. data/lib/capricorn.rb +5 -99
  81. data/lib/capricorn/driver.rb +86 -0
  82. data/lib/capricorn/recipes/apache-debian.rb +112 -0
  83. data/lib/capricorn/recipes/centos-plesk.rb +162 -0
  84. data/lib/capricorn/recipes/macports.rb +54 -0
  85. data/lib/capricorn/system_context.rb +49 -0
  86. data/lib/capricorn/version.rb +5 -0
  87. metadata +233 -74
  88. data/app_generators/engine/engine_generator.rb +0 -40
  89. data/app_generators/engine/templates/Gmfile +0 -20
  90. data/app_generators/engine/templates/MIT-LICENSE.txt +0 -20
  91. data/app_generators/engine/templates/README.rdoc +0 -7
  92. data/app_generators/engine/templates/config/initializers/rails_init.rb +0 -1
  93. data/app_generators/engine/templates/config/routes.rb +0 -2
  94. data/app_generators/engine/templates/gitignore +0 -9
  95. data/app_generators/engine/templates/init.rb +0 -1
  96. data/app_generators/engine/templates/lib/engine.rb +0 -4
  97. data/app_generators/engine/templates/rails/init.rb +0 -1
  98. data/app_generators/engine/templates/tasks/engine_tasks.rake +0 -4
  99. data/bin/capricorn +0 -20
  100. data/lib/capricorn/actor.rb +0 -23
  101. data/lib/capricorn/actor/actions.rb +0 -76
  102. data/lib/capricorn/actors/apache_actor.rb +0 -56
  103. data/lib/capricorn/actors/base_actor.rb +0 -335
  104. data/lib/capricorn/actors/host_file_actor.rb +0 -77
  105. data/lib/capricorn/actors/mysql_actor.rb +0 -20
  106. data/lib/capricorn/actors/passenger_actor.rb +0 -28
  107. data/lib/capricorn/actors/plesk_actor.rb +0 -228
  108. data/lib/capricorn/actors/sqlite3_actor.rb +0 -44
  109. data/lib/capricorn/app_runner.rb +0 -108
  110. data/lib/capricorn/apps/dev.rb +0 -15
  111. data/lib/capricorn/apps/engines.rb +0 -33
  112. data/lib/capricorn/apps/jobs.rb +0 -35
  113. data/lib/capricorn/apps/satellite.rb +0 -68
  114. data/lib/capricorn/apps/server.rb +0 -73
  115. data/lib/capricorn/client.rb +0 -48
  116. data/lib/capricorn/client/auth_token.rb +0 -98
  117. data/lib/capricorn/daemon.rb +0 -81
  118. data/lib/capricorn/exception_handler.rb +0 -79
  119. data/lib/capricorn/extentions/rubygems_plugin.rb +0 -27
  120. data/lib/capricorn/extentions/thor_extentions.rb +0 -32
  121. data/lib/capricorn/job_queue.rb +0 -203
  122. data/lib/capricorn/satellite.rb +0 -52
  123. data/lib/capricorn/satellite/actions.rb +0 -55
  124. data/lib/capricorn/satellite/dependency_loader.rb +0 -82
  125. data/lib/capricorn/satellite/persistence.rb +0 -50
  126. data/lib/capricorn/server.rb +0 -144
  127. data/lib/capricorn/server/daemon.rb +0 -83
  128. data/lib/capricorn/server/proxy.rb +0 -25
  129. data/lib/capricorn/server/security.rb +0 -120
  130. data/lib/capricorn/system.rb +0 -218
  131. data/lib/capricorn/system/config.rb +0 -49
  132. data/lib/capricorn/system/helper.rb +0 -21
  133. data/lib/capricorn/system/options.rb +0 -79
  134. data/lib/capricorn/system/process_user.rb +0 -73
  135. data/lib/capricorn/system/satellites.rb +0 -44
  136. data/lib/capricorn/system/shell.rb +0 -80
  137. data/lib/rubygems_plugin.rb +0 -1
  138. data/spec/actor/actions_spec.rb +0 -13
  139. data/spec/spec_helper.rb +0 -1
@@ -0,0 +1,332 @@
1
+ -module(emq_queue).
2
+ -behaviour(gen_server).
3
+
4
+
5
+
6
+ -export([size/1, push/6, reserve/1, commit/2, rollback/2]).
7
+
8
+ -export([start_link/1]).
9
+ -export([init/1, handle_call/3, handle_cast/2,
10
+ handle_info/2, terminate/2, code_change/3]).
11
+
12
+
13
+
14
+ -record(state, {
15
+ name :: atom(),
16
+ jobs :: pid(),
17
+ queue :: queue(),
18
+ reserved :: [term()]
19
+ }).
20
+ -type state() :: #state{} .
21
+
22
+
23
+
24
+ %%% External API
25
+ size(Pid) ->
26
+ gen_server:call(Pid, {size}).
27
+
28
+
29
+
30
+ push(Pid, Id, Dependencies, Dependents, Callbacks, Job) ->
31
+ gen_server:cast(Pid, {push, Id, Dependencies, Dependents, Callbacks, Job}).
32
+
33
+
34
+
35
+ reserve(Pid) ->
36
+ gen_server:call(Pid, {reserve}).
37
+
38
+
39
+
40
+ commit(Pid, Id) ->
41
+ gen_server:cast(Pid, {commit, Id}).
42
+
43
+
44
+
45
+ rollback(Pid, Id) ->
46
+ gen_server:cast(Pid, {rollback, Id}).
47
+
48
+
49
+
50
+ %%% Start the server
51
+ -spec start_link(atom()) -> {ok, pid()} .
52
+ start_link(Name) ->
53
+ gen_server:start_link(?MODULE, {Name}, []).
54
+
55
+
56
+
57
+ %%% Initialize the server
58
+ -spec init({atom()}) -> {ok, state()} .
59
+ init({Name}) ->
60
+ Jobs = ets:new(queue_jobs, [set, private]),
61
+ {ok, #state{ jobs=Jobs, queue=queue:new(), name = Name, reserved = [] }}.
62
+
63
+
64
+
65
+ %%% Handle call messages
66
+ handle_call({setup_process, Name, Emq, Queue, Pool, Status}, _From, State) ->
67
+ erlang:put('$emq_name', Name),
68
+ erlang:put('$emq', Emq),
69
+ erlang:put('$emq_pool', Pool),
70
+ erlang:put('$emq_queue', Queue),
71
+ erlang:put('$emq_status', Status),
72
+ {reply, ok, State};
73
+
74
+ handle_call({size}, _From, State) ->
75
+ #state { jobs=Jobs } = State,
76
+ {reply, ets:info(Jobs, size), State};
77
+
78
+ handle_call({reserve}, From, State) ->
79
+ #state { jobs = Jobs, queue = Queue1, reserved = Reserved } = State,
80
+ case queue:out(Queue1) of
81
+ {{value, Id}, Queue2} ->
82
+ State2 = State#state { queue = Queue2 },
83
+ case ets:lookup(Jobs, Id) of
84
+ [] ->
85
+ handle_call({reserve}, From, State2);
86
+ [{Id, _Dependencies, _Dependents, Callbacks, Job}] ->
87
+ State3 = State2#state { reserved = [Id|Reserved] },
88
+ {reply, {value, Id, Job, Callbacks}, State3}
89
+ end;
90
+ {empty, _Queue} ->
91
+ {reply, {empty}, State}
92
+ end.
93
+
94
+
95
+
96
+ %%% Handle cast messages;
97
+ handle_cast({commit, Id}, State1) ->
98
+ #state { jobs=Jobs, reserved = Reserved1, name = Name } = State1,
99
+ Reserved2 = lists:delete(Id, Reserved1),
100
+ State2 = State1#state { reserved = Reserved2 },
101
+ case ets:lookup(Jobs, Id) of
102
+ [] ->
103
+ {noreply, State2};
104
+ [{Id, _, Dependents, _, _}] ->
105
+ ets:delete(Jobs, Id),
106
+
107
+ State3 = lists:foldl(fun
108
+ ({QueueName, DependentId}, OldState)->
109
+ case emq_sup:get_queue(QueueName) of
110
+ {ok, QueuePid} when QueuePid == self() ->
111
+ {noreply, NewState} =
112
+ handle_info({remove_dependency, DependentId, {Name, Id}}, OldState),
113
+ NewState;
114
+ {ok, QueuePid} ->
115
+ QueuePid ! {remove_dependency, DependentId, {Name, Id}},
116
+ OldState;
117
+ {error, not_found} ->
118
+ OldState
119
+ end
120
+ end, State2, Dependents),
121
+
122
+ {noreply, State3}
123
+ end;
124
+
125
+ handle_cast({rollback, Id}, State1) ->
126
+ #state { queue = Queue1, reserved = Reserved1 } = State1,
127
+ Reserved2 = lists:delete(Id, Reserved1),
128
+ Queue2 = queue:in(Id, Queue1),
129
+ State2 = State1#state { queue = Queue2, reserved = Reserved2 },
130
+ {noreply, State2};
131
+
132
+ handle_cast({push, Id1, Dependencies1, Dependents1, Callbacks1, Job1}, State1) ->
133
+ #state { jobs = Jobs } = State1,
134
+ case ets:lookup(Jobs, Id1) of
135
+ [] ->
136
+ handle_cast({push_new, Id1, Dependencies1, Dependents1, Callbacks1, Job1}, State1);
137
+ [{Id1, Dependencies2, Dependents2, Callbacks2, Job2}] ->
138
+ handle_cast({push_update, {Id1, Dependencies2, Dependents2, Callbacks2, Job2}, {Dependencies1, Dependents1, Callbacks1}}, State1)
139
+ end;
140
+
141
+ handle_cast({push_update, {Id, Dependencies, Dependents, Callbacks, Job}, {NewDependencies, NewDependents, NewCallbacks}}, State1) ->
142
+ #state { jobs = Jobs, name = Name } = State1,
143
+
144
+ CombinedDependencies = Dependencies ++ NewDependencies,
145
+ CombinedDependents = Dependents ++ NewDependents,
146
+ CombinedCallbacks = Callbacks ++ NewCallbacks,
147
+
148
+ ets:insert(Jobs, {Id, CombinedDependencies, CombinedDependents, CombinedCallbacks, Job}),
149
+
150
+ State2 = lists:foldl(fun
151
+ ({QueueName, DependentId}, OldState)->
152
+ case emq_sup:get_queue(QueueName) of
153
+ {ok, QueuePid} when QueuePid == self() ->
154
+ {noreply, NewState} =
155
+ handle_info({new_dependency, DependentId, {Name, Id}}, OldState),
156
+ NewState;
157
+ {ok, QueuePid} ->
158
+ QueuePid ! {new_dependency, DependentId, {Name, Id}},
159
+ OldState;
160
+ {error, not_found} ->
161
+ OldState
162
+ end
163
+ end, State1, NewDependents),
164
+
165
+ State3 = lists:foldl(fun
166
+ ({QueueName, DependencyId}, OldState)->
167
+ case emq_sup:get_queue(QueueName) of
168
+ {ok, QueuePid} when QueuePid == self() ->
169
+ {noreply, NewState} =
170
+ handle_info({new_dependent, DependencyId, {Name, Id}}, OldState),
171
+ NewState;
172
+ {ok, QueuePid} ->
173
+ QueuePid ! {new_dependent, DependencyId, {Name, Id}},
174
+ OldState;
175
+ {error, not_found} ->
176
+ OldState
177
+ end
178
+ end, State2, NewDependencies),
179
+
180
+ #state { queue = Queue1, reserved = Reserved } = State3,
181
+
182
+ Queue2 = queue:filter(fun
183
+ (QueuedId) when QueuedId == Id -> false;
184
+ (_) -> true
185
+ end, Queue1),
186
+
187
+ {ok, StatusPid} = emq_sup:get_status(local),
188
+
189
+ case CombinedDependencies of
190
+ [] ->
191
+ case lists:member(Id, Reserved) of
192
+ true ->
193
+ {noreply, State3#state { queue = Queue2 }};
194
+ false ->
195
+ Queue3 = queue:in(Id, Queue2),
196
+ emq_status:put(StatusPid, Id, queued),
197
+ {noreply, State3#state { queue = Queue3 }}
198
+ end;
199
+ _Else ->
200
+ emq_status:put(StatusPid, Id, pending),
201
+ {noreply, State3#state { queue = Queue2 }}
202
+ end;
203
+
204
+ handle_cast({push_new, Id, Dependencies, Dependents, Callbacks, Job}, State1) ->
205
+ #state { jobs = Jobs, name = Name } = State1,
206
+
207
+ ets:insert(Jobs, {Id, Dependencies, Dependents, Callbacks, Job}),
208
+
209
+ State2 = lists:foldl(fun
210
+ ({QueueName, DependentId}, OldState)->
211
+ case emq_sup:get_queue(QueueName) of
212
+ {ok, QueuePid} when QueuePid == self() ->
213
+ {noreply, NewState} =
214
+ handle_info({new_dependency, DependentId, {Name, Id}}, OldState),
215
+ NewState;
216
+ {ok, QueuePid} ->
217
+ QueuePid ! {new_dependency, DependentId, {Name, Id}},
218
+ OldState;
219
+ {error, not_found} ->
220
+ OldState
221
+ end
222
+ end, State1, Dependents),
223
+
224
+
225
+ State3 = lists:foldl(fun
226
+ ({QueueName, DependencyId}, OldState)->
227
+ case emq_sup:get_queue(QueueName) of
228
+ {ok, QueuePid} when QueuePid == self() ->
229
+ {noreply, NewState} =
230
+ handle_info({new_dependent, DependencyId, {Name, Id}}, OldState),
231
+ NewState;
232
+ {ok, QueuePid} ->
233
+ QueuePid ! {new_dependent, DependencyId, {Name, Id}},
234
+ OldState;
235
+ {error, not_found} ->
236
+ OldState
237
+ end
238
+ end, State2, Dependencies),
239
+
240
+
241
+ {ok, StatusPid} = emq_sup:get_status(local),
242
+
243
+
244
+ case Dependencies of
245
+ [] ->
246
+ #state { queue = Queue1 } = State3,
247
+ Queue2 = queue:in(Id, Queue1),
248
+ emq_status:put(StatusPid, Id, queued),
249
+ {noreply, State3#state { queue = Queue2 }};
250
+ _Else ->
251
+ emq_status:put(StatusPid, Id, pending),
252
+ {noreply, State3}
253
+ end.
254
+
255
+
256
+
257
+ %%% Handle generic messages
258
+ handle_info({new_dependent, DependencyId, {Queue, Id}}, State) ->
259
+ #state { jobs = Jobs, name = Name } = State,
260
+ case ets:lookup(Jobs, DependencyId) of
261
+ [] ->
262
+ case emq_sup:get_queue(Queue) of
263
+ {ok, QueuePid} when QueuePid == self() ->
264
+ handle_info({remove_dependency, Id, {Name, DependencyId}}, State);
265
+ {ok, QueuePid} ->
266
+ QueuePid ! {remove_dependency, Id, {Name, DependencyId}},
267
+ {noreply, State};
268
+ {error, not_found} ->
269
+ {noreply, State}
270
+ end;
271
+ [{DependencyId, Dependencies, Dependents, Callbacks, Job}] ->
272
+ ets:insert(Jobs, {DependencyId, Dependencies, [{Queue, Id}|Dependents], Callbacks, Job}),
273
+ {noreply, State}
274
+ end;
275
+
276
+ handle_info({new_dependency, DependentId, {Queue, Id}}, State) ->
277
+ #state { jobs = Jobs, queue = Queue1 } = State,
278
+ case ets:lookup(Jobs, DependentId) of
279
+ [] ->
280
+ {noreply, State};
281
+ [{DependentId, Dependencies, Dependents, Callbacks, Job}] ->
282
+ ets:insert(Jobs, {DependentId, [{Queue, Id}|Dependencies], Dependents, Callbacks, Job}),
283
+ Queue2 = queue:filter(fun
284
+ (QueuedId) when QueuedId == DependentId -> false;
285
+ (_) -> true
286
+ end, Queue1),
287
+ {ok, StatusPid} = emq_sup:get_status(local),
288
+ emq_status:put(StatusPid, DependentId, pending),
289
+ {noreply, State#state{ queue = Queue2 }}
290
+ end;
291
+
292
+ handle_info({remove_dependency, DependentId, {Queue, DependencyId}}, State) ->
293
+ #state { jobs = Jobs, queue = Queue1 } = State,
294
+
295
+ case ets:lookup(Jobs, DependentId) of
296
+ [] ->
297
+ {noreply, State};
298
+ [{Id, Dependencies1, Dependents, Callbacks, Job}] ->
299
+ Dependencies2 = lists:delete({Queue, DependencyId}, Dependencies1),
300
+ ets:insert(Jobs, {Id, Dependencies2, Dependents, Callbacks, Job}),
301
+
302
+ case Dependencies2 of
303
+ [] ->
304
+ Queue2 = queue:in(Id, Queue1),
305
+ {ok, StatusPid} = emq_sup:get_status(local),
306
+ emq_status:put(StatusPid, DependentId, queued),
307
+
308
+ {ok, EmqPid} = emq_sup:get_emq(local),
309
+ EmqPid ! kick
310
+ ,
311
+ {noreply, State#state{ queue = Queue2 }};
312
+ _Else ->
313
+ {noreply, State}
314
+ end
315
+ end.
316
+
317
+
318
+
319
+ %%% Before stopping the server
320
+ terminate(_Reason, State) ->
321
+ #state { jobs = Jobs } = State,
322
+ ets:delete(Jobs),
323
+ ok.
324
+
325
+
326
+ %%% Code Changes
327
+ code_change(_OldVsn, State, _Extra) ->
328
+ {ok, State}.
329
+
330
+
331
+
332
+ %%% Internal API
@@ -0,0 +1,148 @@
1
+ -module(emq_status).
2
+ -behaviour(gen_server).
3
+
4
+
5
+
6
+ -export([put/3, get/2, get_all/1, delete/2]).
7
+
8
+ -export([start_link/1]).
9
+ -export([init/1, handle_call/3, handle_cast/2,
10
+ handle_info/2, terminate/2, code_change/3]).
11
+
12
+
13
+
14
+ -record(state, {
15
+ name :: atom(),
16
+ jobs :: pid()
17
+ }).
18
+ -type state() :: #state{} .
19
+
20
+
21
+
22
+ %%% External API
23
+ put(Pid, Id, Status) when is_atom(Status) ->
24
+ put(Pid, Id, {Status, undefined});
25
+
26
+ put(Pid, Id, Message) when is_list(Message) ->
27
+ put(Pid, Id, {undefined, Message});
28
+
29
+ put(Pid, Id, {Status, Message}) ->
30
+ gen_server:cast(Pid, {put, {Id, Status, Message}}).
31
+
32
+
33
+
34
+ get(Pid, Id) ->
35
+ gen_server:call(Pid, {get, Id}).
36
+
37
+
38
+
39
+ get_all(Pid) ->
40
+ gen_server:call(Pid, {get_all}).
41
+
42
+
43
+
44
+ delete(Pid, Id) ->
45
+ gen_server:cast(Pid, {delete, Id}).
46
+
47
+
48
+
49
+ %%% Start the server
50
+ -spec start_link(atom()) -> {ok, pid()} .
51
+ start_link(Name) ->
52
+ gen_server:start_link(?MODULE, {Name}, []).
53
+
54
+
55
+
56
+ %%% Initialize the server
57
+ -spec init({atom()}) -> {ok, state()} .
58
+ init({Name}) ->
59
+ Jobs = ets:new(jobs, [set, private]),
60
+ {ok, #state{ jobs=Jobs, name=Name }}.
61
+
62
+
63
+
64
+ %%% Handle call messages
65
+ handle_call({setup_process, Name, Emq, Queue, Pool, Status}, _From, State) ->
66
+ erlang:put('$emq_name', Name),
67
+ erlang:put('$emq', Emq),
68
+ erlang:put('$emq_pool', Pool),
69
+ erlang:put('$emq_queue', Queue),
70
+ erlang:put('$emq_status', Status),
71
+ {reply, ok, State};
72
+
73
+ handle_call({get, Id}, _From, State) ->
74
+ #state { jobs = Jobs } = State,
75
+ case ets:lookup(Jobs, Id) of
76
+ [] ->
77
+ {reply, {error, not_found}, State};
78
+ [{Id, Status, Message}] ->
79
+ {reply, {ok, {Status, Message}}, State}
80
+ end;
81
+
82
+ handle_call({get_all}, _From, State) ->
83
+ #state { jobs = Jobs } = State,
84
+ {reply, {ok, ets:tab2list(Jobs)}, State}.
85
+
86
+
87
+
88
+ %%% Handle cast messages
89
+ handle_cast({put, {Id, Status, Message}}, State) ->
90
+ #state { jobs = Jobs } = State,
91
+ Record =
92
+ case ets:lookup(Jobs, Id) of
93
+ [] ->
94
+ if
95
+ (Status == undefined) and (Message == undefined) ->
96
+ {Id, pending, "pending"};
97
+ (Status == undefined) ->
98
+ {Id, pending, Message};
99
+ (Message == undefined) ->
100
+ {Id, Status, "pending"};
101
+ true ->
102
+ {Id, Status, Message}
103
+ end;
104
+ [{Id, OldStatus, OldMessage}] ->
105
+ if
106
+ (Status == undefined) and (Message == undefined) ->
107
+ {Id, OldStatus, OldMessage};
108
+ (Status == undefined) ->
109
+ {Id, OldStatus, Message};
110
+ (Message == undefined) ->
111
+ {Id, Status, OldMessage};
112
+ true ->
113
+ {Id, Status, Message}
114
+ end
115
+ end,
116
+ ets:insert(Jobs, Record),
117
+ {noreply, State};
118
+
119
+ handle_cast({delete, Id}, State) ->
120
+ #state { jobs = Jobs } = State,
121
+ ets:delete(Jobs, Id),
122
+ {noreply, State}.
123
+
124
+
125
+
126
+ %%% Handle generic messages
127
+ handle_info(raise_error_on_unknow_message, State) ->
128
+ {noreply, State}.
129
+
130
+
131
+
132
+ %%% Before stopping the server
133
+ terminate(_Reason, State) ->
134
+ #state { jobs = Jobs } = State,
135
+ ets:delete(Jobs),
136
+ ok.
137
+
138
+
139
+
140
+ %%% Code Changes
141
+ code_change(_OldVsn, State, _Extra) ->
142
+ {ok, State}.
143
+
144
+
145
+
146
+ %%% Internal API
147
+
148
+