This will source a project R file, or render any other project file, if time and user requirements are met using a CRON-like syntax (https://cron.help).

schedule_task(
  minute,
  hour,
  day,
  month,
  weekday,
  users,
  file,
  project_number,
  log = TRUE,
  ref_time = Sys.time(),
  account = connect_planner(),
  check_mail = length(users) > 1,
  check_log = length(users) > 1,
  sent_delay = 15,
  sent_account = connect_outlook(),
  sent_to = read_secret("mail.error_to"),
  log_folder = read_secret("projects.log_path")
)

Arguments

minute

one or more values between 0-59, or . or "*" (or NULL or left blank) for each minute

hour

one or more values between 0-23, or . or "*" (or NULL or left blank) for each hour

day

one or more values between 1-31, or . or "*" (or NULL or left blank) for each day

month

one or more values between 1-12, or . or "*" (or NULL or left blank) for each month

weekday

one or more values between 0-7 (Sunday is both 0 and 7; Monday is 1), or . or "*" (or NULL or left blank) for each weekday

users

logged in users, must correspond with Sys.info()["user"]. Currently logged in user is "runner ". This must be length > 1 if check_mail is TRUE.

file

file name within the project, supports regular expression

project_number

number of the project, must be numeric and exist in planner_tasks_list()

log

a logical to indicate whether this message should be printed: Running scheduled task at...

ref_time

time to use for reference, defaults to Sys.time()

account

Planner account

check_mail

a logical to indicate whether a project was sent by a previous user, by running certemail::mail_is_sent()

check_log

a logical to indicate whether a log file exist for the project from a previous user

sent_delay

delay in minutes. This will be multiplied by the position of the current user in users minus 1. For example, when sent_delay = 15, this will be 15 for user 2, and 30 for user 3.

sent_account

Outlook account, to search sent mails

sent_to

users to send error mail to

log_folder

path that contains log files

Details

For Windows, the Task Scheduler must be set up to use this function. Most convenient is to:

  1. Create an R file such as R_cron.R containing calls to schedule_task()

  2. Create a batch file such as R_cron.bat that runs R_cron.R with R CMD BATCH

  3. Set up a Windows Task Scheduler task that runs R_cron.bat every minute

For macOS and Linux, use the crontab to run the commands with Rscript:

* * * * * Rscript -e 'certeprojects::schedule_task(users = "user", file = "file every minute.R", project_number = 123)' 0 7 * * * Rscript -e 'certeprojects::schedule_task(users = "user", file = "file at 7 AM.R", project_number = 123)'

Examples

something_to_run <- function() {
  1 + 1
}

# units:      M  H  d  m  wd
schedule_task(., ., ., ., ., "user", "file", 123) # every minute
schedule_task(0, ., ., ., ., "user", "file", 123) # start of each hour
schedule_task(0, 7, ., ., ., "user", "file", 123) # everyday at 7h00
schedule_task(0, 7, 1, ., ., "user", "file", 123) # first day of month at 7h00
schedule_task(0, 7, ., 2, ., "user", "file", 123) # everyday day in February at 7h00
schedule_task(0, 7, ., ., 1, "user", "file", 123) # every Monday at 7h00
schedule_task(0, 7, 1, 2, ., "user", "file", 123) # every 1st of February at 7h00
schedule_task(0, 7, ., 2, 1, "user", "file", 123) # every Monday in February at 7h00
schedule_task(0, 7, 1, 2, 1, "user", "file", 123) # each February 1st if it's a Monday at 7h00
schedule_task(0, 7,29, 2, ., "user", "file", 123) # once every 4 years at 7h00

# examples of combinations

# everyday at 7h00 and 7h30
schedule_task(c(0, 30), 7,        .,       .,              ., "user", "file", 123)
# everyday at 7h00 and 15h00
schedule_task(0,        c(7, 15), .,       .,              ., "user", "file", 123)
# everyday at 7h00 and 7h30 and 15h00 and 15h30
schedule_task(c(0, 30), c(7, 15), .,       .,              ., "user", "file", 123)
# every second Monday of the month at 7h00:
schedule_task(0,        7,        c(8:14), .,              1, "user", "file", 123)
# every 15th of April at 8h30 and 16h30:
schedule_task(30,       c(8, 16), 15,      4,              ., "user", "file", 123)
# once per quarter at 8h00 on the first day of the month:
schedule_task(0,        8,        1,       c(1, 4, 7, 10), ., "user", "file", 123)

# fall-back for failed jobs

# this will run at 8h00 if current user is "user1"
schedule_task(0, 8, ., ., ., c("user1", "user2"), "file", 123)
# it will run again at default 15 minutes later (so, 8h15), if:
# - current user is "user2"
# - project 123 has no mail in Sent Items or log of "user1" contains errors