Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change API so it works when a midnight boundary is crossed.
- Loading branch information
Showing
4 changed files
with
64 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
%% @doc Runs a command at the same time every day. | ||
|
||
-module(daily_task). | ||
|
||
-export([start/3, | ||
start_link/3]). | ||
|
||
-record(task, {cmd, | ||
start_at, | ||
duration=60000, | ||
state=wait}). | ||
|
||
%% -- public api | ||
|
||
%% @doc Start the task server standalone. | ||
%% | ||
%% @spec start(Cmd, {H,M,S}, Duration) -> {ok, Pid}. | ||
%% @spec start("say 'Wakey Wakey!'", {6,0,0}, 30) -> {ok, Pid}. | ||
start(Cmd, Start = {_, _, _}, Duration) -> | ||
{ok, spawn(fun() -> loop(make_task(Cmd, Start, Duration)) end)}. | ||
|
||
%% @doc Start the task server. | ||
%% | ||
%% Cmd is the shell command to run. | ||
%% {H,M,S} represents the time at which it should run. | ||
%% Duration is the number of seconds the task should run for. | ||
%% | ||
%% @spec start_link(Cmd, {H,M,S}, Duration) -> {ok, Pid}. | ||
%% @spec start_link("say 'Wakey Wakey!'", {6,0,0}, 30) -> {ok, Pid}. | ||
start_link(Cmd, Start = {_, _, _}, Duration) -> | ||
{ok, spawn_link(fun() -> loop(make_task(Cmd, Start, Duration)) end)}. | ||
|
||
%% -- private api | ||
|
||
loop(Task = #task{state=wait}) -> | ||
case within_run_window(calendar:local_time(), Task) of | ||
true -> | ||
io:format("Running `~s'~n", [Task#task.cmd]), | ||
os:cmd(Task#task.cmd), | ||
loop(Task#task{state=running}); | ||
false -> | ||
timer:sleep(1000), | ||
loop(Task) | ||
end; | ||
loop(Task = #task{state=running}) -> | ||
case within_run_window(calendar:local_time(), Task) of | ||
true -> | ||
timer:sleep(1000), | ||
loop(Task); | ||
false -> | ||
io:format("Stopping `~s'~n", [Task#task.cmd]), | ||
loop(Task#task{state=wait}) | ||
end. | ||
|
||
make_task(Cmd, {H,M,S}, Duration) -> | ||
#task{cmd=Cmd, state=wait, start_at={H,M,S}, duration=Duration}. | ||
|
||
within_run_window({Date, Time}, #task{start_at={H, M, S}, duration=Duration}) -> | ||
Now = calendar:datetime_to_gregorian_seconds({Date, Time}), | ||
Start = calendar:datetime_to_gregorian_seconds({Date, {H, M, S}}), | ||
Now >= Start andalso Now < Start + Duration. |