Create a heartbeat instance. This can be used by running
obj$start()
which will reset the TTL (Time To Live) on key
every
period
seconds (don't set this too high). If the R process
dies, then the key will expire after 3 * period
seconds (or
set expire
) and another application can tell that this R
instance has died.
heartbeat( key, period, expire = 3 * period, value = expire, config = NULL, start = TRUE, timeout = 10 )
key | Key to use |
---|---|
period | Timeout period (in seconds) |
expire | Key expiry time (in seconds) |
value | Value to store in the key. By default it stores the expiry time, so the time since last heartbeat can be computed. |
config | Configuration parameters passed through to
|
start | Should the heartbeat be started immediately? |
timeout | Time, in seconds, to wait for the heartbeat to appear. It should generally appear very quickly (within a second unless your connection is very slow) so this can be generally left alone. |
The heartbeat object has three methods:
is_running()
which returns TRUE
or
FALSE
if the heartbeat is/is not running.
start()
which starts a heartbeat
stop()
which requests a stop for the heartbeat
Heavily inspired by the doRedis
package.
if (redux::redis_available()) { rand_str <- function() { paste(sample(letters, 20, TRUE), collapse = "") } key <- sprintf("heartbeatr:test:%s", rand_str()) h <- heartbeatr::heartbeat(key, 1, expire = 2) con <- redux::hiredis() # The heartbeat key exists con$EXISTS(key) # And has an expiry of less than 2000ms con$PTTL(key) # We can manually stop the heartbeat, and 2s later the key will # stop existing h$stop() # Sys.sleep(2) # con$EXISTS(key) # 0 }#> [1] TRUE