|
# Ensure the folder structure exists such as: |
|
# ./app/main.py |
|
import json |
|
import time |
|
|
|
import boto3 |
|
from flask import Flask, jsonify, request |
|
|
|
USER_AGENT = "globus-sdk-py-3.41.0" |
|
MAX_SLEEP = 90 |
|
|
|
dynamodb_table_name = "sc_36616_timeout_test" |
|
dynamodb = boto3.client("dynamodb") |
|
|
|
|
|
def get_flows_request_item(dynamodb_client, table_name, request_id): |
|
flows_req = dynamodb_client.get_item( |
|
TableName=table_name, Key={"request_id": {"S": str(request_id)}} |
|
) |
|
if "Item" in flows_req: |
|
return flows_req["Item"] |
|
else: |
|
return None |
|
|
|
|
|
def insert_flows_request_item(dynamodb_client, table_name, item): |
|
resp = dynamodb_client.put_item(TableName=table_name, Item=item) |
|
if resp["ResponseMetadata"]["HTTPStatusCode"] != 200: |
|
raise Exception(f"Error inserting item into {table_name}: {resp}") |
|
return resp |
|
|
|
|
|
def update_flows_request_item(dynamodb_client, table_name, request_id, count): |
|
dynamodb_client.update_item( |
|
TableName=table_name, |
|
Key={"request_id": {"S": request_id}}, |
|
UpdateExpression="set #count = :count", |
|
ExpressionAttributeNames={"#count": "count"}, |
|
ExpressionAttributeValues={":count": {"N": str(count)}}, |
|
) |
|
|
|
|
|
def create_app(): |
|
app = Flask(__name__) |
|
|
|
@app.errorhandler(Exception) |
|
def handle_exception(error): |
|
response = {"error": str(error)} |
|
print(str(error)) |
|
return jsonify(response), 500 |
|
|
|
@app.route("/", methods=["GET"]) |
|
def home(): |
|
return jsonify({"message": "ping"}), 200 |
|
|
|
@app.route("/run", methods=["GET", "POST"]) |
|
def run(): |
|
user_agent = request.headers.get("User-Agent") |
|
|
|
# Only process request if it is from flows |
|
if request.method == "POST" and user_agent == USER_AGENT: |
|
print("Received POST request from flows!") |
|
data = request.get_json() |
|
request_id = data.get("request_id", None) |
|
|
|
if request_id and request_id.startswith("flows"): |
|
req_id = request_id.split("_")[-1] |
|
print(json.dumps(data)) |
|
|
|
flows_req_item = get_flows_request_item( |
|
dynamodb, dynamodb_table_name, req_id |
|
) |
|
|
|
if flows_req_item is None: |
|
print(f"No item found for {req_id}. Inserting new item.") |
|
insert_flows_request_item( |
|
dynamodb, |
|
dynamodb_table_name, |
|
{"request_id": {"S": req_id}, "count": {"N": "1"}}, |
|
) |
|
time.sleep(MAX_SLEEP) |
|
else: |
|
count = int(flows_req_item["count"]["N"]) + 1 |
|
print(f"Request id {req_id} found. Updating count to {count}") |
|
update_flows_request_item( |
|
dynamodb, dynamodb_table_name, req_id, count + 1 |
|
) |
|
time.sleep(MAX_SLEEP) |
|
return ( |
|
jsonify( |
|
{ |
|
"message": f"slept for {MAX_SLEEP} sec for {req_id} with count {count}" |
|
} |
|
), |
|
200, |
|
) |
|
else: |
|
return jsonify({"message": data}), 200 |
|
else: |
|
print("Received GET request!") |
|
|
|
return jsonify({"message": "Hello!"}), 200 |
|
|
|
@app.route("/", defaults={"path": ""}) |
|
@app.route("/<path:path>") |
|
def catch_all(path): |
|
print( |
|
"Received an unregistered path for: " |
|
f"{path} with method: {request.method}" |
|
) |
|
return jsonify({"message": "Unregistered path"}), 404 |
|
|
|
return app |
|
|
|
|
|
fake_ap = create_app() |
|
|
|
|
|
if __name__ == "__main__": |
|
fake_ap = create_app() |
|
fake_ap.run(debug=True, host="0.0.0.0", port=8080) |