In addition to passing tasks (and results) between a controller and workers, the controller can also send "messages" to workers. This vignette shows what the possible messages do.
In order to do this, we're going to need a queue and a worker:
library(rrq) id <- paste0("rrq:", ids::random_id(bytes = 4)) obj <- rrq_controller2(id) rrq_default_controller_set(obj) logdir <- tempfile() w <- rrq_worker_spawn2(logdir = logdir) #> ℹ Spawning 1 worker with prefix 'schematic_hairstreakbutterfly' worker_id <- w$id
On startup the worker log contains:
[2024-04-19 12:24:14.351378] ALIVE [2024-04-19 12:24:14.353241] ENVIR new [2024-04-19 12:24:14.354013] QUEUE default __ ______________ _/ / ______ / ___/ ___/ __ `/ /_____ /_____/ / / / / / /_/ /_/_____/ ______ /_/ /_/ \__, (_) ______ /_____/ /_/ /_____/ worker: schematic_hairstreakbutterfly_1 config: localhost rrq_version: 0.7.13 platform: x86_64-pc-linux-gnu (64-bit) running: Ubuntu 20.04.6 LTS hostname: wpia-dide136 username: rich queue: rrq:82028045:queue:default wd: /home/rich/Documents/src/rrq/vignettes_src pid: 79662 redis_host: 127.0.0.1 redis_port: 6379 heartbeat_key: <not set>
Because one of the main effects of messages is to print to the worker logfile, we'll print this fairly often.
The queue sends a message for one or more workers to process. The message has an identifier that is derived from the current time. Messages are written to a first-in-first-out queue, per worker, and are processed independently by workers who do not look to see if other workers have messages or are processing them.
As soon as a worker has finished processing any current job it will process the message (it must wait to finish a current job but will not start any further jobs).
Once the message has been processed (see below) a response will be written to a response list with the same identifier as the message.
Some messages interact with the worker timeout:
PING
, ECHO
, EVAL
, INFO
PAUSE
, RESUME
and REFRESH
will reset the timer, as if a task had been runTIMEOUT_SET
explicitly interacts with the timerTIMEOUT_GET
does not reset the timer, reporting the remaining timeSTOP
causes the worker to exit, so has no interaction with the timerPING
The PING
message simply asks the worker to return PONG
. It's
useful for diagnosing communication issues because it does so
little
message_id <- rrq_message_send("PING")
The message id is going to be useful for getting responses:
message_id
#> [1] "1713525854.452866"
(this is derived from the current time, according to Redis which is the central reference point of time for the whole system).
[2024-04-19 12:24:14.453323] MESSAGE PING PONG [2024-04-19 12:24:14.454021] RESPONSE PING
The logfile prints:
PING
(MESSAGE PING
)PONG
to the R message streamRESPONSE PONG
), which means that something is written to the response stream.We can access the same bits of information in the worker log:
rrq_worker_log_tail(n = Inf) #> worker_id child time command message #> 1 schematic_hairstreakbutterfly_1 NA 1713525854 ALIVE #> 2 schematic_hairstreakbutterfly_1 NA 1713525854 ENVIR new #> 3 schematic_hairstreakbutterfly_1 NA 1713525854 QUEUE default #> 4 schematic_hairstreakbutterfly_1 NA 1713525854 MESSAGE PING #> 5 schematic_hairstreakbutterfly_1 NA 1713525854 RESPONSE PING
This includes the ALIVE
message as the worker comes up.
Inspecting the logs is fine for interactive use, but it's going to be more useful often to poll for a response.
We already know that our worker has a response, but we can ask anyway:
rrq_message_has_response(message_id) #> schematic_hairstreakbutterfly_1 #> TRUE
Or inversely we can as what messages a given worker has responses for:
rrq_message_response_ids(worker_id) #> [1] "1713525854.452866"
To fetch the responses from all workers it was sent to (always returning a named list):
rrq_message_get_response(message_id) #> $schematic_hairstreakbutterfly_1 #> [1] "PONG"
or to fetch the response from a given worker:
rrq_message_get_response(message_id, worker_id) #> $schematic_hairstreakbutterfly_1 #> [1] "PONG"
The response can be deleted by passing delete = TRUE
to this method:
rrq_message_get_response(message_id, worker_id, delete = TRUE) #> $schematic_hairstreakbutterfly_1 #> [1] "PONG"
after which recalling the message will throw an error:
rrq_message_get_response(message_id, worker_id) #> Error in `rrq_message_get_response()`: #> ! Response missing for worker: 'schematic_hairstreakbutterfly_1'
There is also a timeout
argument that lets you wait until a response is
ready (as in rrq_task_wait()
).
rrq_task_create_expr(Sys.sleep(2)) #> [1] "647f26b19fd40d13426a8ea60fe1cc2d" message_id <- rrq_message_send("PING") rrq_message_get_response( message_id, worker_id, delete = TRUE, timeout = 10) #> $schematic_hairstreakbutterfly_1 #> [1] "PONG"
Looking at the log will show what went on here:
rrq_worker_log_tail(n = 4) #> worker_id child time command #> 1 schematic_hairstreakbutterfly_1 NA 1713525855 TASK_START #> 2 schematic_hairstreakbutterfly_1 NA 1713525857 TASK_COMPLETE #> 3 schematic_hairstreakbutterfly_1 NA 1713525857 MESSAGE #> 4 schematic_hairstreakbutterfly_1 NA 1713525857 RESPONSE #> message #> 1 647f26b19fd40d13426a8ea60fe1cc2d #> 2 647f26b19fd40d13426a8ea60fe1cc2d #> 3 PING #> 4 PING
However, because the message is only processed after the task is completed, the response takes a while to come back. Equivalently, from the worker log:
[2024-04-19 12:24:14.603817] TASK_START 647f26b19fd40d13426a8ea60fe1cc2d [2024-04-19 12:24:16.612506] TASK_COMPLETE 647f26b19fd40d13426a8ea60fe1cc2d [2024-04-19 12:24:16.614554] MESSAGE PING PONG [2024-04-19 12:24:16.615794] RESPONSE PING
ECHO
This is basically like PING
and not very interesting; it prints
an arbitrary string to the log. It always returns "OK"
as a
response.
message_id <- rrq_message_send("ECHO", "hello world!") rrq_message_get_response(message_id, worker_id, timeout = 10) #> $schematic_hairstreakbutterfly_1 #> [1] "OK"
[2024-04-19 12:24:17.136045] MESSAGE ECHO hello world! [2024-04-19 12:24:17.136638] RESPONSE ECHO
INFO
The INFO
command refreshes and returns the worker information.
We already have a copy of the worker info; it was created when the worker started up:
rrq_worker_info()[[worker_id]] #> <rrq_worker_info> #> worker: schematic_hairstreakbutterfly_1 #> config: localhost #> rrq_version: 0.7.13 #> platform: x86_64-pc-linux-gnu (64-bit) #> running: Ubuntu 20.04.6 LTS #> hostname: wpia-dide136 #> username: rich #> queue: rrq:82028045:queue:default #> wd: /home/rich/Documents/src/rrq/vignettes_src #> pid: 79662 #> redis_host: 127.0.0.1 #> redis_port: 6379
We can force the worker to refresh:
message_id <- rrq_message_send("INFO")
Here's the new worker information, complete with an updated envir
field:
rrq_message_get_response(message_id, worker_id, timeout = 10) #> $schematic_hairstreakbutterfly_1 #> $schematic_hairstreakbutterfly_1$worker #> [1] "schematic_hairstreakbutterfly_1" #> #> $schematic_hairstreakbutterfly_1$config #> [1] "localhost" #> #> $schematic_hairstreakbutterfly_1$rrq_version #> [1] "0.7.13" #> #> $schematic_hairstreakbutterfly_1$platform #> [1] "x86_64-pc-linux-gnu (64-bit)" #> #> $schematic_hairstreakbutterfly_1$running #> [1] "Ubuntu 20.04.6 LTS" #> #> $schematic_hairstreakbutterfly_1$hostname #> [1] "wpia-dide136" #> #> $schematic_hairstreakbutterfly_1$username #> [1] "rich" #> #> $schematic_hairstreakbutterfly_1$queue #> [1] "rrq:82028045:queue:default" #> #> $schematic_hairstreakbutterfly_1$wd #> [1] "/home/rich/Documents/src/rrq/vignettes_src" #> #> $schematic_hairstreakbutterfly_1$pid #> [1] 79662 #> #> $schematic_hairstreakbutterfly_1$redis_host #> [1] "127.0.0.1" #> #> $schematic_hairstreakbutterfly_1$redis_port #> [1] 6379
EVAL
Evaluate an arbitrary R expression, passed as a string (not as any sort of unevaluated or quoted expression). This expression is evaluated in the global environment, which is not the environment in which queued code is evaluated in.
message_id <- rrq_message_send("EVAL", "1 + 1") rrq_message_get_response(message_id, worker_id, timeout = 10) #> $schematic_hairstreakbutterfly_1 #> [1] 2
This could be used to evaluate code that has side effects, such as installing packages. However, due to limitations with how R loads packages the only way to update and reload a package is going to be to restart the worker.
PAUSE
/ RESUME
The PAUSE
/ RESUME
messages can be used to prevent workers from picking up new work (and then allowing them to start again).
rrq_worker_status() #> schematic_hairstreakbutterfly_1 #> "IDLE" message_id <- rrq_message_send("PAUSE") rrq_message_get_response(message_id, worker_id, timeout = 10) #> $schematic_hairstreakbutterfly_1 #> [1] "OK" rrq_worker_status() #> schematic_hairstreakbutterfly_1 #> "PAUSED"
Once paused workers ignore tasks, which stay on the queue:
t <- rrq_task_create_expr(runif(5)) rrq_task_status(t) #> [1] "PENDING"
Sending a RESUME
message unpauses the worker:
message_id <- rrq_message_send("RESUME") rrq_message_get_response(message_id, worker_id, timeout = 10) #> $schematic_hairstreakbutterfly_1 #> [1] "OK" rrq_task_wait(t, 5) #> [1] TRUE
SET_TIMEOUT
/ GET_TIMEOUT
Workers will quit after being left idle for more than a certain time; this is their timeout. Only processing tasks counts as work (not messages). You can query the timeout with GET_TIMEOUT
and set it with SET_TIMEOUT
. For our worker above the timeout is infinite; it will never quit:
rrq_message_send_and_wait("TIMEOUT_GET", worker_ids = worker_id) #> $schematic_hairstreakbutterfly_1 #> timeout_idle remaining #> Inf Inf
We can set this to a finite value, in seconds:
rrq_message_send_and_wait("TIMEOUT_SET", 600, worker_ids = worker_id) #> $schematic_hairstreakbutterfly_1 #> [1] "OK"
Here the timeout is set to 10 minutes (600s).
Once set, the TIMEOUT_GET
returns the length of time remaining before the worker exits
rrq_message_send_and_wait("TIMEOUT_GET", worker_ids = worker_id) #> $schematic_hairstreakbutterfly_1 #> timeout_idle remaining #> 600.0000 599.9353 Sys.sleep(5) rrq_message_send_and_wait("TIMEOUT_GET", worker_ids = worker_id) #> $schematic_hairstreakbutterfly_1 #> timeout_idle remaining #> 600.0000 594.8724
One useful pattern is to send work to workers, then set the timeout to zero. This means that when work is complete they will exit (almost) immediately:
ids <- rrq_task_create_bulk_call(function(x) { Sys.sleep(0.5) runif(x) }, 1:5) rrq_message_send("TIMEOUT_SET", 0, worker_id) rrq_task_wait(ids) #> [1] TRUE rrq_task_results(ids) #> [[1]] #> [1] 0.1705179 #> #> [[2]] #> [1] 0.2393208 0.4059544 #> #> [[3]] #> [1] 0.5884221 0.9453424 0.6684895 #> #> [[4]] #> [1] 0.07630751 0.92618400 0.67542756 0.28708847 #> #> [[5]] #> [1] 0.7492767 0.2436322 0.6087192 0.8494850 0.2543259
The worker will remain idle for 60s (by default) which is the length of time that one poll for work lasts, then it will exit.
rrq_worker_status(worker_id) #> schematic_hairstreakbutterfly_1 #> "IDLE" rrq_message_send_and_wait("TIMEOUT_GET", worker_ids = worker_id) #> $schematic_hairstreakbutterfly_1 #> timeout_idle remaining #> 0 0
There are other methods that are typically used via methods on the [rrq_controller
] object.
REFRESH
: requests that the worker refresh its evaluation environment. Typically used via rrq_worker_envir_set()
STOP
: sent with a informational message as an argument, requests that the worker stop. Typically used via rrq_worker_stop()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.