rrq_heartbeat | R Documentation |
Create a heartbeat instance
Create a heartbeat instance
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.
new()
Create a heartbeat object
rrq_heartbeat$new( key, period, expire = 3 * period, value = expire, config = NULL, start = TRUE, timeout = 10 )
key
Key to use. Once the heartbeat starts it will
create this key and set it to expire after expiry
seconds.
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.
This will be converted to character with as.character
before saving into Redis
config
Configuration parameters passed through to
redux::redis_config
. Provide as either a named list or a
redis_config
object. This allows host, port, password,
db, etc all to be set.
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.
is_running()
Report if heartbeat process is running. This will be
TRUE
if the process has been started and has not stopped.
rrq_heartbeat$is_running()
start()
Start the heartbeat process. An error will be thrown if it is already running.
rrq_heartbeat$start()
stop()
Stop the heartbeat process
rrq_heartbeat$stop(wait = TRUE)
wait
Logical, indicating if we should wait until the heartbeat process terminates (should take only a fraction of a second)
format()
Format method, used by R6 to nicely print the object
rrq_heartbeat$format(...)
...
Additional arguments, currently ignored
if (redux::redis_available()) {
rand_str <- function() {
paste(sample(letters, 20, TRUE), collapse = "")
}
key <- sprintf("rrq:heartbeat:%s", rand_str())
h <- rrq::rrq_heartbeat$new(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
# This is required to close any processes opened by this
# example, normally you would not need this.
processx:::supervisor_kill()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.