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

hanging on local app wait_for_shiny_ready when headless #6

Open
yonicd opened this issue Sep 29, 2020 · 5 comments
Open

hanging on local app wait_for_shiny_ready when headless #6

yonicd opened this issue Sep 29, 2020 · 5 comments

Comments

@yonicd
Copy link
Collaborator

yonicd commented Sep 29, 2020

local:

> test <- crrry::CrrryProc$new(
   chrome_bin = pagedown::find_chrome(),
   chrome_port = 6697L,
   fun = crrry_args(
     shiny_dir = system.file('examples/bad_app.R',package = 'reactor')
     ),
   headless = TRUE
 )
# Running \
#   '/Applications/Google http://Chrome.app/Contents/MacOS/Google Chrome' \
#   --no-first-run --headless \
#   '--user-data-dir=/Users/yonis/Library/Application Support/r-crrri/chrome-data-dir-obuysdjo' \
#   '--remote-debugging-port=6697'

test$wait_for_shiny_ready()
# Shiny is computing
crrry_args <- function(test_dir = tempdir(), shiny_dir){
  
  glue::glue(
    'pkgload::load_all()',
    'library(whereami)',
    "whereami_dir <- file.path('{test_dir}','reactor')",
    'if(!dir.exists(whereami_dir)) dir.create(whereami_dir,showWarnings = FALSE)',
    'whereami::set_whereami_log(whereami_dir)',
    "shiny::runApp(appDir = '{shiny_dir}')",
    .sep = ";"
    )  

} 

remote:

> test <- crrry::CrrryOnPage$new(
   chrome_bin = pagedown::find_chrome(),
   chrome_port = httpuv::randomPort(),
   url = "https://connect.thinkr.fr/prenoms/",
   headless = TRUE
 )
# Running \
#   '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' \
#   --no-first-run --headless \
#   '--user-data-dir=/Users/yonis/Library/Application Support/r-crrri/chrome-data-dir-nyswbsmz' \
#   '--remote-debugging-port=5881'
test$wait_for_shiny_ready()
# Shiny is computing
# ✓ Shiny is still running
test$call_js(
  '$("#mod_popuui-dep").click()'
)
# ── Launching JS: $("#mod_popuui-dep").click() ─────────────────────────
# Shiny is computing
# ✓ Shiny is still running
test$stop()

Chrome Version 85.0.4183.121 (Official Build) beta (64-bit)

