|
1 | | -library(httr) |
2 | | -library(jsonlite) |
3 | | -library(logging) |
4 | | - |
5 | 1 | to_str <- function(x) { |
6 | | - return(paste(capture.output(print(x)), collapse = "\n")) |
| 2 | + return(paste(capture.output(print(x)), collapse = "\n")) |
| 3 | +} |
| 4 | + |
| 5 | +error_to_payload <- function(error) { |
| 6 | + return(list(errorMessage = toString(error), errorType = class(error)[1])) |
| 7 | +} |
| 8 | + |
| 9 | +post_error <- function(error, url) { |
| 10 | + logerror(error) |
| 11 | + res <- POST(url, |
| 12 | + add_headers("Lambda-Runtime-Function-Error-Type" = "Unhandled"), |
| 13 | + body = error_to_payload(error), |
| 14 | + encode = "json") |
| 15 | + loginfo("Posted result:\n%s", to_str(res)) |
| 16 | +} |
| 17 | + |
| 18 | +get_source_file_name <- function(file_base_name) { |
| 19 | + file_name <- paste0(file_base_name, ".R") |
| 20 | + if (! file.exists(file_name)) { |
| 21 | + file_name <- paste0(file_base_name, ".r") |
| 22 | + } |
| 23 | + if (! file.exists(file_name)) { |
| 24 | + stop(paste0('Source file does not exist: ', file_base_name, '.[R|r]')) |
| 25 | + } |
| 26 | + return(file_name) |
| 27 | +} |
| 28 | + |
| 29 | +invoke_lambda <- function(EVENT_DATA, function_name) { |
| 30 | + params <- fromJSON(EVENT_DATA) |
| 31 | + loginfo("Invoking function '%s' with parameters:\n%s", function_name, to_str(params)) |
| 32 | + result <- do.call(function_name, params) |
| 33 | + loginfo("Function returned:\n%s", to_str(result)) |
| 34 | + return(result) |
| 35 | +} |
| 36 | + |
| 37 | +initializeRuntime <- function() { |
| 38 | + library(httr) |
| 39 | + library(jsonlite) |
| 40 | + library(logging) |
| 41 | + |
| 42 | + HANDLER <- Sys.getenv("_HANDLER") |
| 43 | + HANDLER_split <- strsplit(HANDLER, ".", fixed = TRUE)[[1]] |
| 44 | + file_base_name <- HANDLER_split[1] |
| 45 | + file_name <- get_source_file_name(file_base_name) |
| 46 | + loginfo("Sourcing '%s'", file_name) |
| 47 | + source(file_name) |
| 48 | + return(HANDLER_split[2]) |
7 | 49 | } |
8 | 50 |
|
9 | | -HANDLER <- Sys.getenv("_HANDLER") |
10 | 51 | AWS_LAMBDA_RUNTIME_API <- Sys.getenv("AWS_LAMBDA_RUNTIME_API") |
11 | | -args = commandArgs(trailingOnly = TRUE) |
12 | | -EVENT_DATA <- args[1] |
13 | | -REQUEST_ID <- args[2] |
14 | | - |
15 | | -HANDLER_split <- strsplit(HANDLER, ".", fixed = TRUE)[[1]] |
16 | | -file_name <- paste0(HANDLER_split[1], ".R") |
17 | | -function_name <- HANDLER_split[2] |
18 | | -loginfo("Sourcing '%s'", file_name) |
19 | | -source(file_name) |
20 | | -params <- fromJSON(EVENT_DATA) |
21 | | -loginfo("Invoking function '%s' with parameters: %s", function_name, to_str(params)) |
22 | | -result <- do.call(function_name, params) |
23 | | -loginfo("Function returned: %s", to_str(result)) |
24 | | -url <- paste0("http://", |
25 | | - AWS_LAMBDA_RUNTIME_API, |
26 | | - "/2018-06-01/runtime/invocation/", |
27 | | - REQUEST_ID, |
28 | | - "/response") |
29 | | -res <- POST(url, body = list(result = result), encode = "json") |
30 | | -loginfo("Posted result: %s", to_str(res)) |
| 52 | +API_ENDPOINT <- paste0("http://", AWS_LAMBDA_RUNTIME_API, "/2018-06-01/runtime/") |
| 53 | + |
| 54 | +throwInitError <- function(error) { |
| 55 | + url <- paste0(API_ENDPOINT, "init/error") |
| 56 | + post_error(error, url) |
| 57 | + stop() |
| 58 | +} |
| 59 | + |
| 60 | +throwRuntimeError <- function(error, REQUEST_ID) { |
| 61 | + url <- paste0(API_ENDPOINT, "invocation/", REQUEST_ID, "/error") |
| 62 | + post_error(error, url) |
| 63 | +} |
| 64 | + |
| 65 | +postResult <- function(result, REQUEST_ID) { |
| 66 | + url <- paste0(API_ENDPOINT, "invocation/", REQUEST_ID, "/response") |
| 67 | + res <- POST(url, body = list(result = result), encode = "json") |
| 68 | + loginfo("Posted result:\n%s", to_str(res)) |
| 69 | +} |
| 70 | + |
| 71 | +handle_request <- function(function_name) { |
| 72 | + event_url <- paste0(API_ENDPOINT, "invocation/next") |
| 73 | + event_response <- GET(event_url) |
| 74 | + REQUEST_ID <- event_response$headers$`Lambda-Runtime-Aws-Request-Id` |
| 75 | + tryCatch({ |
| 76 | + EVENT_DATA <- rawToChar(event_response$content) |
| 77 | + result <- invoke_lambda(EVENT_DATA, function_name) |
| 78 | + postResult(result, REQUEST_ID) |
| 79 | + }, |
| 80 | + error = function(error) { |
| 81 | + throwRuntimeError(error, REQUEST_ID) |
| 82 | + }) |
| 83 | +} |
0 commit comments