Skip to content

Commit

Permalink
feat: adding debug prints and job id generation for sync career
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonNotJson committed Nov 13, 2023
1 parent 1a2cc80 commit 8bb94be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/constructs/persistence/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class DynamoDatabase extends Construct {
removalPolicy: RemovalPolicy.RETAIN,
sortKey: { name: 'created_at', type: dynamodb.AttributeType.STRING },
tableName: 'career',
readCapacity: 1,
writeCapacity: 1,
readCapacity: 10,
writeCapacity: 10,
},
);

Expand Down
18 changes: 14 additions & 4 deletions src/lambda/sync-career/index.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import json
from datetime import datetime
from utils import JsonPayloadBuilder
from utils import resp_handler, table, bucket, s3_client, get_image_key

from utils import resp_handler, table, bucket, s3_client, get_image_key, build_job_id

@resp_handler
def sync_career(key):

dt_now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
job_id = build_job_id()

try:
print(f"Starting sync_career with key: {key}")

s3_object = s3_client.get_object(Bucket=bucket, Key=key)
file_content = s3_object['Body'].read().decode('utf-8')
json_content = json.loads(file_content)
print("JSON content loaded successfully.")

# Get folder (company name)
folder = key.split('/')[0]
print(f"Folder (company name): {folder}")

# Construct keys for images
hero_image_key = get_image_key(folder, "hero_image")
company_logo_key = get_image_key(folder, "company_logo")
print(f"Image keys: hero_image_key={hero_image_key}, company_logo_key={company_logo_key}")

item = {
'job_id': json_content['job_id'],
'job_id': job_id,
'title': json_content['title'],
'company_description': json_content['company_description'],
'location': json_content['location'],
Expand All @@ -40,21 +45,26 @@ def sync_career(key):
'company_logo': company_logo_key,
}


print(f"Item to be inserted: {item}")
table.put_item(Item=item)
print("Item inserted into DynamoDB table successfully.")

body = JsonPayloadBuilder().add_status(True).add_data(
None).add_message('Imgs key load to table successfully.').compile()
print("Payload built successfully.")
return body

except Exception as e:
# Handle any exceptions that occur
print(f"Exception occurred: {str(e)}")
return JsonPayloadBuilder().add_status(False).add_message(str(e)).compile()


def handler(event, context):

# Get the object from the event
print("Lambda handler started.")
key = event['Records'][0]['s3']['object']['key']
print(f"Received key from event: {key}")

return sync_career(key)
10 changes: 9 additions & 1 deletion src/lambda/sync-career/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
import os
from decimal import Decimal
import uuid
from datetime import datetime

db = boto3.resource("dynamodb", region_name="ap-northeast-1")
table = db.Table(os.getenv('TABLE_NAME'))
Expand Down Expand Up @@ -74,4 +76,10 @@ def get_image_key(folder, image_name):
key = obj['Key']
if key.startswith(f"{folder}/{image_name}"):
return key
return None
return None

def build_job_id():

unique_id = str(uuid.uuid4())

return unique_id

0 comments on commit 8bb94be

Please sign in to comment.