0

I have a small API, that I need to stop running if a certain time in the day is reached, say midnight for example, and then continue on to the rest of the program. And that independant from a call from a request. Beacause one could include a function that breaks the process by checking the Sy.time(), but that will only be executed if a request come through. My guess is that I have to modify some attribute of the $run, but can't find much about it on the internet. I know that one could get the pid and kill it with a system command, but I don't know if that solution makes sure that the rest of the program runs.

Does anyone have an idea?

Thanks in advance.

The file containing the function looks like : (my_file.R)

#* @param x My argument
#* @get /lag_lead 
function(x){
 x <- as.numeric(x)  
 c(x-1, x+1) 
}

and the running script :

library(plumber)
mon_api <- plumb('my_file.R')
mon_api$run(port = 8000)
print('hello')
Parisa
  • 448
  • 5
  • 11

1 Answers1

1

One way to do so, would be to set a timeout with withTimeout which is a wrapper for setTimeLimit

library(R-utils)
withTimeout(mon_api$run(port = 8000), timeout = 30)
# timeout is in seconds

# calculate timeout, using difftime between now and midnight
tmo <- as.numeric(difftime(as.POSIXct("2018-12-28 00:00:00"), Sys.time(), units = "secs"))
withTimeout(mon_api$run(port = 8000), timeout = tmo)

Some other timeout solutions, one using parallel Time out an R command via something like try()

SamFlynn
  • 369
  • 7
  • 20
  • Hello and thanks alot, this is what I was lookibg for, but the library is not available at the R server I use ! I will try to mimick the function. Thanks again – Parisa Dec 27 '18 at 14:51