Skip to content

Commit

Permalink
Switch to mirai
Browse files Browse the repository at this point in the history
- Drop callr for mirai
- Remove remotes from CLI suggests script
  • Loading branch information
ElianHugh committed Jun 23, 2024
1 parent 892abad commit 6bfe2be
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ linters: linters_with_defaults(
line_length_linter(120),
implicit_integer_linter(),
indentation_linter(indent = 4L),
object_usage_linter = NULL,
object_name_linter(styles = c("snake_case", "symbols"), regexes = character()),
object_name_linter = NULL
)
exclusions: list("man/", "inst/", "src/", ".vscode/", ".Rproj.user/", "R/import-standalone-obj-type.R", "R/import-standalone-types-check.R")
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ License: GPL (>= 3)
URL: https://github.com/ElianHugh/hotwater
BugReports: https://github.com/ElianHugh/hotwater/issues
Imports:
callr,
cli,
httpuv,
httr2,
nanonext,
mirai,
plumber,
utils
Suggests:
Expand Down
3 changes: 2 additions & 1 deletion R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ new_config <- function(...) {
used = port,
host = host
),
ignore = ignore
ignore = ignore,
runner_compute = "hotwater_runner"
),
class = c("hotwater_config", "list")
)
Expand Down
3 changes: 1 addition & 2 deletions R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ run_engine <- function(engine) {
)

repeat {
poll_runner(engine)
Sys.sleep(0.05)
current_state <- watch_directory(
engine,
current_state,
Expand All @@ -60,7 +60,6 @@ buildup_engine <- function(engine) {
if (!res) {
cli::cli_progress_done(result = "failed")
} else {
poll_runner(engine)
publish_browser_reload(engine)
cli::cli_progress_done()
}
Expand Down
61 changes: 61 additions & 0 deletions R/mirai.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
new_runner <- function(engine) {
mirai::daemons(
n = 1L,
dispatcher = FALSE,
resilience = FALSE,
autoexit = get_kill_signal(),
output = TRUE,
.compute = engine$config$runner_compute
)
engine$runner <- mirai::mirai(
{
if (requireNamespace("box", quietly = TRUE)) {
box::set_script_path(mod) # nolint: object_usage_linter.
}
plumber::pr(path) |> # nolint: object_usage_linter.
middleware() |>
plumber::pr_run(
port = port, # nolint: object_usage_linter.
host = host, # nolint: object_usage_linter.
quiet = TRUE,
debug = TRUE
)
},
port = engine$config$port,
path = engine$config$entry_path,
host = engine$config$host,
middleware = middleware(engine),
mod = file.path(getwd(), engine$config$entry_path),
.compute = engine$config$runner_compute
)

i <- 0L
timeout <- 1000L

while (i < timeout && is_runner_alive(engine) && !is_plumber_running(engine)) {
i <- i + 1L
try(cli::cli_progress_update(.envir = parent.frame(n = 1L)), silent = TRUE)
Sys.sleep(0.1)
}

if (i == timeout && !is_plumber_running(engine)) {
return(FALSE)
}

TRUE
}

kill_runner <- function(engine) {
mirai::daemons(0L, .compute = engine$config$runner_compute)
!is_runner_alive(engine$runner)
}

is_runner_alive <- function(engine) {
mirai::unresolved(engine$runner)
}

get_kill_signal <- function() {
tools::SIGKILL %|NA|%
tools::SIGTERM %|NA|%
tools::SIGINT
}
82 changes: 0 additions & 82 deletions R/runner.R

This file was deleted.

2 changes: 1 addition & 1 deletion R/script.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ uninstall_hotwater <- function(install_folder = "~/.local/bin/") {
#'
#' @noRd
check_suggests <- function() {
suggests <- c("docopt", "remotes")
suggests <- c("docopt")
lapply(
suggests,
function(suggestion) {
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
`%||%` <- function(x, y) {
if (is.null(x)) y else x
}

`%|NA|%` <- function(x, y) {
if (is.na(x)) y else x
}
19 changes: 9 additions & 10 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
# TODO

## 2
## 1

- Cannot pass a plumber router to hotwater

## 3
## 2

- It would be nice to remove callr dependency and just rely on mirai/nanonext
- Updating CSS shouldn't typically cause a refresh

## 6
## 3

- Updating CSS shouldn't typically cause a refresh
- Look at using NNG js side for more advances msg passing

## 7
## 4

- Nanonext is currently only used because I want an async connection for the local webpage refresh. May not need to use nanonext, considering we fake the protocol on the JS side of things anyway
- The CLI messages are a bit all over the place, and errors don't always cause the progress bar to fail

## 8
## 5

- The CLI messages are a bit all over the place, and errors don't always cause
the progress bar to fail
- Fix some global binding notes
6 changes: 2 additions & 4 deletions tests/testthat/test-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ test_that("engine reuse", {
)
)
)

cleanup_test_engine(engine)
})

test_that("can kill engine", {
engine <- new_test_engine()
new_runner(engine)
kill_engine(engine)
expect_false(engine$runner$is_alive())

expect_false(is_runner_alive(engine))
cleanup_test_engine(engine)
})
})
56 changes: 29 additions & 27 deletions tests/testthat/test-middleware.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,29 @@ test_that("middleware injection works", {

test_that("middleware injection works with filters", {
engine <- new_test_engine()
runner <- suppressMessages(callr::r_bg(
function(config, middleware_filter) {
plumber::pr(config$entry_path) |>
plumber::pr_filter("foo", function(req, res) {
stop("I break things")
}) |>
middleware_filter() |>
plumber::pr_run(port = config$port)
},
args = list(
runner <- suppressMessages(
mirai::mirai(
{
plumber::pr(config$entry_path) |>
plumber::pr_filter("foo", function(req, res) {
stop("I break things")
}) |>
middleware_filter() |>
plumber::pr_run(port = config$port)
},
config = engine$config,
middleware_filter = middleware(engine)
middleware_filter = middleware(engine),
.compute = engine$config$runner_compute
)
))
)

i <- 1L
while (i < 20L && !is_plumber_running(engine)) {
i <- i + 1L
Sys.sleep(0.5)
}

resp <- httr2::request(sprintf("localhost:%s/__hotwater__", engine$config$port)) |>
resp <- httr2::request(sprintf("localhost:%s/__hotwater__", engine$config$port)) |>
httr2::req_perform() |>
httr2::resp_status()

Expand All @@ -49,26 +50,27 @@ test_that("middleware injection works with filters", {

test_that("is_plumber_running works", {
engine <- new_test_engine()
router <- suppressMessages(callr::r_bg(
function(config) {
plumber::pr(config$entry_path) |>
plumber::pr_get(
"/__hotwater__",
function() "running",
serializer = plumber::serializer_text()
) |>
plumber::pr_run(port = config$port)
},
args = list(
config = engine$config
router <- suppressMessages(
mirai::mirai(
{
plumber::pr(config$entry_path) |>
plumber::pr_get(
"/__hotwater__",
function() "running",
serializer = plumber::serializer_text()
) |>
plumber::pr_run(port = config$port)
},
config = engine$config,
.compute = engine$config$runner_compute
)
))
)
i <- 1L
while (i < 20L && !is_plumber_running(engine)) {
i <- i + 1L
Sys.sleep(0.5)
}
router$kill_tree()
kill_runner(engine)
expect_lt(i, 20L, label = "loop iterations")
cleanup_test_engine(engine)
})
Expand Down

0 comments on commit 6bfe2be

Please sign in to comment.