Skip to content

Commit

Permalink
πŸ”Š logging λͺ¨λ“ˆμ„ μ΄μš©ν•΄μ„œ live_check λ‘œκΉ…
Browse files Browse the repository at this point in the history
  • Loading branch information
junah201 committed Jun 22, 2024
1 parent 3668b65 commit f1c9e50
Showing 1 changed file with 121 additions and 18 deletions.
139 changes: 121 additions & 18 deletions lambdas/live_check/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from layers.common.python.common import *

import boto3
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

dynamodb = boto3.client('dynamodb')

Expand All @@ -22,6 +26,16 @@ def middleware(event, context):
νŠΈλ¦¬κ±°λ§ˆλ‹€ μ²˜λ¦¬ν•˜λŠ” 데이터가 λ‹€λ₯΄κ²Œ λΆ„μ‚°ν™”ν•©λ‹ˆλ‹€.
"""

logger.info(
json.dumps(
{
"type": "START",
"index": index
},
ensure_ascii=False
)
)

result = {
"total_channel_count": 0,
"live_channel_count": 0,
Expand All @@ -41,13 +55,50 @@ def middleware(event, context):
}
)

logger.info(
json.dumps(
{
"type": "CHANNEL_QUERY_END",
"total_channel_count": res["Count"],
"index": index
},
ensure_ascii=False
)
)

result["total_channel_count"] = res["Count"]

for item in res["Items"]:
channel_id = item["PK"]["S"].split("#")[1]
last_live_id = item["lastLiveId"]["N"]

chzzk = get_chzzk(channel_id)
try:
start_time = datetime.now()
chzzk = get_chzzk(channel_id)
end_time = datetime.now()
logger.info(
json.dumps(
{
"type": "GET_CHZZK_END",
"channel_id": channel_id,
"index": index,
"time": (end_time - start_time).total_seconds(),
},
ensure_ascii=False
)
)
except Exception as e:
logger.error(
json.dumps(
{
"type": "GET_CHZZK_ERROR",
"channel_id": channel_id,
"index": index,
"error": str(e)
}
)
)
continue

if not chzzk:
continue
Expand All @@ -58,6 +109,18 @@ def middleware(event, context):
if chzzk['status'] != "OPEN":
continue

logger.info(
json.dumps(
{
"type": "LIVE_START",
"chzzk_id": channel_id,
"index": index,
"liveId": chzzk['liveId'],
"liveTitle": chzzk['liveTitle']
}
)
)

result["live_channel_count"] += 1

dynamodb.update_item(
Expand All @@ -73,9 +136,6 @@ def middleware(event, context):
}
)

print(
f"LIVE_START {chzzk['channel']['channelName']}, {chzzk['liveId']}, {chzzk['liveTitle']}, {index=}")

res = dynamodb.query(
TableName='chzzk-bot-db',
KeyConditionExpression='PK = :pk_val AND begins_with(SK, :sk_val)',
Expand All @@ -85,6 +145,18 @@ def middleware(event, context):
}
)

logger.info(
json.dumps(
{
"type": "NOTI_QUERY_END",
"chzzk_id": channel_id,
"index": index,
"noti_count": res["Count"]
}
),
ensure_ascii=False
)

for noti in res["Items"]:
discord_channel_id = noti["channel_id"]["S"]

Expand Down Expand Up @@ -140,7 +212,17 @@ def middleware(event, context):

# 채널이 μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 경우
if res.status_code == 404:
print("channel not found")
logging.error(
json.dumps(
{
"type": "DISCORD_CHANNEL_NOT_FOUND",
"chzzk_id": channel_id,
"index": index,
"discord_channel_id": discord_channel_id
},
ensure_ascii=False
)
)
dynamodb.delete_item(
TableName='chzzk-bot-db',
Key={
Expand All @@ -153,7 +235,20 @@ def middleware(event, context):

# λ©”μ‹œμ§€ 전솑에 μ‹€νŒ¨ν•œ 경우
if res.status_code != 200:
print("send message fail", res.status_code)

logging.error(
json.dumps(
{
"type": "DISCORD_MESSAGE_SEND_FAIL",
"chzzk_id": channel_id,
"index": index,
"discord_channel_id": discord_channel_id,
"status_code": res.status_code,
"response": res.text
},
ensure_ascii=False
),
)
dynamodb.update_item(
TableName='chzzk-bot-db',
Key={
Expand All @@ -167,18 +262,21 @@ def middleware(event, context):
':noti_fail_count': {'N': str(int(noti.get("noti_fail_count", {"N": "0"})["N"]) + 1)}
}
)
try:
print(res.json())
except:
try:
print(res.text)
except:
print("unknown error")
result["fail_send_count"] += 1
continue

# 성곡
print("send message success")
logging.info(
json.dumps(
{
"type": "DISCORD_MESSAGE_SEND_SUCCESS",
"chzzk_id": channel_id,
"index": index,
"discord_channel_id": discord_channel_id
},
ensure_ascii=False
)
)
result["total_send_count"] += 1
dynamodb.update_item(
TableName='chzzk-bot-db',
Expand All @@ -200,7 +298,12 @@ def middleware(event, context):
def lambda_handler(event, context):
res = middleware(event, context)

print("=== res ===")
print(json.dumps(res))

return res
logger.info(
json.dumps(
{
"type": "FINAL_RESULT",
"result": res
},
ensure_ascii=False
)
)

0 comments on commit f1c9e50

Please sign in to comment.