From 39cc6c19e79c19000d5e2174aa3b5f5c2ed2545b Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Wed, 15 Mar 2017 17:16:06 +0100 Subject: Add compat functions for timing functionality missing in R17. --- src/plop_compat.erl | 27 +++++++++++++++++++++++++-- src/statusreport.erl | 8 ++++---- statusserver/src/statusserver.erl | 4 ++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/plop_compat.erl b/src/plop_compat.erl index 9a98e3d..4d45590 100644 --- a/src/plop_compat.erl +++ b/src/plop_compat.erl @@ -2,14 +2,17 @@ %%% See LICENSE for licensing information. -module(plop_compat). --export([unpack_spki/1, timestamp/0]). +-export([unpack_spki/1, timestamp/0, monotonic_time/1, start_timer/4]). -include_lib("public_key/include/public_key.hrl"). unpack_spki(SPKI) -> unpack_spki(erlang:system_info(otp_release), SPKI). timestamp() -> timestamp(erlang:system_info(otp_release)). - +monotonic_time(Unit) -> + monotonic_time(erlang:system_info(otp_release), Unit). +start_timer(Time, Dest, Msg, Options) -> + start_timer(erlang:system_info(otp_release), Time, Dest, Msg, Options). unpack_spki("R16" ++ _, SPKI) -> #'SubjectPublicKeyInfo'{subjectPublicKey = {_, Octets}, @@ -37,3 +40,23 @@ timestamp("18") -> erlang:timestamp(); timestamp("19") -> erlang:timestamp(). + +monotonic_time("R16" ++ _, millisecond) -> + {MeS, S, MiS} = timestamp(), + MeS * 1000000 + S * 1000 + MiS; +monotonic_time("17", millisecond) -> + {MeS, S, MiS} = timestamp(), + MeS * 1000000 + S * 1000 + MiS; +monotonic_time("18", Unit) -> + erlang:monotonic_time(Unit); +monotonic_time("19", Unit) -> + erlang:monotonic_time(Unit). + +start_timer("R16" ++ _, Time, Dest, Msg, [{abs, true}]) -> + erlang:start_timer(max(0, Time - monotonic_time(millisecond)), Dest, Msg); +start_timer("17", Time, Dest, Msg, [{abs, true}]) -> + erlang:start_timer(max(0, Time - monotonic_time(millisecond)), Dest, Msg); +start_timer("18", Time, Dest, Msg, Options) -> + erlang:start_timer(Time, Dest, Msg, Options); +start_timer("19", Time, Dest, Msg, Options) -> + erlang:start_timer(Time, Dest, Msg, Options). diff --git a/src/statusreport.erl b/src/statusreport.erl index f0b7503..a9fef7f 100644 --- a/src/statusreport.erl +++ b/src/statusreport.erl @@ -29,7 +29,7 @@ init([]) -> {ok, #state{timer = none, nodename = http_auth:own_name(), statusreports = dict:new(), - lastsent = erlang:monotonic_time(millisecond) - ReportInterval}}. + lastsent = plop_compat:monotonic_time(millisecond) - ReportInterval}}. store_status(State, Service, Target, Variable, Status) -> Statusreports = dict:store({Service, Target, Variable}, @@ -101,7 +101,7 @@ set_timer(State) -> case State#state.timer of none -> ReportInterval = application:get_env(plop, status_report_interval, 1000), - Timer = erlang:start_timer(State#state.lastsent + ReportInterval, self(), force_send, [{abs, true}]), + Timer = plop_compat:start_timer(State#state.lastsent + ReportInterval, self(), force_send, [{abs, true}]), State#state{timer = Timer}; _ -> State @@ -174,12 +174,12 @@ force_send(State) -> send(Service, Statusreports, State#state.nodename) end, group_by_service(State#state.statusreports)), NewState = cancel_timer(State), - NewState#state{statusreports = dict:new(), lastsent = erlang:monotonic_time(millisecond)}. + NewState#state{statusreports = dict:new(), lastsent = plop_compat:monotonic_time(millisecond)}. try_send(State) -> ReportInterval = application:get_env(plop, status_report_interval, 1000), NextSend = State#state.lastsent + ReportInterval, - Now = erlang:monotonic_time(millisecond), + Now = plop_compat:monotonic_time(millisecond), if NextSend > Now -> lager:debug("status report sent ~p ms ago, setting timer", [NextSend - Now]), diff --git a/statusserver/src/statusserver.erl b/statusserver/src/statusserver.erl index e4f38d5..81fcd7a 100644 --- a/statusserver/src/statusserver.erl +++ b/statusserver/src/statusserver.erl @@ -23,7 +23,7 @@ request(post, ?APPURL_PLOP_STATUS, Service, Input) -> end, success({[{result, <<"ok">>}]}); request(get, "", "status", Input) -> - Now = erlang:monotonic_time(millisecond), + Now = plop_compat:monotonic_time(millisecond), Variables = [{struct, [ {service, list_to_binary(Service)}, {source, Source}, @@ -65,7 +65,7 @@ get_status() -> set_status(Service, Source, Target, Variable, Status) -> lager:info("status: ~p ~p ~p ~p ~p", [Service, Source, Target, Variable, Status]), - Timestamp = erlang:monotonic_time(millisecond), + Timestamp = plop_compat:monotonic_time(millisecond), true = ets:insert(?STATUSDB_TABLE, {{Service, Source, Target, Variable}, Status, Timestamp}), ok. -- cgit v1.1