Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ElianHugh committed Jun 19, 2024
1 parent e9e5cee commit afa31be
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 39 deletions.
2 changes: 1 addition & 1 deletion R/cli.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cli_goodbye <- function() {
}

cli_watching_directory <- function(engine) {
dirs <- c(engine$config$path, engine$config$dirs)
dirs <- c(engine$config$entry_dir, engine$config$dirs)
cli::cli_inform("Watching {.file {dirs}} for changes...")
}

Expand Down
35 changes: 25 additions & 10 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ new_config <- function(...) {
"127.0.0.1"
port <- dots$port %||%
plumber::get_option_or_env("plumber.port") %||%
httpuv::randomPort(
host = host,
n = 100L
)
new_port(host = host)
ignore <- dots$ignore %||%
utils::glob2rx(
paste0(
Expand All @@ -23,22 +20,24 @@ new_config <- function(...) {

structure(
list(
plumber_path = dots$path,
path = dirname(dots$path),
entry_path = dots$path,
entry_dir = dirname(dots$path),
dirs = dots$dirs,
host = host,
port = port,
socket_port = httpuv::randomPort(),
socket_port = new_port(
used = port,
host = host
),
ignore = ignore
),
class = c("hotwater_config", "list")

)
}

validate_config <- function(config) {
if (!file.exists(config$plumber_path) || dir.exists(config$plumber_path)) {
error_invalid_path(config$plumber_path)
if (!file.exists(config$entry_path) || dir.exists(config$entry_path)) {
error_invalid_path(config$entry_path)
}

if (!is.null(config$dirs) && any(!dir.exists(config$dirs))) {
Expand All @@ -53,4 +52,20 @@ validate_config <- function(config) {
if (is.numeric(config$host)) {
error_invalid_host(config$host)
}
}

#' it's possible to duplicate the port when it isn't immediately used
#' this just makes sure we end up with a different number...
#' @noRd
new_port <- function(used, host = "127.0.0.1") {
out <- NULL
if (missing(used)) {
out <- httpuv::randomPort(host = host)
} else {
repeat {
out <- httpuv::randomPort(host = host)
if (out != used) break
}
}
out
}
9 changes: 6 additions & 3 deletions R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ run_engine <- function(engine) {

current_state <- directory_state(
c(
engine$config$path,
engine$config$entry_dir,
engine$config$dirs
),
engine$config$ignore
)

repeat {
poll_runner(engine)
current_state <- examine_directory_change(
current_state <- watch_directory(
engine,
current_state,
callback
Expand All @@ -51,7 +51,10 @@ run_engine <- function(engine) {
}

kill_engine <- function(engine) {
nanonext::reap(engine$publisher)
# technically, if we are killing the engine,
# we don't want to close the
# publlisher because we might reuse it
#close(engine$publisher)
kill_runner(engine)
}

Expand Down
2 changes: 1 addition & 1 deletion R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ run <- function(path, dirs = NULL, port = NULL, host = NULL, ignore = NULL) {

should_reuse_engine <- function(old_config, config) {
old_exists <- !is.null(old_config)
same_path <- identical(old_config$plumber_path, config$plumber_path)
same_path <- identical(old_config$entry_path, config$entry_path)
same_dirs <- identical(old_config$dirs, config$dirs)
same_port <- identical(old_config$port, config$port) || is.null(config$port)
same_host <- identical(old_config$host, config$host) || is.null(config$host)
Expand Down
6 changes: 2 additions & 4 deletions R/runner.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ new_runner <- function(engine) {
}
plumber::pr(path) |>
middleware() |>
# todo, try to leverage plumber defaults,
# likely in the upcoming config
plumber::pr_run(
port = port,
host = host,
Expand All @@ -19,10 +17,10 @@ new_runner <- function(engine) {
error = "error",
args = list(
port = engine$config$port,
path = engine$config$plumber_path,
path = engine$config$entry_path,
host = engine$config$host,
middleware = middleware(engine),
mod = file.path(getwd(), engine$config$plumber_path)
mod = file.path(getwd(), engine$config$entry_path)
),
supervise = TRUE
)
Expand Down
5 changes: 2 additions & 3 deletions R/watcher.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# todo, rename this...
examine_directory_change <- function(engine, current_state, callback) {
watch_directory <- function(engine, current_state, callback) {
paths <- c(
engine$config$path,
engine$config$entry_dir,
engine$config$dirs
)
next_state <- directory_state(paths, engine$config$ignore)
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<!-- @format -->

# hotwater

<!-- badges: start -->

[![Codecov test
coverage](https://codecov.io/gh/ElianHugh/hotwater/branch/main/graph/badge.svg)](https://app.codecov.io/gh/ElianHugh/hotwater?branch=main)
[![R-CMD-check](https://github.com/ElianHugh/hotwater/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ElianHugh/hotwater/actions/workflows/R-CMD-check.yaml)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
<!-- badges: end -->
[![Codecov test coverage](https://codecov.io/gh/ElianHugh/hotwater/branch/main/graph/badge.svg)](https://app.codecov.io/gh/ElianHugh/hotwater?branch=main)
[![R-CMD-check](https://github.com/ElianHugh/hotwater/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ElianHugh/hotwater/actions/workflows/R-CMD-check.yaml)

*work in progress*
<!-- badges: end -->

- for plumber development
- autoreload for plumber
- also auto-refreshes the browser when a change is made
- run from the commandline with the `/exec/hotwater` bash script
- for plumber development
- autoreload for plumber
- also auto-refreshes the browser when a change is made
- run from the commandline with the `/exec/hotwater` bash script

## Installation

You can install the development version of hotwater from
[GitHub](https://github.com/) with:

``` r
```r
# install.packages("devtools")
devtools::install_github("ElianHugh/hotwater")
```
Expand All @@ -29,14 +29,14 @@ devtools::install_github("ElianHugh/hotwater")

Hotwater can be run via an R session:

``` r
```r
hotwater::run(
system.file("examples", "plumber.R", package = "hotwater"),
port = 9999L
)
```

``` r
```r
Server running on <127.0.0.1:9999> [17ms]
Watching ./path/to/ for changes...
```
Expand All @@ -47,7 +47,7 @@ or a terminal using the bash script:
hotwater -f my/plumber/api.R -p 9999
```

``` r
```r
Server running on <127.0.0.1:9999> [17ms]
Watching ./path/to/ for changes...
```
2 changes: 1 addition & 1 deletion man/run.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testthat/test-cli.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that("startup/teardown messages don't error", {
engine <- new_engine(
config = new_config(
path = system.file("examples", "plumber.R", package = "hotwater")
)
)
expect_no_error(
suppressMessages(
buildup_engine(engine)
)
)
expect_no_error(
suppressMessages(
teardown_engine(engine)
)
)
})
15 changes: 14 additions & 1 deletion tests/testthat/test-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ test_that("engine reuse", {
old = engine$config,
new_config(
dirs = engine$config$dirs,
path = engine$config$plumber_path,
path = engine$config$entry_path,
port = engine$config$port,
host = engine$config$host,
ignore = engine$config$ignore
)
)
)
})

test_that("can kill engine", {
engine <- new_engine(
config = new_config(
path = system.file("examples", "plumber.R", package = "hotwater")
)
)
new_runner(engine)
kill_engine(engine)

expect_false(engine$runner$is_alive())
# expect_true(engine$publisher$state == "closed")
})
4 changes: 2 additions & 2 deletions tests/testthat/test-middleware.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test_that("middleware injection works with filters", {
)
runner <- suppressMessages(callr::r_bg(
function(config, middleware_filter) {
plumber::pr(config$plumber_path) |>
plumber::pr(config$entry_path) |>
plumber::pr_filter("foo", function(req, res) {
stop("I break things")
}) |>
Expand Down Expand Up @@ -59,7 +59,7 @@ test_that("is_plumber_running works", {
)
router <- suppressMessages(callr::r_bg(
function(config) {
plumber::pr(config$plumber_path) |>
plumber::pr(config$entry_path) |>
plumber::pr_get(
"/__hotwater__",
function() "running",
Expand Down

0 comments on commit afa31be

Please sign in to comment.