-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer-start.py
103 lines (92 loc) · 3.95 KB
/
transfer-start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#
# This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved.
# Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected]
#
# The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license.
#
import base64
import boto3
import json
import uuid
import gzip
from datetime import datetime
from os import environ
from module.logger import get_logger
from module.transfer_mentor_schema import transfer_mentor_json_schema
from jsonschema import validate, ValidationError
from module.api import import_task_create_gql, ImportTaskGQLRequest, user_can_edit_mentor
from module.utils import (
create_json_response,
load_sentry,
require_env,
get_auth_headers,
)
load_sentry()
log = get_logger("transfer-start")
ttl_sec = environ.get("TTL_SEC", (60 * 60 * 24) * 180) # 180 days
aws_region = require_env("REGION")
JOBS_TABLE_NAME = require_env("JOBS_TABLE_NAME")
log.info(f"using table {JOBS_TABLE_NAME}")
dynamodb = boto3.resource("dynamodb", region_name=aws_region)
job_table = dynamodb.Table(JOBS_TABLE_NAME)
def handler(event, context):
log.info(event)
if "body" not in event:
data = {
"error": "Bad Request",
"message": "body payload is required",
}
return create_json_response(401, data, event)
if event["isBase64Encoded"]:
body = base64.b64decode(event["body"])
else:
body = event["body"]
transfer_request = json.loads(body)
try:
validate(instance=transfer_request, schema=transfer_mentor_json_schema)
except ValidationError as err:
log.warning(err)
data = {
"error": "Bad Request",
"message": f"invalid payload: {err}",
}
return create_json_response(401, data, event)
auth_headers = get_auth_headers(event)
mentor = transfer_request["mentor"]
if not user_can_edit_mentor(mentor, auth_headers):
data = {
"error": "not authorized",
"message": "not authorized",
}
return create_json_response(401, data, event)
# this tends to be large so to avoid 400kb max item size:
compressed_body = gzip.compress(bytes(body, "utf-8"))
graphql_update = {"status": "QUEUED"}
s3_video_migration = {"status": "QUEUED"}
import_task_create_gql(
ImportTaskGQLRequest(mentor, graphql_update, s3_video_migration), auth_headers
)
job_id = str(uuid.uuid4())
train_job = {
"id": job_id,
"mentor": mentor,
"authHeaders": json.dumps(auth_headers),
"status": "QUEUED",
"payload": boto3.dynamodb.types.Binary(compressed_body),
"created": datetime.now().isoformat(),
# https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.html#time-to-live-ttl-before-you-start-formatting
"ttl": int(datetime.now().timestamp()) + ttl_sec,
}
job_table.put_item(Item=train_job)
data = {
"id": job_id,
"mentor": mentor,
"status": "QUEUED",
"statusUrl": f"/transfer/status/{job_id}",
}
return create_json_response(200, data, event)
# # for local debugging:
# if __name__ == "__main__":
# with open("__events__/transfer-start-event.json.dist") as f:
# event = json.loads(f.read())
# handler(event, {})