session info
Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       macOS Catalina 10.15.6      
 system   x86_64, darwin15.6.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2020-09-29Packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date       lib source                           
 askpass       1.1        2019-01-13 [1] CRAN (R 3.6.0)                   
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                   
 attempt       0.3.1      2020-05-03 [1] CRAN (R 3.6.2)                   
 backports     1.1.10     2020-09-15 [1] CRAN (R 3.6.2)                   
 binman        0.1.1      2018-07-18 [1] CRAN (R 3.6.0)                   
 bitops        1.0-6      2013-08-17 [1] CRAN (R 3.6.0)                   
 caTools       1.18.0     2020-01-17 [1] CRAN (R 3.6.0)                   
 cli           2.0.2      2020-02-28 [1] CRAN (R 3.6.0)                   
 clipr         0.7.0      2019-07-23 [1] CRAN (R 3.6.0)                   
 crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                   
 crrri         0.0.13     2020-09-28 [1] Github (rlesur/crrri@8cc6ce1)    
 crrry       * 0.0.0.9001 2020-09-29 [1] local                            
 curl          4.3        2019-12-02 [1] CRAN (R 3.6.0)                   
 debugme       1.1.0      2017-10-22 [1] CRAN (R 3.6.0)                   
 desc          1.2.0.9000 2020-09-28 [1] Github (r-lib/desc@c175259)      
 details       0.2.1      2020-01-12 [1] local                            
 digest        0.6.25     2020-02-23 [1] CRAN (R 3.6.0)                   
 fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.0)                   
 fastmap       1.0.1      2019-10-08 [1] CRAN (R 3.6.0)                   
 glue          1.4.2      2020-08-27 [1] CRAN (R 3.6.2)                   
 htmltools     0.5.0      2020-06-16 [1] CRAN (R 3.6.2)                   
 httpuv        1.5.4      2020-06-06 [1] CRAN (R 3.6.2)                   
 httr          1.4.2      2020-07-20 [1] CRAN (R 3.6.2)                   
 jsonlite      1.7.1      2020-09-07 [1] CRAN (R 3.6.2)                   
 knitr         1.30       2020-09-22 [1] CRAN (R 3.6.2)                   
 later         1.1.0.9000 2020-09-28 [1] Github (r-lib/later@56269c2)     
 magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)                   
 mime          0.9        2020-02-04 [1] CRAN (R 3.6.0)                   
 openssl       1.4.3      2020-09-18 [1] CRAN (R 3.6.2)                   
 packrat       0.5.0      2018-11-14 [1] CRAN (R 3.6.0)                   
 pagedown      0.9        2020-03-18 [1] CRAN (R 3.6.0)                   
 png           0.1-7      2013-12-03 [1] CRAN (R 3.6.0)                   
 processx      3.4.4      2020-09-03 [1] CRAN (R 3.6.2)                   
 promises      1.1.1.9000 2020-09-28 [1] Github (rstudio/promises@3d54b42)
 ps            1.3.4      2020-08-11 [1] CRAN (R 3.6.2)                   
 purrr         0.3.4      2020-04-17 [1] CRAN (R 3.6.2)                   
 R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.0)                   
 rappdirs      0.3.1      2016-03-28 [1] CRAN (R 3.6.0)                   
 Rcpp          1.0.5      2020-07-06 [1] CRAN (R 3.6.2)                   
 reactor       0.0.8      2020-09-28 [1] local                            
 rlang         0.4.7      2020-07-09 [1] CRAN (R 3.6.2)                   
 rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)                   
 RSelenium     1.7.7      2020-02-03 [1] CRAN (R 3.6.1)                   
 rstudioapi    0.11       2020-02-07 [1] CRAN (R 3.6.1)                   
 semver        0.2.0      2017-01-06 [1] CRAN (R 3.6.0)                   
 sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                   
 shiny         1.4.0.2    2020-03-13 [1] CRAN (R 3.6.0)                   
 testthat      2.3.2      2020-03-02 [1] CRAN (R 3.6.0)                   
 wdman         0.2.5      2020-01-31 [1] CRAN (R 3.6.0)                   
 websocket     1.3.1      2020-07-13 [1] CRAN (R 3.6.2)                   
 whereami      0.1.9      2020-09-24 [1] local                            
 withr         2.3.0      2020-09-22 [1] CRAN (R 3.6.2)                   
 xfun          0.17       2020-09-09 [1] CRAN (R 3.6.2)                   
 XML           3.98-1.20  2019-06-06 [1] CRAN (R 3.6.0)                   
 xml2          1.3.0      2020-04-01 [1] CRAN (R 3.6.2)                   
 xtable        1.8-4      2019-04-21 [1] CRAN (R 3.6.0)                   

