Erlcron 源碼分析(2)

上一篇介紹了如何使用,這次看看具體實現

github將項目拉到本地,看下項目具體的結構


可以看到項目是rebar3管理,遵循opt原則,集成travis ci 做集成測試。


項目啟動

  1. ecrn_app.erl 運行start/2
start(_StartType, _StartArgs) ->
    case ecrn_sup:start_link() of
        {ok, Pid} ->
            setup(),
            {ok, Pid};
        Error ->
            Error
    end.
  1. ecrn_sup.erl 執行start_link/0
start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================

%% @private
init([]) ->
    RestartStrategy = one_for_one,
    MaxRestarts = 3,
    MaxSecondsBetweenRestarts = 10,
    SupFlags = {RestartStrategy,
                MaxRestarts,
                MaxSecondsBetweenRestarts},

    ChildSup =  {ecrn_cron_sup, {ecrn_cron_sup, start_link, []},
                 permanent, 1000, supervisor, [ecrn_cron_sup]},
    RegistrationServer  =  {ecrn_reg_server, {ecrn_reg, start_link, []},
                            permanent, 1000, worker, [ecrn_reg]},
    BroadcastServer  =  {ecrn_control, {ecrn_control, start_link, []},
                         permanent, 1000, worker, [ecrn_control]},


    {ok, {SupFlags, [ChildSup, RegistrationServer, BroadcastServer]}}.

查看源碼可以看出ecrn_app:start_link() 分別啟動了三個子進程分別是
ecrn_cron_sup、ecrn_reg_server、ecrn_control
3.三個啟動模塊

  • 我們先看ecrn_cron_sup啟動都做了些什么
start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).
init([]) ->
    RestartStrategy = simple_one_for_one,
    MaxRestarts = 1000,
    MaxSecondsBetweenRestarts = 3600,

    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},

    Restart = transient,
    Shutdown = 2000,
    Type = worker,

    AChild = {ecrn_agent, {ecrn_agent, start_link, []},
              Restart, Shutdown, Type, [ecrn_agent]},

    {ok, {SupFlags, [AChild]}}.

又去啟動了ecrn_agent,真是一層套一層,不過這也遵循了otp原則,這里可以看到啟動策略使用了simple_one_for_one,需要時創建子進程,初步考慮應當就是任務執行的最終模塊了

  • 那接著我們再去看第二個啟動的ecrn_reg
-behaviour(gen_server).

start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

init([]) ->
    {ok, #state{registered=dict:new()}}.

一個gen_server模塊,創建了一個進程字典,不過先不管他,看最后一個啟動的模塊

  • ecrn_control
-behaviour(gen_server).

start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

init([]) ->
    DateTime = erlang:localtime(),
    {ok, #state{reference_datetime=DateTime,
                datetime_at_reference=ecrn_util:epoch_seconds()}}.

也是一個gen_server 模塊,state里放了啟動時間,不過是兩種不同格式

3.三個進程啟動完了后在ecrn_app.erl 執行setup()

setup() ->
    case application:get_env(erlcron, crontab) of 
        {ok, Crontab} ->
            lists:foreach(fun(CronJob) ->
                erlcron:cron(CronJob) 
            end, Crontab);
        undefined ->
            ok
    end.

這里看到用了一個lists:foreach/2 循環執行在sys.config里配置的需要定時執行的任務。


具體如何做到循環執行的 就要看erlcron:cron/1是如何處理列表里job的了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • erlang應用腳本stop分析 其實這篇文章的名字應該是如何安全關閉erlang應用更加科學。 erlang應用...
    randyjiawenjie閱讀 1,430評論 0 1
  • erlang應用腳本stop分析 其實這篇文章的名字應該是如何安全關閉erlang應用更加科學。 erlang應用...
    randyjia閱讀 1,256評論 0 1
  • 世界是并行的,Erlang程序反應了我們思考和交流的方式,人作為個體通過發送消息進行交流,如果有人死亡,其他人會注...
    abel_cao閱讀 2,795評論 1 4
  • 水都去哪兒了 楊文智老師的一首《感受秋天》的詩,把我的思緒一下子勾回到三十年前的那個村莊,那...
    欣民閱讀 370評論 1 1