heartbeat: Create a heartbeat instance

Description Usage Arguments Details Examples

View source: R/heartbeat.R

Description

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.

Usage

1
2
3
4
5
6
7
8
9
heartbeat(
  key,
  period,
  expire = 3 * period,
  value = expire,
  config = NULL,
  start = TRUE,
  timeout = 10
)

Arguments

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 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. Socket connections (i.e., using path to access Redis over a socket) are not currently supported.

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.

Details

The heartbeat object has three methods:

Heavily inspired by the doRedis package.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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
}

richfitz/RedisHeartbeat documentation built on April 17, 2021, 8:51 p.m.