Improve mod_cron to support ctl and command options (#307)
This commit is contained in:
parent
3d523cec45
commit
7e2fe1b789
|
@ -139,9 +139,10 @@ update_timer_ref(TaskId, NewTimerRef) ->
|
||||||
|
|
||||||
%% Method to add new task
|
%% Method to add new task
|
||||||
add_task(Host, Task) ->
|
add_task(Host, Task) ->
|
||||||
[TimeNum, TimeUnit, Mod, Fun, ArgsType, Args1, InTimerType] =
|
[TimeNum, TimeUnit, Mod1, Fun1, ArgsType, Args1, InTimerType, Command, Ctl] =
|
||||||
[proplists:get_value(Key, Task) || Key <- [time, units, module, function,
|
[proplists:get_value(Key, Task) || Key <- [time, units, module, function,
|
||||||
args_type, arguments, timer_type]],
|
args_type, arguments, timer_type,
|
||||||
|
command, ctl]],
|
||||||
TimerType = case InTimerType of
|
TimerType = case InTimerType of
|
||||||
<<"fixed">> ->
|
<<"fixed">> ->
|
||||||
fixed;
|
fixed;
|
||||||
|
@ -154,10 +155,9 @@ add_task(Host, Task) ->
|
||||||
%% Get new task identifier
|
%% Get new task identifier
|
||||||
TaskId = get_new_taskid(),
|
TaskId = get_new_taskid(),
|
||||||
|
|
||||||
Args = case ArgsType of
|
Args2 = parse_args_type(ArgsType, Args1),
|
||||||
string -> [binary_to_list(Arg) || Arg <- Args1];
|
|
||||||
_ -> Args1
|
{Mod, Fun, Args} = prepare_mfa(Mod1, Fun1, Args2, Command, Ctl),
|
||||||
end,
|
|
||||||
|
|
||||||
TimerRef = case TimerType of
|
TimerRef = case TimerType of
|
||||||
interval ->
|
interval ->
|
||||||
|
@ -182,6 +182,32 @@ get_new_taskid() ->
|
||||||
Id -> Id + 1
|
Id -> Id + 1
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
parse_args_type(string, Args) ->
|
||||||
|
lists:map(fun(Arg) when is_binary(Arg) -> binary_to_list(Arg);
|
||||||
|
(Arg) -> Arg
|
||||||
|
end,
|
||||||
|
Args);
|
||||||
|
parse_args_type(_, Args) ->
|
||||||
|
Args.
|
||||||
|
|
||||||
|
parse_args_ctl(Ctl, Args2) ->
|
||||||
|
[[atom_to_list(Ctl) | Args2]].
|
||||||
|
|
||||||
|
parse_args_command(Command, Args2) ->
|
||||||
|
CI = #{caller_module => ?MODULE},
|
||||||
|
[Command, Args2, CI].
|
||||||
|
|
||||||
|
prepare_mfa(undefined, undefined, Args2, Command, undefined)
|
||||||
|
when Command /= undefined ->
|
||||||
|
{ejabberd_commands, execute_command2,
|
||||||
|
parse_args_command(Command, Args2)};
|
||||||
|
prepare_mfa(undefined, undefined, Args2, undefined, Ctl)
|
||||||
|
when Ctl /= undefined ->
|
||||||
|
{ejabberd_ctl, process,
|
||||||
|
parse_args_ctl(Ctl, parse_args_type(string, Args2))};
|
||||||
|
prepare_mfa(Mod1, Fun1, Args2, undefined, undefined) ->
|
||||||
|
{Mod1, Fun1, Args2}.
|
||||||
|
|
||||||
%% Method to run existing task
|
%% Method to run existing task
|
||||||
run_task(Mod, Fun, Args) ->
|
run_task(Mod, Fun, Args) ->
|
||||||
case catch apply(Mod, Fun, Args) of
|
case catch apply(Mod, Fun, Args) of
|
||||||
|
|
Loading…
Reference in New Issue