|
@@ -0,0 +1,93 @@
|
|
|
+-module(myapp_events2).
|
|
|
+-behaviour(gen_server).
|
|
|
+
|
|
|
+-export([start_link/0]).
|
|
|
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
|
|
+
|
|
|
+-export([add_task/4, delete_task/2]).
|
|
|
+
|
|
|
+
|
|
|
+-define(IS_STRING(S), (is_list(S) andalso S /= [] andalso is_integer(hd(S)))).
|
|
|
+
|
|
|
+
|
|
|
+add_task(Task_Id, Proc_Id, Type, Time)
|
|
|
+when ?IS_STRING(Task_Id), is_integer(Proc_Id), ((Type =:= static) orelse (Type =:= dynamic)), is_integer(Time) ->
|
|
|
+ Time2 = case Type of
|
|
|
+ dynamic ->
|
|
|
+ erlang:system_time(second) + Time; % Timestamp_Now + Time
|
|
|
+ _ ->
|
|
|
+ % static
|
|
|
+ Time
|
|
|
+ end,
|
|
|
+ gen_server:cast(?MODULE, {add_task, {{Task_Id, Proc_Id}, Time2}}),
|
|
|
+ ok.
|
|
|
+
|
|
|
+delete_task(Task_Id, Proc_Id)
|
|
|
+when ?IS_STRING(Task_Id), is_integer(Proc_Id) ->
|
|
|
+ gen_server:cast(?MODULE, {delete_task, {Task_Id, Proc_Id}}),
|
|
|
+ ok.
|
|
|
+
|
|
|
+
|
|
|
+start_link() ->
|
|
|
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
+
|
|
|
+
|
|
|
+init([]) ->
|
|
|
+ erlang:send_after(100, self(), check_time_process),
|
|
|
+ State = [],
|
|
|
+ {ok, State}.
|
|
|
+
|
|
|
+
|
|
|
+handle_call(_Req, _From, State) ->
|
|
|
+ {reply, not_handled, State}.
|
|
|
+
|
|
|
+
|
|
|
+handle_cast({add_task, {{Task_Id, Proc_Id}, _Time}=V}, State) ->
|
|
|
+ ets:insert(events_table, V),
|
|
|
+ io:format("add/update: ~p ~p~n", [Task_Id, Proc_Id]),
|
|
|
+ {noreply, State};
|
|
|
+
|
|
|
+
|
|
|
+handle_cast({delete_task, {Task_Id, Proc_Id}=V}, State) ->
|
|
|
+ ets:delete(events_table, V),
|
|
|
+ io:format("deleted: ~p ~p~n", [Task_Id, Proc_Id]),
|
|
|
+ {noreply, State};
|
|
|
+
|
|
|
+
|
|
|
+handle_cast(_Req, State) ->
|
|
|
+ {noreply, State}.
|
|
|
+
|
|
|
+
|
|
|
+%handle_info(check_time_process, []) ->
|
|
|
+% erlang:send_after(1000, self(), check_time_process),
|
|
|
+% {noreply, []};
|
|
|
+handle_info(check_time_process, State) ->
|
|
|
+ Timestamp_Now = erlang:system_time(second),
|
|
|
+
|
|
|
+ %ets:fun2ms(fun({{A,B},C}) when C =< Timestamp_Now -> {{A,B},C} end).
|
|
|
+ %[{{{'$1','$2'},'$3'}, [{'=<','$3',{const,1642507256}}], [{{{{'$1','$2'}},'$3'}}]}]
|
|
|
+
|
|
|
+ L = ets:select(events_table, [{{{'$1','$2'},'$3'}, [{'=<','$3',{const, Timestamp_Now}}], [{{{{'$1','$2'}},'$3'}}]}] ),
|
|
|
+
|
|
|
+ lists:foldl(fun({{Task_Id0, Proc_Id0}=V, _Time0}, ok) ->
|
|
|
+ % send_to_rabbitmq({call_timer, Proc_Id0, Task_Id0}),
|
|
|
+ ets:delete(events_table, V),
|
|
|
+ io:format("tick: ~p ~p~n", [Task_Id0, Proc_Id0]),
|
|
|
+ ok
|
|
|
+ end, ok, L),
|
|
|
+
|
|
|
+ erlang:send_after(1000, self(), check_time_process),
|
|
|
+ {noreply, State};
|
|
|
+
|
|
|
+
|
|
|
+handle_info(_Request, State) ->
|
|
|
+ {noreply, State}.
|
|
|
+
|
|
|
+
|
|
|
+terminate(_Reason, _State) ->
|
|
|
+ ok.
|
|
|
+
|
|
|
+code_change(_OldVsn, State, _Extra) ->
|
|
|
+ {ok, State}.
|
|
|
+
|
|
|
+
|