PecanProject / pecan

The Predictive Ecosystem Analyzer (PEcAn) is an integrated ecological bioinformatics toolbox.
www.pecanproject.org
Other
202 stars 230 forks source link

Move `robustly()` to PEcAn.utils? Redundant of `retry.func()`? #3073

Closed Aariq closed 1 year ago

Aariq commented 1 year ago

PEcAn.data.atmosphere::robustly() and PEcAn.utils::retry.func() do almost the same thing.

robustly() is an adverb that returns a function (similar to purrr::safely()), while retry.func() returns the output of an expression (more like try())

robustly() seems like it would be widely useful and should go in PEcAn.utils along side or replacing retry.func()

#' @param .f Function to call.
#' @param n Number of attempts to try
#' @param timeout Timeout between attempts, in seconds
#' @param silent Silence error messages?
#' @return Modified version of input function
robustly(.f, n = 10, timeout = 0.2, silent = TRUE)
#' @param expr The function to try running
#' @param maxErrors The number of times to retry the function
#' @param sleep How long to wait before retrying the function call
#' @param isError function to use for checking whether to try again.
#'   Must take one argument that contains the result of evaluating `expr`
#'   and return TRUE if another retry is needed 
#' @return retval returns the results of the function call
retry.func(
  expr,
  isError = function(x) inherits(x, "try-error"),
  maxErrors = 5,
  sleep = 0
)
infotroph commented 1 year ago

I agree that the functionality of robustly isn't specific to data.atmosphere, but am less enthusiastic about moving it for a few reasons:

mdietze commented 1 year ago

On @infotroph second and 3rd points, I'm fairly confident that this function exists in data.atm because of API calls. I'm not familiar with RETRY, but taking a quick look I don't think it's equivalent (e.g. one of the example applications is opening a remote netCDF file via nc_open). That said, the other usage in data.atm is a httr::GET call that does look like it could be replaced by RETRY. I suspect that robustly isn't used in more packages more because people don't know about it than a lack of demand.