add support to save executed scheduled
This commit is contained in:
@@ -6,7 +6,9 @@ from aws_lambda_powertools.utilities.data_classes import (
|
||||
event_source,
|
||||
)
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from layercake.dateutils import now
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer
|
||||
from layercake.funcs import pick
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import ENROLLMENT_TABLE
|
||||
@@ -20,6 +22,8 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
@logger.inject_lambda_context
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
old_image = event.detail['old_image']
|
||||
sk = old_image['sk']
|
||||
now_ = now()
|
||||
# Key pattern `SCHEDULED#ORG#{org_id}`
|
||||
*_, org_id = old_image['id'].split('#')
|
||||
offset_days = old_image.get('dedup_window_offset_days')
|
||||
@@ -30,23 +34,52 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
user=old_image['user'],
|
||||
)
|
||||
|
||||
return enroll(
|
||||
enrollment,
|
||||
org={
|
||||
'org_id': org_id,
|
||||
'name': old_image['org_name'],
|
||||
},
|
||||
subscription=(
|
||||
{
|
||||
try:
|
||||
enroll(
|
||||
enrollment,
|
||||
org={
|
||||
'org_id': org_id,
|
||||
'billing_day': int(billing_day),
|
||||
'name': old_image['org_name'],
|
||||
},
|
||||
subscription=(
|
||||
{
|
||||
'org_id': org_id,
|
||||
'billing_day': int(billing_day),
|
||||
}
|
||||
if billing_day
|
||||
else None
|
||||
),
|
||||
created_by=created_by,
|
||||
scheduled_at=datetime.fromisoformat(old_image['created_at']),
|
||||
# Transfer the deduplication window if it exists
|
||||
deduplication_window={'offset_days': offset_days} if offset_days else None,
|
||||
persistence_layer=dyn,
|
||||
)
|
||||
|
||||
keys = ('id', 'name')
|
||||
dyn.put_item(
|
||||
item={
|
||||
'id': old_image['id'],
|
||||
'sk': f'{sk}#EXECUTED',
|
||||
'enrollment_id': enrollment.id,
|
||||
'user': pick(keys, old_image['user']),
|
||||
'course': pick(keys, old_image['course']),
|
||||
'created_by': created_by,
|
||||
'created_at': now_,
|
||||
}
|
||||
if billing_day
|
||||
else None
|
||||
),
|
||||
created_by=created_by,
|
||||
scheduled_at=datetime.fromisoformat(old_image['created_at']),
|
||||
# Transfer the deduplication window if it exists
|
||||
deduplication_window={'offset_days': offset_days} if offset_days else None,
|
||||
persistence_layer=dyn,
|
||||
)
|
||||
)
|
||||
except Exception as exc:
|
||||
dyn.put_item(
|
||||
item={
|
||||
'id': old_image['id'],
|
||||
'sk': f'{sk}#FAILED',
|
||||
'cause': {
|
||||
'type': type(exc).__name__,
|
||||
'message': str(exc),
|
||||
},
|
||||
'snapshot': old_image,
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user