PecanProject / pecan

The Predictive Ecosystem Analyzer (PEcAn) is an integrated ecological bioinformatics toolbox.

Home Page:www.pecanproject.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Aariq opened this issue · comments

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
)

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

  • PEcAn.utils is full of very-rarely-used functions and I think it generally needs to get smaller rather than larger. If we want to use robustly in multiple PEcAn packages then PEcAn.utils is the right place for it, but if it's currently only used in one package I favor waiting for a demonstrated need in another package rather than moving it preemptively.
  • I think of "let's retry this until it succeeds" as the thing I do when I don't understand what's failing, so if we start adopting this approach across many packages maybe we should re-evaluate what we mean by "robust".
  • The most obvious (to me) use cases are in the context of network calls, for which we're probably better off using dedicated network functions (e.g. httr::RETRY)

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.