Skip to content

Commit

Permalink
use a new Session for each lambda invocation
Browse files Browse the repository at this point in the history
this attempts to work around an issue we see with requests to Opencast
possibly failing when re-using urllib https connections
  • Loading branch information
lbjay committed Dec 11, 2024
1 parent b1a834d commit a730207
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions functions/zoom-uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,8 @@
aws_lambda = boto3.client("lambda")
sqs = boto3.resource("sqs")

session = requests.Session()
session.auth = HTTPDigestAuth(OPENCAST_API_USER, OPENCAST_API_PASSWORD)
session.headers.update(
{
"X-REQUESTED-AUTH": "Digest",
# TODO: it's possible this header is not necessary for the endpoints
# being used here. It seems like for Opencast endpoints where the
# header *is* necessary the correct value is actually
# "X-Opencast-Matterhorn-Authorization"
"X-Opencast-Matterhorn-Authentication": "true",
}
)

UPLOAD_OP_TYPES = ["track", "uri-track"]
SESSION = None


class OpencastConnectionError(Exception):
Expand All @@ -65,10 +53,12 @@ class InvalidOpencastSeriesId(Exception):


def oc_api_request(method, endpoint, **kwargs):

url = urljoin(OPENCAST_BASE_URL, endpoint)
logger.info({"url": url, "kwargs": kwargs})

try:
resp = session.request(method, url, **kwargs)
resp = SESSION.request(method, url, **kwargs)
except requests.RequestException:
raise OpencastConnectionError
resp.raise_for_status()
Expand All @@ -89,6 +79,16 @@ def handler(event, context):
else:
logger.info(f"{len(messages)} upload messages in queue")

global SESSION
SESSION = requests.Session()
SESSION.auth = HTTPDigestAuth(OPENCAST_API_USER, OPENCAST_API_PASSWORD)
SESSION.headers.update(
{
"X-REQUESTED-AUTH": "Digest",
"X-Opencast-Matterhorn-Authentication": "true",
}
)

upload_message = messages[0]
logger.info(
{
Expand Down Expand Up @@ -118,6 +118,7 @@ def handler(event, context):
else:
final_status = PipelineStatus.IGNORED
logger.info("No workflow initiated.")

except Exception as e:
logger.exception(e)
final_status = PipelineStatus.UPLOADER_FAILED
Expand All @@ -133,6 +134,7 @@ def handler(event, context):
final_status,
reason=reason,
)
SESSION.close()


def minutes_in_pipeline(webhook_received_time):
Expand Down

0 comments on commit a730207

Please sign in to comment.