Lua Module API Reference

retry.lua

The retry.lua module contains the retry.call() function for retrying operations which might periodically fail like database operations.

The code for this module can be downloaded from our code repository.

The functions for this module are:

  • call : Retries a function, using the specified number of retries and pause time in seconds

retry.call{} [top]

Usage: retry.call{‘func’=<value> [, ‘arg1/2/3’=<value>] [, retry=<value>] [, pause=<value>] [, funcname=<value>]}

Retries a function, using the specified number of retries and pause time in seconds.

The purpose is to implement retry logic in interfaces that handle resources which may not always be available, e.g., databases, webservices etc. If the function throws an error this module logs an informational message containing the error details, then it sleeps and then retries the operation after the specified ‘pause’ time (in seconds).

If the function returns false this will be treated as a ‘fatal’ error and the error will be re-thrown which will (usually) stop the channel.

Note: Up to three arguments (arg1, arg2, arg3) are currently supported, it is simple to increase the number of arguments as required.

Returns:

  • Multiple Returns: Zero or more returns from the function call OR a single ERROR return when an error occurs any type
  • Last Return: Informational message describing the retry count and pause delay string
    Note: If nothing is returned from the function then the informational message becomes the first (and only) return

Accepts a table with the following required entries:

  • func : The function to call function

The following optional parameters can be added to the table:

  • arg1/2/3 : Three argument to the function (arg1, arg2, arg3) any type
    Note: The number of arguments can be increased as needed
  • retry : Count of times to retry (default = 100) integer
  • pause : Delay between retries in seconds (default = 10) integer
  • funcname : Name of the function (informational only for errors/logging) string

Sample Code

local R, M = retry.call{func=DoInsert, retry=1000, pause=10}
local R, M = retry.call{func=Hello, arg1={'Hello', 'World',}, retry=99, pause=3, funcname='Hello'}
local R, M = retry.call{func=Hello, arg1='Hello', arg2='World', retry=99, pause=3,funcname='Hello'}

-- the order of arguments within the table is not important - so this will also work
local R, M = retry.call{func=Hello, arg3='(again)', arg2='World', retry=99, pause=3,funcname='Hello', arg1='Hello'}