use super::{full, BLOCK_NEXT_INVOCATION, LOCAL_REQUEST_ID};
use crate::config::PayloadSources;
use crate::sqs;
use crate::CONFIG;
use http_body_util::combinators::BoxBody;
use hyper::body::Bytes;
use hyper::Error;
use hyper::Response;
use tokio::time::{sleep, Duration};
use tracing::{error, info, warn};
pub(crate) async fn handler() -> Response<BoxBody<Bytes, Error>> {
block_if_rerun().await;
let config = CONFIG.get().await;
if let PayloadSources::Local(local_config) = &config.sources {
info!("Lambda request: sending payload from file");
return Response::builder()
.status(hyper::StatusCode::OK)
.header("lambda-runtime-aws-request-id", LOCAL_REQUEST_ID)
.header("lambda-runtime-deadline-ms", "2035313041000") .header("lambda-runtime-invoked-function-arn", "from-local-payload")
.header(
"lambda-runtime-trace-id",
"Root=0-00000000-000000000000000000000000;Parent=0000000000000000;Sampled=0;Lineage=00000000:0",
)
.body(full(local_config.payload.clone()))
.expect("Failed to create a response");
};
let sqs_message = sqs::get_input().await;
info!("Lambda request:\n{}", sqs_message.payload);
Response::builder()
.status(hyper::StatusCode::OK)
.header("lambda-runtime-aws-request-id", sqs_message.receipt_handle)
.header("lambda-runtime-deadline-ms", sqs_message.ctx.deadline)
.header(
"lambda-runtime-invoked-function-arn",
sqs_message.ctx.invoked_function_arn,
)
.header(
"lambda-runtime-trace-id",
sqs_message.ctx.xray_trace_id.unwrap_or_else(|| {
"Root=0-00000000-000000000000000000000000;Parent=0000000000000000;Sampled=0;Lineage=00000000:0"
.to_owned()
}),
)
.body(full(sqs_message.payload))
.expect("Failed to create a response")
}
async fn block_if_rerun() {
let block = if let Ok(block) = BLOCK_NEXT_INVOCATION.read() {
*block
} else {
error!("Read deadlock on BLOCK_NEXT_INVOCATION. It's a bug");
false
};
if block {
if let Ok(mut w) = BLOCK_NEXT_INVOCATION.write() {
*w = false;
} else {
error!("Write deadlock on BLOCK_NEXT_INVOCATION. It's a bug");
}
}
if block {
warn!("Restart your lambda for a rerun");
sleep(Duration::from_secs(31563000)).await;
}
}