This commit is contained in:
anlicheng 2025-05-07 17:24:46 +08:00
parent f42433e609
commit ff6af0faa3
3 changed files with 3 additions and 152 deletions

View File

@ -1,150 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 19. 4 2025 16:48
%%%-------------------------------------------------------------------
-module(efka_downloader).
-author("anlicheng").
-behaviour(gen_server).
%% API
-export([start_monitor/0, download/4]).
-export([test/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, {
}).
%%%===================================================================
%%% API
%%%===================================================================
test() ->
{ok, {Pid, _}} = start_monitor(),
Url = "http://118.178.229.213:3000/anlicheng/ekfa/archive1/main.tar.gz",
TargetDir = "/tmp/",
Ref = make_ref(),
download(Pid, Ref, Url, TargetDir),
receive
Info ->
efka_logger:debug("info is: ~p", [Info])
end,
ok.
-spec download(Pid :: pid(), Ref :: reference(), Url :: string(), TargetDir :: string()) -> reference().
download(Pid, Ref, Url, TargetDir) when is_pid(Pid), is_reference(Ref), is_list(Url), is_list(TargetDir) ->
ReceiverPid = self(),
gen_server:cast(Pid, {download, Ref, ReceiverPid, Url, TargetDir}).
%% @doc Spawns the server and registers the local name (unique)
-spec(start_monitor() ->
{ok, {Pid :: pid(), Ref :: reference()}} | ignore | {error, Reason :: term()}).
start_monitor() ->
gen_server:start_monitor(?MODULE, [], []).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%% @private
%% @doc Initializes the server
-spec(init(Args :: term()) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
{ok, #state{}}.
%% @private
%% @doc Handling call messages
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
State :: #state{}) ->
{reply, Reply :: term(), NewState :: #state{}} |
{reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Request, _From, State = #state{}) ->
{reply, ok, State}.
%% @private
%% @doc Handling cast messages
-spec(handle_cast(Request :: term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_cast({download, Ref, ReceiverPid, Url, TargetDir}, State = #state{}) ->
SslOpts = [
{ssl, [
%
{verify, verify_none}
]}
],
TargetFile = get_filename_from_url(Url),
FullFilename = TargetDir ++ TargetFile,
StartTs = os:timestamp(),
case httpc:request(get, {Url, []}, SslOpts, [{sync, false}, {stream, self}]) of
{ok, RequestId} ->
case receive_data(RequestId, FullFilename) of
ok ->
EndTs = os:timestamp(),
%%
CostMs = timer:now_diff(EndTs, StartTs) div 1000,
lager:debug("[efka_downloader] download url: ~p, cost: ~p(ms)", [Url, CostMs]),
ReceiverPid ! {downloader_reply, Ref, ok},
{stop, normal, State};
{error, Reason} ->
%%
file:delete(FullFilename),
ReceiverPid ! {downloader_reply, Ref, {error, Reason}},
{stop, normal, State}
end;
{error, Reason} ->
ReceiverPid ! {downloader_reply, Ref, {error, Reason}},
{stop, normal, State}
end.
%% @private
%% @doc Handling all non call/cast messages
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
{noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}).
handle_info(_Info, State = #state{}) ->
{noreply, State}.
%% @private
%% @doc This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
State :: #state{}) -> term()).
terminate(_Reason, _State = #state{}) ->
ok.
%% @private
%% @doc Convert process state when code is changed
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
Extra :: term()) ->
{ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State = #state{}, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================

View File

@ -55,6 +55,7 @@ start_link() ->
{stop, Reason :: term()} | ignore).
init([]) ->
erlang:process_flag(trap_exit, true),
% Url = "http://118.178.229.213:3000/anlicheng/ekfa/archive1/main.tar.gz",
{ok, RootDir} = application:get_env(efka, root_dir),
{ok, #state{root_dir = RootDir}}.

View File

@ -120,8 +120,8 @@ init([ServiceId]) ->
error ->
lager:notice("[efka_micro_service] service_id: ~p, not found", [ServiceId]),
ignore;
{ok, Service = #micro_service{work_dir = WorkDir0}} ->
case efka_manifest:new(WorkDir0) of
{ok, Service = #micro_service{root_dir = RootDir}} ->
case efka_manifest:new(RootDir) of
{ok, Manifest} ->
init0(Service, Manifest);
{error, Reason} ->