Title: | Heartbeat Support using 'Redis' |
---|---|
Description: | Simple heartbeat support for R using 'Redis'. A heartbeat is a background thread that acts as a dead-man's switch. It will create a key on Redis that will automatically expire after a number of seconds and then periodically refresh that key, even when the R process is busy. If the process dies for some reason, then the key will disappear. A heartbeat can be used to detect loss of worker processes on shared systems. |
Authors: | Rich FitzJohn [aut, cre], Imperial College of Science, Technology and Medicine [cph] |
Maintainer: | Rich FitzJohn <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.6.0 |
Built: | 2024-11-25 05:55:05 UTC |
Source: | https://github.com/mrc-ide/heartbeatr |
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 )
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 }
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 }
Sends a signal to a heartbeat process that is using key key
heartbeat_send_signal(con, key, signal)
heartbeat_send_signal(con, key, signal)
con |
A hiredis object |
key |
The heartbeat key |
signal |
A signal to send (e.g. |
if (redux::redis_available()) { rand_str <- function() { paste(sample(letters, 20, TRUE), collapse = "") } # Suppose we have a process that exposes a heartbeat running on # this key: key <- sprintf("heartbeatr:test:%s", rand_str()) # We can send it an interrupt over redis using: con <- redux::hiredis() heartbeatr::heartbeat_send_signal(con, key, tools::SIGINT) }
if (redux::redis_available()) { rand_str <- function() { paste(sample(letters, 20, TRUE), collapse = "") } # Suppose we have a process that exposes a heartbeat running on # this key: key <- sprintf("heartbeatr:test:%s", rand_str()) # We can send it an interrupt over redis using: con <- redux::hiredis() heartbeatr::heartbeat_send_signal(con, key, tools::SIGINT) }