[1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

@yonicd yonicd changed the title hanging on local app wait_for_shiny_ready hanging on local app wait_for_shiny_ready when headless Sep 29, 2020
@ColinFay
Copy link
Owner

Is it still an issue?

@yonicd
Copy link
Collaborator Author

yonicd commented Oct 14, 2020

no. this seems to have been resolved. thnx

@yonicd yonicd closed this as completed Oct 14, 2020
@yonicd yonicd reopened this Dec 12, 2020
@yonicd
Copy link
Collaborator Author

yonicd commented Dec 12, 2020

this is happening again

  test <- crrry::CrrryProc$new(
    chrome_bin = pagedown::find_chrome(),
    shiny_port = httpuv::randomPort(),
    chrome_port = httpuv::randomPort(),
    fun = "puzzlemath::run_app()",
    pre_launch_cmd = glue::glue("whereami::set_whereami_log('{tempdir()}')"),
    headless = TRUE
  )
  
  test$wait_for_shiny_ready()
session info
Session info ────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       macOS Catalina 10.15.7      
 system   x86_64, darwin15.6.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2020-12-11Packages ────────────────────────────────────────────────────────────────────
 package     * version    date       lib source                           
 askpass       1.1        2019-01-13 [1] CRAN (R 3.6.0)                   
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                   
 attempt       0.3.1      2020-05-03 [1] CRAN (R 3.6.2)                   
 binman        0.1.1      2018-07-18 [1] CRAN (R 3.6.0)                   
 bitops        1.0-6      2013-08-17 [1] CRAN (R 3.6.0)                   
 caTools       1.18.0     2020-01-17 [1] CRAN (R 3.6.0)                   
 cli           2.2.0      2020-11-20 [1] CRAN (R 3.6.2)                   
 clipr         0.7.1      2020-10-08 [1] CRAN (R 3.6.2)                   
 crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                   
 crrri         0.0.13     2020-09-28 [1] Github (rlesur/crrri@8cc6ce1)    
 crrry         0.0.0.9001 2020-12-08 [1] Github (colinfay/crrry@cb56e76)  
 curl          4.3        2019-12-02 [1] CRAN (R 3.6.0)                   
 debugme       1.1.0      2017-10-22 [1] CRAN (R 3.6.0)                   
 desc          1.2.0.9000 2020-09-28 [1] Github (r-lib/desc@c175259)      
 details       0.2.1      2020-01-12 [1] local                            
 digest        0.6.27     2020-10-24 [1] CRAN (R 3.6.2)                   
 fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.0)                   
 fastmap       1.0.1      2019-10-08 [1] CRAN (R 3.6.0)                   
 glue          1.4.2      2020-08-27 [1] CRAN (R 3.6.2)                   
 here          1.0.0.9000 2020-11-25 [1] Github (r-lib/here@fdd23b1)      
 htmltools     0.5.0      2020-06-16 [1] CRAN (R 3.6.2)                   
 httpuv        1.5.4      2020-06-06 [1] CRAN (R 3.6.2)                   
 httr          1.4.2      2020-07-20 [1] CRAN (R 3.6.2)                   
 jsonlite      1.7.1      2020-09-07 [1] CRAN (R 3.6.2)                   
 knitr         1.30       2020-09-22 [1] CRAN (R 3.6.2)                   
 later         1.1.0.9000 2020-11-25 [1] Github (r-lib/later@0cfd3b5)     
 magrittr      2.0.1      2020-11-17 [1] CRAN (R 3.6.2)                   
 mime          0.9        2020-02-04 [1] CRAN (R 3.6.0)                   
 openssl       1.4.3      2020-09-18 [1] CRAN (R 3.6.2)                   
 pagedown      0.9        2020-03-18 [1] CRAN (R 3.6.0)                   
 png           0.1-7      2013-12-03 [1] CRAN (R 3.6.0)                   
 processx      3.4.5      2020-11-30 [1] CRAN (R 3.6.2)                   
 promises      1.1.1.9001 2020-11-25 [1] Github (rstudio/promises@bbadb3d)
 ps            1.5.0      2020-12-05 [1] CRAN (R 3.6.2)                   
 purrr         0.3.4      2020-04-17 [1] CRAN (R 3.6.2)                   
 R6            2.5.0      2020-10-28 [1] CRAN (R 3.6.2)                   
 rappdirs      0.3.1      2016-03-28 [1] CRAN (R 3.6.0)                   
 Rcpp          1.0.5      2020-07-06 [1] CRAN (R 3.6.2)                   
 reactor       0.0.8      2020-12-08 [1] local                            
 rlang         0.4.9      2020-11-26 [1] CRAN (R 3.6.2)                   
 rprojroot     2.0.2      2020-11-25 [1] Github (r-lib/rprojroot@5bafca9) 
 RSelenium     1.7.7      2020-02-03 [1] CRAN (R 3.6.1)                   
 rstudioapi    0.13       2020-11-12 [1] CRAN (R 3.6.2)                   
 semver        0.2.0      2017-01-06 [1] CRAN (R 3.6.0)                   
 sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                   
 shiny         1.5.0      2020-06-23 [1] CRAN (R 3.6.2)                   
 shinyjs       1.0        2018-01-08 [1] CRAN (R 3.6.0)                   
 testthat      3.0.0      2020-10-31 [1] CRAN (R 3.6.2)                   
 wdman         0.2.5      2020-01-31 [1] CRAN (R 3.6.0)                   
 websocket     1.3.1      2020-07-13 [1] CRAN (R 3.6.2)                   
 whereami      0.1.9      2020-09-24 [1] local                            
 withr         2.3.0      2020-09-22 [1] CRAN (R 3.6.2)                   
 xfun          0.19       2020-10-30 [1] CRAN (R 3.6.2)                   
 XML           3.98-1.20  2019-06-06 [1] CRAN (R 3.6.0)                   
 xml2          1.3.2      2020-04-23 [1] CRAN (R 3.6.2)                   
 xtable        1.8-4      2019-04-21 [1] CRAN (R 3.6.0)                   

