A function to use in plumber scripts to accept Pub/Sub messages

cr_plumber_pubsub(message = NULL, pass_f = function(x) x)

Arguments

message

The pubsub message

pass_f

An R function that will work with the data parsed out of the pubsub message$data field.

Details

This function is intended to be used within plumb API scripts. It needs to be annotated with a @post URL route and a @param message The pubsub message as per the plumber documentation.

pass_f should be a function you create that accepts one argument, the data from the pubsub message$data field. It is unencoded for you. Make sure the function returns a 200 response otherwise pub/sub will keep resending the message! return(TRUE) is adequate.

The Docker container for the API will need to include googleCloudRunner installed in its R environment to run this function. This is available in the public gcr.io/gcer-public/cloudrunner image.

Use cr_pubsub to test this function once deployed.

See also

Google Pub/Sub tutorial for Cloud Run. You can set up Pub/Sub messages from Google Cloud Storage buckets via gcs_create_pubsub

Other Cloud Run functions: cr_jwt_create(), cr_run_get(), cr_run_list(), cr_run()

Examples

if (FALSE) { # 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) } }