Tools to Work with the 'Splash' JavaScript Rendering Service in R
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #' Retrieve the Docker image for Splash
  2. #'
  3. #' @md
  4. #' @param tag Splash Docker image tag to install
  5. #' @return a `docker_image` object or `NULL` if an error occurred.
  6. #' @export
  7. #' @family splash_docker_helpers
  8. #' @examples \dontrun{
  9. #' install_splash()
  10. #' splash_container <- start_splash()
  11. #' stop_splash(splash_container)
  12. #' }
  13. install_splash <- function(tag="latest") {
  14. docker <- stevedore::docker_client()
  15. tryCatch(
  16. docker$image$pull("scrapinghub/splash", tag=tag, stream=stdout()),
  17. error = function(e) {
  18. message("Error pulling image from DockerHub.\n", e)
  19. return(NULL)
  20. },
  21. interrupt = function(e) {
  22. stop("Terminated by user", call. = FALSE)
  23. }
  24. ) -> res
  25. invisible(res)
  26. }
  27. #' Start a Splash server Docker container
  28. #'
  29. #' If using this in an automation context, you should consider adding a
  30. #' `Sys.sleep(3)` (or higher) after starting the docker container.
  31. #'
  32. #' This uses the `latest` image and passed the `--disable-browser-caches`
  33. #' parameter. If you do not want to use the 3.2.x+ versions of `Splash`
  34. #' you should use your own startup scripts vs this helper function.
  35. #'
  36. #' @param tag Splash Docker image tag to start
  37. #' @note you need Docker running on your system and have pulled the container with
  38. #' [install_splash] for this to work. You should save the resultant
  39. #' object for use in [stop_splash] otherwise you'll have to kill it from the
  40. #' command line interface.
  41. #' @param container_name naem for the container. Defaults to "`splashr`".
  42. #' @param remove remove the Splash container instance after it's stopped?
  43. #' Defaults to `FALSE`.
  44. #' @param ... passed on to Splash instance launch parameters
  45. #' @family splash_docker_helpers
  46. #' @return `stevedore` container object
  47. #' @export
  48. #' @examples \dontrun{
  49. #' install_splash()
  50. #' splash_container <- start_splash()
  51. #' stop_splash(splash_container)
  52. #' }
  53. start_splash <- function(tag="latest", container_name = "splashr", remove=FALSE, ...) {
  54. docker <- stevedore::docker_client()
  55. tryCatch(
  56. docker$container$run(
  57. image = sprintf("scrapinghub/splash:%s", tag),
  58. name = container_name,
  59. ports = c("5023:5023", "8051:8051", "8050:8050"),
  60. detach = TRUE,
  61. rm = remove,
  62. tty = TRUE,
  63. "--disable-browser-caches",
  64. ...
  65. ),
  66. error = function(e) {
  67. message("Error pulling image from DockerHub.")
  68. return(NULL)
  69. },
  70. interrupt = function(e) {
  71. stop("Terminated by user", call. = FALSE)
  72. }
  73. ) -> splash_inst
  74. invisible(splash_inst)
  75. }
  76. #' Stop a running a Splash server Docker container
  77. #'
  78. #' @param splash_container Docker `container` object created by [start_splash()]
  79. #' @note you need Docker running on your system and have pulled the container with
  80. #' [install_splash()] and started the Splash container with [start_splash()] for this
  81. #' to work. You will need the `container` object from [start_splash()] for this to work.
  82. #' @family splash_docker_helpers
  83. #' @export
  84. #' @examples \dontrun{
  85. #' install_splash()
  86. #' splash_container <- start_splash()
  87. #' stop_splash(splash_container)
  88. #' }
  89. stop_splash <- function(splash_container) {
  90. if (inherits(splash_container, "stevedore_object")) {
  91. splash_container$stop()
  92. splash_container$remove()
  93. }
  94. invisible(NULL)
  95. }
  96. #' Prune all dead and running Splash Docker containers
  97. #'
  98. #' _This is a destructive function._ It will stop **any** Docker container that
  99. #' is based on an image matching "`scrapinghub/splashr`". It's best used when you
  100. #' had a session forcefully interuppted and had been using the R helper functions
  101. #' to start/stop the Splash Docker container. You may want to consider using the
  102. #' Docker command-line interface to perform this work manually.
  103. #'
  104. #' @export
  105. killall_splash <- function() {
  106. docker <- stevedore::docker_client()
  107. x <- docker$container$list(all=TRUE)
  108. for (i in 1:nrow(x)) {
  109. if (length(x$command[i])) {
  110. if (grepl("bin/splash", x$command[i])) {
  111. message(sprintf("Pruning: %s...", x$id[i]))
  112. if (x$state[i] == "running") {
  113. cntnr <- docker$container$get(x$id[i])
  114. cntnr$stop()
  115. cntnr$remove()
  116. }
  117. }
  118. }
  119. }
  120. }
  121. # @param add_tempdir This is `FALSE` initially since you could try to run
  122. # the splash image on a remote system. It has to be a local one for this to work.
  123. # If `TRUE` then a local temporary directory (made with [tempdir()])
  124. # will be added to the mount configuration for use with [render_file()]. You will need to
  125. # ensure the necessary system temp dirs are accessible as a mounts. For
  126. # macOS this means adding `/private` to said Docker config.