Skip to content

Commit 2de74ee

Browse files
committed
Support non-blocking (de)registration in the latest SewerRat API.
1 parent d5e0440 commit 2de74ee

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

R/deregister.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#'
1111
#' @return On success, the directory is deregistered and NULL is invisibly returned.
1212
#'
13+
#' If \code{block=FALSE}, the function returns before confirmation of successful deregistration from the SewerRat API.
14+
#' This can be useful for asynchronous processing of directories with many files.
15+
#'
1316
#' @author Aaron Lun
1417
#'
1518
#' @examples
@@ -27,12 +30,12 @@
2730
#' query("bar", url=info$url)
2831
#' @export
2932
#' @import httr2
30-
deregister <- function(dir, url, retry=3, wait=1) {
33+
deregister <- function(dir, url, retry=3, wait=1, block=TRUE) {
3134
dir <- clean_path(dir)
3235

3336
req <- request(paste0(url, "/deregister/start"))
3437
req <- req_method(req, "POST")
35-
req <- req_body_json(req, list(path=dir))
38+
req <- req_body_json(req, list(path=dir, block=block))
3639
req <- redirect_post(req)
3740
req <- handle_error(req)
3841
res <- req_perform(req)
@@ -45,7 +48,7 @@ deregister <- function(dir, url, retry=3, wait=1) {
4548

4649
req <- request(paste0(url, "/deregister/finish"))
4750
req <- req_method(req, "POST")
48-
req <- req_body_json(req, list(path=dir))
51+
req <- req_body_json(req, list(path=dir, block=block))
4952
req <- redirect_post(req)
5053
req <- handle_error(req)
5154
res <- req_perform(req)

R/register.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
#' @param names Character vector containing the base names of the metadata files.
99
#' @param url String containing the URL to the SewerRat REST API.
1010
#' @param retry,wait Deprecated arguments, ignored.
11+
#' @param block Logical scalar indicating whether to block on successful registration.
1112
#'
1213
#' @return On success, the directory is registered and NULL is invisibly returned.
1314
#' A warning is raised if any particular metadata file cannot be indexed.
1415
#'
16+
#' If \code{block=FALSE}, the function returns before confirmation of successful registration from the SewerRat API.
17+
#' This can be useful for asynchronous processing of directories with many files.
18+
#'
1519
#' @author Aaron Lun
1620
#'
1721
#' @examples
@@ -28,7 +32,7 @@
2832
#' query("Aaron", url=info$url)
2933
#' @export
3034
#' @import httr2
31-
register <- function(dir, names, url, retry=3, wait=1) {
35+
register <- function(dir, names, url, retry=3, wait=1, block=TRUE) {
3236
dir <- clean_path(dir)
3337
stopifnot(length(names) > 0)
3438

@@ -46,7 +50,7 @@ register <- function(dir, names, url, retry=3, wait=1) {
4650

4751
req <- request(paste0(url, "/register/finish"))
4852
req <- req_method(req, "POST")
49-
req <- req_body_json(req, list(path=dir, base=as.list(names)))
53+
req <- req_body_json(req, list(path=dir, base=as.list(names), block=block))
5054
req <- redirect_post(req)
5155
req <- handle_error(req)
5256
res <- req_perform(req)

R/startSewerRat.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#' startSewerRat() # initialize a new instance.
2626
#'
2727
#' @export
28-
startSewerRat <- function(db=tempfile(fileext=".sqlite3"), port=NULL, wait = 1, version = "1.1.0", overwrite = FALSE) {
28+
startSewerRat <- function(db=tempfile(fileext=".sqlite3"), port=NULL, wait = 1, version = "1.1.1", overwrite = FALSE) {
2929
if (!is.null(running$active)) {
3030
return(list(new=FALSE, port=running$port, url=assemble_url(running$port)))
3131
}

man/deregister.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/register.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/startSewerRat.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-register.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,29 @@ test_that("(de)registration works as expected", {
1616
expect_identical(length(res), 0L)
1717
})
1818

19+
test_that("(de)registration works as expected when delayed", {
20+
res <- query("aaron", url=info$url)
21+
expect_identical(length(res), 0L)
22+
23+
register(mydir, "metadata.json", url=info$url, block=FALSE)
24+
for (x in seq_len(10)) {
25+
Sys.sleep(0.1)
26+
res <- query("aaron", url=info$url)
27+
if (length(res) == 1L) {
28+
break
29+
}
30+
}
31+
expect_identical(length(res), 1L)
32+
33+
deregister(mydir, url=info$url, block=FALSE)
34+
for (x in seq_len(10)) {
35+
Sys.sleep(0.1)
36+
res <- query("aaron", url=info$url)
37+
if (length(res) == 0L) {
38+
break
39+
}
40+
}
41+
expect_identical(length(res), 0L)
42+
})
43+
1944
})()

0 commit comments

Comments
 (0)