-
Notifications
You must be signed in to change notification settings - Fork 141
/
ecrn_app.erl
73 lines (63 loc) · 1.95 KB
/
ecrn_app.erl
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
%%% @copyright Erlware, LLC. All Rights Reserved.
%%%
%%% This file is provided to you under the BSD License; you may not use
%%% this file except in compliance with the License.
%%%----------------------------------------------------------------
%%% @doc
%%% erlcron app system
-module(ecrn_app).
-behaviour(application).
%% API
-export([manual_start/0, manual_stop/0]).
%% Application callbacks
-export([start/2, stop/1]).
%%%===================================================================
%%% API
%%%===================================================================
%% @doc
%% start up the app and all the dependent apps.
manual_start() ->
%application:start(crypto),
application:start(eunit),
%application:start(sasl),
application:start(erlcron).
%% @doc
%% stop the app manually
manual_stop() ->
application:stop(erlcron).
%%%===================================================================
%%% Application callbacks
%%%===================================================================
%% @private
start(_StartType, _StartArgs) ->
case ecrn_sup:start_link() of
{ok, Pid} ->
setup(),
{ok, Pid};
Error ->
Error
end.
%% @private
stop(_State) ->
ok.
setup() ->
case application:get_env(erlcron, crontab) of
{ok, Crontab} ->
Def = application:get_env(erlcron, defaults, #{}),
is_map(Def) orelse
erlang:error("erlcron/defaults config must be a map!"),
lists:foreach(fun(CronJob) ->
case erlcron:cron(CronJob, Def) of
ok ->
ok;
already_started ->
ok;
ignored ->
ok;
{error, Reason} ->
erlang:error({failed_to_add_cron_job, CronJob, Reason})
end
end, Crontab);
undefined ->
ok
end.