-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
9db554f
commit 406466a
Showing
13 changed files
with
162 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#' Plumber - Pub/Sub parser | ||
#' | ||
#' A function to use in plumber scripts to accept Pub/Sub messages | ||
#' | ||
#' @param message The pubsub message | ||
#' @param pass_f An R function that will work with the data parsed out of the pubsub \code{message$data} field. | ||
#' | ||
#' @details | ||
#' | ||
#' This function is intended to be used within \link[plumber]{plumb} API scripts. It needs to be annotated with a \code{@post} URL route and a \code{@param message The pubsub message} as per the plumber documentation. | ||
#' | ||
#' \code{pass_f} should be a function you create that accepts one argument, the data from the pubsub \code{message$data} field. It is unencoded for you. | ||
#' | ||
#' The Docker container for the API will need to include \code{googleCloudRunner} installed in its R environment to run this function. This is available in the public \code{gcr.io/gcer-public/cloudrunner} image. | ||
#' | ||
#' | ||
#' @export | ||
#' @examples | ||
#' | ||
#' \dontrun{ | ||
#' | ||
#' # within a plumber api.R script: | ||
#' | ||
#' # example function echos back pubsub message | ||
#' pub <- function(x){ | ||
#' paste("Echo:", x) | ||
#' } | ||
#' | ||
#' #' Recieve pub/sub message | ||
#' #' @post /pubsub | ||
#' #' @param message a pub/sub message | ||
#' function(message=NULL){ | ||
#' googleCloudRunner::cr_plumber_pubsub(message, pub) | ||
#' } | ||
#' | ||
#' } | ||
#' @seealso \href{https://cloud.google.com/run/docs/tutorials/pubsub}{Google Pub/Sub tutorial for Cloud Run} | ||
#' @family Cloud Run functions | ||
cr_run_pubsub <- function(message=NULL, | ||
pass_f=function(x) x){ | ||
# | ||
if(is.null(message)) stop("pub/sub message not found") | ||
stopifnot( | ||
is.list(message), | ||
!is.null(message$data) | ||
) | ||
|
||
the_data <- rawToChar(jsonlite::base64_dec(message$data)) | ||
|
||
pass_f(the_data) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
FROM trestletech/plumber | ||
FROM gcr.io/gcer-public/cloudrunner:master | ||
LABEL maintainer="cloudRunner" | ||
|
||
COPY [".", "./"] | ||
|
||
ENTRYPOINT ["R", "-e", "pr <- plumber::plumb(commandArgs()[4]); pr$run(host='0.0.0.0', port=as.numeric(Sys.getenv('PORT')))"] | ||
CMD ["api.R"] | ||
CMD ["api.R"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.