[1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

@DivadNojnarg
Copy link

@yonicd : were you able to solve it? It got the same issue. It is running well locally but get stuck on GitLab CI/CD.

@DivadNojnarg
Copy link

I believe part of the problem is here:https://github.com/ColinFay/crrry/blob/cb56e7656ec0d231c2aeccba5bf66180c33ccb50/R/launch.R#L240
I realised that most of the time, the process starts, chrome immediately tries to connect to the app but the app is not available yet. You can check this by browsing to the Chrome debug port: the web inspector will show a blank page. The result is a sort of messed-up state where the code is stuck. If you add a delay between process start and chrome connect, the issue vanishes.

Below is the hacked R6 class I am using:

CrrryProcCustom <- R6::R6Class(
  "CrrryProcCustom",
  inherit = crrry::CrrryProc,
  public = list(
    stop = function() {
      private$chrome$close()
      message("Chrome closed")
      self$process$kill()
      message("Process killed")
    },
    initialize = function(
      chrome_bin = Sys.getenv("HEADLESS_CHROME"),
      fun = "pkgload::load_all();run_app()",
      shiny_port = 2811L,
      chrome_port = 9222L,
      headless = TRUE,
      pre_launch_cmd = "",
      ...
    ){
      private$stdout_ <- tempfile()
      private$stderr_ <- tempfile()

      attempt::stop_if(
        pre_launch_cmd,
        is.null,
        'pre_launch_cmd can not be NULL. If you want it empty, use `""` (the default).'
      )

      self$process <- processx::process$new(
        "Rscript", c(
          "-e",
          sprintf(
            "options(shiny.port = %s, shiny.launch.browser = invisible);%s;%s",
            shiny_port, pre_launch_cmd, fun
          )
        ),
        stderr  = private$stderr_,
        stdout  = private$stdout_
      )
      
      # It seems that Shiny need a lot of time to load properly
      Sys.sleep(5)

      attempt::stop_if_not(
        self$process$is_alive(),
        msg = "Unable to launch the Shiny App"
      )

      private$chrome <- crrri::Chrome$new(
        chrome_bin,
        debug_port = chrome_port,
        headless = headless,
        ...
      )
      private$client <- crrri::hold(
        private$chrome$connect()
      )

      private$Page <-  private$client$Page
      private$Runtime <-  private$client$Runtime

      crrri::hold({
        private$client$Page$navigate(
          url = sprintf(
            "http://127.0.0.1:%s",
            shiny_port
          )
        )
      })
    }
  )
)

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