| 1 |
|
| 2 |
renv_watchdog_server_start <- function(client) { |
| 3 |
|
| 4 |
# silence warnings |
| 5 |
renv_scope_options(warn = -1L) |
| 6 |
|
| 7 |
# initialize logging |
| 8 |
renv_log_init() |
| 9 |
|
| 10 |
# create socket server |
| 11 |
server <- renv_socket_server() |
| 12 |
dlog("watchdog-server", "Listening on port %i.", server$port) |
| 13 |
|
| 14 |
# communicate information back to client |
| 15 |
dlog("watchdog-server", "Waiting for client...") |
| 16 |
metadata <- list(port = server$port, pid = server$pid) |
| 17 |
conn <- renv_socket_connect(port = client$port, open = "wb") |
| 18 |
defer(close(conn)) |
| 19 |
serialize(metadata, connection = conn) |
| 20 |
dlog("watchdog-server", "Synchronized with client.") |
| 21 |
|
| 22 |
# initialize locks |
| 23 |
lockenv <- new.env(parent = emptyenv()) |
| 24 |
|
| 25 |
# start listening for connections |
| 26 |
repeat tryCatch( |
| 27 |
renv_watchdog_server_run(server, client, lockenv), |
| 28 |
error = function(e) { |
| 29 |
dlog("watchdog-server", "Error: %s", conditionMessage(e)) |
| 30 |
} |
| 31 |
) |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
renv_watchdog_server_run <- function(server, client, lockenv) { |
| 36 |
|
| 37 |
# check for parent exit |
| 38 |
if (!renv_process_exists(client$pid)) { |
| 39 |
dlog("watchdog-server", "Client process has exited; shutting down.") |
| 40 |
renv_watchdog_server_exit(server, client, lockenv) |
| 41 |
} |
| 42 |
|
| 43 |
# set file time on owned locks, so we can see they're not orphaned |
| 44 |
dlog("watchdog-server", "Refreshing lock times.") |
| 45 |
locks <- ls(envir = lockenv, all.names = TRUE) |
| 46 |
renv_lock_refresh(locks) |
| 47 |
|
| 48 |
# wait for connection |
| 49 |
dlog("watchdog-server", "Waiting for connection...") |
| 50 |
conn <- renv_socket_accept(server$socket, open = "rb", timeout = 1) |
| 51 |
defer(close(conn)) |
| 52 |
|
| 53 |
# read the request |
| 54 |
dlog("watchdog-server", "Received connection; reading data.") |
| 55 |
request <- unserialize(conn) |
| 56 |
|
| 57 |
dlog("watchdog-server", "Received request.") |
| 58 |
str(request) |
| 59 |
|
| 60 |
# handle the request |
| 61 |
renv_watchdog_server_run_impl(server, client, lockenv, request) |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
renv_watchdog_server_run_impl <- function(server, client, lockenv, request) { |
| 66 |
|
| 67 |
switch( |
| 68 |
|
| 69 |
request$method %||% "<missing>", |
| 70 |
|
| 71 |
ListLocks = { |
| 72 |
dlog("watchdog-server", "Executing 'ListLocks' request.") |
| 73 |
conn <- renv_socket_connect(port = request$port, open = "wb") |
| 74 |
defer(close(conn)) |
| 75 |
locks <- ls(envir = lockenv, all.names = TRUE) |
| 76 |
serialize(locks, connection = conn) |
| 77 |
}, |
| 78 |
|
| 79 |
LockAcquired = { |
| 80 |
dlog("watchdog-server", "Acquired lock on path '%s'.", request$data$path) |
| 81 |
assign(request$data$path, TRUE, envir = lockenv) |
| 82 |
}, |
| 83 |
|
| 84 |
LockReleased = { |
| 85 |
dlog("watchdog-server", "Released lock on path '%s'.", request$data$path) |
| 86 |
rm(list = request$data$path, envir = lockenv) |
| 87 |
}, |
| 88 |
|
| 89 |
Shutdown = { |
| 90 |
dlog("watchdog-server", "Received shutdown request; shutting down.") |
| 91 |
renv_watchdog_server_exit(server, client, lockenv) |
| 92 |
}, |
| 93 |
|
| 94 |
"<missing>" = { |
| 95 |
dlog("watchdog-server", "Received request with no method field available.") |
| 96 |
}, |
| 97 |
|
| 98 |
{ |
| 99 |
dlog("watchdog-server", "Unknown method '%s'", request$method) |
| 100 |
} |
| 101 |
|
| 102 |
) |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
renv_watchdog_server_exit <- function(server, client, lockenv) { |
| 107 |
|
| 108 |
# remove any existing locks |
| 109 |
locks <- ls(envir = lockenv, all.names = TRUE) |
| 110 |
unlink(locks, recursive = TRUE, force = TRUE) |
| 111 |
|
| 112 |
# shut down the socket server |
| 113 |
close(server$socket) |
| 114 |
|
| 115 |
# quit |
| 116 |
quit(status = 0) |
| 117 |
|
| 118 |
} |