rstudio / httpuv

HTTP and WebSocket server package for R

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question Regarding Async Dynamic Requests In call

tconwell opened this issue · comments

I was hoping for some extra documentation or guidance on implementing async processing for dynamic page requests. My app sometimes runs many minute long processes for a given request and I can't figure out how to let other users load a page while it's running. Is using future_promise in call = function(req){future_promise(...)} the right approach? I tried something like this but it doesn't work:

dynamic = list(
        "/" = c(
          "GET" = expression(get_login(req)),
          "POST" = expression(post_login(req))
        ),
        "/database" = c(
          "GET" = expression(require_auth(req, require_license(req, get_database(req)))),
          "POST" = expression(require_auth(req, require_license(req, post_database(req))))
        ),
        "/flow" = c(
          "GET" = expression(require_auth(req, require_license(req, get_flow(req)))),
          "POST" = expression(require_auth(req, require_license(req, post_flow(req))))
        )
)
static <- list()

startServer(
        host,
        port,
        app = list(
          call = function(req) {
            req <- list(
              "REQUEST_METHOD" = req$REQUEST_METHOD,
              "SCRIPT_NAME" = req$SCRIPT_NAME,
              "PATH_INFO" = req$PATH_INFO,
              "QUERY_STRING" = req$QUERY_STRING,
              "SERVER_NAME" = req$SERVER_NAME,
              "SERVER_PORT" = req$SERVER_PORT,
              "HEADERS" = req$HEADERS,
              "rook.input" = req[["rook.input"]]$read_lines()
            )

            future_promise({
              if(req$PATH_INFO %in% valid_dynamic_paths){

                x <- eval(dynamic[[req$PATH_INFO]][req$REQUEST_METHOD])

                list(
                  status = x[["status"]],
                  headers = x[["headers"]],
                  body = x[["body"]]
                )

              }else{

                list(
                  status = 404,
                  headers = list(
                    'Content-Type' = 'text/html'
                  ),
                  body = "404. Page not found."
                )

              }
            })
          },
          staticPaths = static
        )
      )

Sorry if this is not the best place to ask, but Stack Overflow is not often helpful for this package.

https://stackoverflow.com/questions/70057672/how-to-implement-async-calls-in-r-httpuv-startserver