From 3d523cec45ccb223da3c6dc62ee2d30c3d71ab5f Mon Sep 17 00:00:00 2001 From: Badlop Date: Wed, 5 Jan 2022 12:45:41 +0100 Subject: [PATCH] New option args_type useful when the function expects arguments as strings --- mod_cron/README.txt | 13 +++++++++++-- mod_cron/src/mod_cron.erl | 10 ++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/mod_cron/README.txt b/mod_cron/README.txt index f24a04b..454f997 100644 --- a/mod_cron/README.txt +++ b/mod_cron/README.txt @@ -27,11 +27,12 @@ modules: TASK SYNTAX =========== -Each task is described with five elements: +Tasks are described with those elements: * Time is an integer. * Units indicates the time unit you use. It can be: seconds, minutes, hours, days. * Module and * Function are the exact call you want to schedule. -* Arguments is an array. Strings will be converted to binaries. +* Arguments is an array. By default strings will be converted to binaries. +* args_type can be set to string, if the function expects strings instead of binaries * timer_type is one of 'fixed' or 'interval'. Fixed timers occur at a fixed time after the [minute|hour|day] e.g. every hour on the 5th minute (1:05PM, 2:05PM etc) interval timers occur every interval (starting on an even unit) e.g. every 10 minutes @@ -64,6 +65,14 @@ modules: - "localhost" - "somepass" timer_type: interval + - time: 24 + units: hours + module: mnesia + function: backup + timer_type: interval + args_type: string + arguments: + - "/var/log/ejabberd/mnesia.backup" EJABBERD COMMANDS diff --git a/mod_cron/src/mod_cron.erl b/mod_cron/src/mod_cron.erl index 5e59ef5..0a3d6f6 100644 --- a/mod_cron/src/mod_cron.erl +++ b/mod_cron/src/mod_cron.erl @@ -139,8 +139,9 @@ update_timer_ref(TaskId, NewTimerRef) -> %% Method to add new task add_task(Host, Task) -> - [TimeNum, TimeUnit, Mod, Fun, Args, InTimerType] = - [proplists:get_value(Key, Task) || Key <- [time, units, module, function, arguments, timer_type]], + [TimeNum, TimeUnit, Mod, Fun, ArgsType, Args1, InTimerType] = + [proplists:get_value(Key, Task) || Key <- [time, units, module, function, + args_type, arguments, timer_type]], TimerType = case InTimerType of <<"fixed">> -> fixed; @@ -153,6 +154,11 @@ add_task(Host, Task) -> %% Get new task identifier TaskId = get_new_taskid(), + Args = case ArgsType of + string -> [binary_to_list(Arg) || Arg <- Args1]; + _ -> Args1 + end, + TimerRef = case TimerType of interval -> begin_interval_timer(TaskId, TimeUnit, TimeNum, [Mod, Fun, Args]);