Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curl for application #58

Open
schloerke opened this issue Mar 2, 2018 · 4 comments
Open

Curl for application #58

schloerke opened this issue Mar 2, 2018 · 4 comments

Comments

@schloerke
Copy link
Collaborator

Rather than waiting the standard 0.5, should we do something like the code instead?

while(!RCurl::url.exists("urltoshinyapp")) {
  Sys.sleep(0.1)
}

I would eliminate the race case, but would introduce a dependency of some kind.

@wch
Copy link
Owner

wch commented Mar 2, 2018

That sounds like a good idea. Do you know how url.exists works? Does it send a HEAD or GET request?

The curl package would be preferable to RCurl, but if it can be done with base R, that would be even better.

@schloerke
Copy link
Collaborator Author

I found this site which had some code for base R only and altered it to work with our example below. Theoretically, it should reach a count of 50.

http://memosisland.blogspot.com/2012/03/check-url-existance-with-r.html

url_exists <- function(address) {
  tryCatch({
    con <- url(address)
    on.exit({
      close(con)
    })
    capture.output(suppressWarnings(readLines(con)))
    TRUE
  }, error = function(err) {
    occur <- grep("cannot open the connection", capture.output(err))
    if (length(occur) > 0) {
      FALSE
    } else {
      NULL
    }
  })
}



# doesn't exist, return NULL
url_exists("cnn.com")
#> NULL
# exists, TRUE
url_exists("http://cnn.com")
#> [1] TRUE


port <- webshot:::available_port()

p <- callr::r_bg(function(...) {
  Sys.sleep(5)  # long time to start up
  shiny::runApp(...)
}, args = list(appDir = system.file("examples", "01_hello", package = "shiny"), 
  port = port, display.mode = "normal"))

counter <- 0
while (!isTRUE(url_exists(paste0("http://127.0.0.1:", port)))) {
  cat("Counter: ", counter, " - ", as.character(Sys.time()), "\n")
  counter <- counter + 1
  Sys.sleep(0.1)
}
#> Warning in as.POSIXlt.POSIXct(x, tz): unknown timezone 'zone/tz/2018c.1.0/
#> zoneinfo/America/New_York'
#> Counter:  0  -  2018-03-02 15:02:17 
#> Counter:  1  -  2018-03-02 15:02:18 
#> Counter:  2  -  2018-03-02 15:02:18 
#> Counter:  3  -  2018-03-02 15:02:18 
#> Counter:  4  -  2018-03-02 15:02:18 
#> Counter:  5  -  2018-03-02 15:02:18 
#> Counter:  6  -  2018-03-02 15:02:19 
#> Counter:  7  -  2018-03-02 15:02:19 
#> Counter:  8  -  2018-03-02 15:02:19 
#> Counter:  9  -  2018-03-02 15:02:19 
#> Counter:  10  -  2018-03-02 15:02:19 
#> Counter:  11  -  2018-03-02 15:02:20 
#> Counter:  12  -  2018-03-02 15:02:20 
#> Counter:  13  -  2018-03-02 15:02:20 
#> Counter:  14  -  2018-03-02 15:02:20 
#> Counter:  15  -  2018-03-02 15:02:20 
#> Counter:  16  -  2018-03-02 15:02:21 
#> Counter:  17  -  2018-03-02 15:02:21 
#> Counter:  18  -  2018-03-02 15:02:21 
#> Counter:  19  -  2018-03-02 15:02:21 
#> Counter:  20  -  2018-03-02 15:02:21 
#> Counter:  21  -  2018-03-02 15:02:22 
#> Counter:  22  -  2018-03-02 15:02:22 
#> Counter:  23  -  2018-03-02 15:02:22 
#> Counter:  24  -  2018-03-02 15:02:22 
#> Counter:  25  -  2018-03-02 15:02:22 
#> Counter:  26  -  2018-03-02 15:02:23

cat("Found it!")
#> Found it!
p$kill()
#> [1] TRUE



# Again with no start up time
p <- callr::r_bg(function(...) {
  # No startup time
  shiny::runApp(...)
}, args = list(appDir = system.file("examples", "01_hello", package = "shiny"), 
  port = port, display.mode = "normal"))
counter <- 0
while (!isTRUE(url_exists(paste0("http://127.0.0.1:", port)))) {
  cat("Counter: ", counter, " - ", as.character(Sys.time()), "\n")
  counter <- counter + 1
  Sys.sleep(0.1)
}
#> Counter:  0  -  2018-03-02 15:02:23 
#> Counter:  1  -  2018-03-02 15:02:24

cat("Found it again!")
#> Found it again!
p$kill()
#> [1] TRUE

@yihui
Copy link
Collaborator

yihui commented Mar 2, 2018

Basically I just used readLines() to check if a URL exists: https://github.com/rstudio/blogdown/blob/051efcacf/R/serve.R#L40-L44 Not sure if it is robust but I guess it should work reasonably well.

@schloerke
Copy link
Collaborator Author

Example of waiting in action... (The bottom two examples work successfully)

# development version
library(webshot)

appdir <- system.file("examples", "01_hello", package = "shiny")
shinyapp <- shiny::shinyAppDir(appdir)

options(webshot.app.timeout = 0.2)
appshot(appdir, "01_hello.png")
#> waiting...
#> Error: It took more than 0.2 seconds to launch the server. There may be something wrong. The process has been killed. If the app needs more time to be launched, set options(webshot.app.timeout) to a larger value.
appshot(shinyapp, "01_hello.png")
#> Loading required package: shiny
#> 
#> Listening on http://127.0.0.1:6813
#> Error: It took more than 0.2 seconds to launch the server. There may be something wrong. The process has been killed. If the app needs more time to be launched, set options(webshot.app.timeout) to a larger value.

options(webshot.app.timeout = 60)
appshot(appdir, "01_hello.png")
#> waiting...
#> waiting...
#> Found!
appshot(shinyapp, "01_hello.png")
#> 
#> Listening on http://127.0.0.1:3586

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants