in source/core-api/lambda_functions/reset_initial_state.py [0:0]
def lambda_handler(event, context):
"""
This function is the entry handler for Lambda.
"""
print(event)
client_event_id = deep_clean(event['event_id'])
response = {}
headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
if EVENT_ID == client_event_id:
# reset counters
rc.getset(SERVING_COUNTER, 0)
rc.getset(QUEUE_COUNTER, 0)
rc.getset(TOKEN_COUNTER, 0)
rc.getset(COMPLETED_SESSION_COUNTER, 0)
rc.getset(ABANDONED_SESSION_COUNTER, 0)
try:
response = ddb_client.delete_table( TableName=DDB_TABLE_NAME )
waiter = ddb_client.get_waiter('table_not_exists')
# wait for table to get deleted
waiter.wait(TableName=DDB_TABLE_NAME)
print("Token table deleted")
# recreate table
response = ddb_client.create_table(
TableName= DDB_TABLE_NAME,
BillingMode = "PAY_PER_REQUEST",
AttributeDefinitions = [
{
"AttributeName": "request_id",
"AttributeType": "S"
},
{
"AttributeName": "expires",
"AttributeType": "N"
},
{
"AttributeName": "event_id",
"AttributeType": "S"
}
],
KeySchema = [
{
"AttributeName": "request_id",
"KeyType": "HASH"
}
],
GlobalSecondaryIndexes = [
{
"IndexName": "EventExpiresIndex",
"KeySchema": [
{
"AttributeName": "event_id",
"KeyType": "HASH"
},
{
"AttributeName": "expires",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
}
}
],
SSESpecification = {
"Enabled": True
}
)
waiter = ddb_client.get_waiter('table_exists')
# wait for table to get created
waiter.wait(TableName=DDB_TABLE_NAME)
print("Token table recreated")
# enable PITR
ddb_client.update_continuous_backups(
TableName=DDB_TABLE_NAME,
PointInTimeRecoverySpecification={
'PointInTimeRecoveryEnabled': True
}
)
response = {
"statusCode": 200,
"headers": headers,
"body": json.dumps({
"message": "Counters reset. DynamoDB table recreated."
})
}
except Exception as other_exception:
print(other_exception)
raise other_exception
else:
response = {
"statusCode": 400,
"headers": headers,
"body": json.dumps({"error": "Invalid event ID"})
}
print(response)
return response