kookmin-sw / capstone-2023-05

capstone-2023-05 created by GitHub Classroom
https://capstone-2023-05.vercel.app
0 stars 0 forks source link

refactor: DB and Websocket #47

Closed ChangHoon-Sung closed 1 year ago

ChangHoon-Sung commented 1 year ago

이 PR은 아래와 같은 리팩토링을 포함합니다.

Jaewook-Lee commented 1 year ago

Refactoring 된 내용을 확인했습니다. 그래도 이 코드들이 teams 에서 나왔던 {"message": "Forbidden"} 이슈를 해결하진 않은거죠?

ChangHoon-Sung commented 1 year ago

@Jaewook-Lee 아직 AWS에 배포 테스트를 진행하지는 않았습니다. 지금 해볼게요.

Jaewook-Lee commented 1 year ago

initJoin 핸들러에서 teams 정보를 message send 하는 방법으로 보내주는 방법 너무 nice한 것 같아요.

Jaewook-Lee commented 1 year ago

dev 스테이지에 배포된 API Gateway에 저도 확인해봤습니다.
initJoin 요청에 대해서는 'Team A, Team B'를 받았습니다.
sendOpinion 요청에 대해서는 timestamp 값을 PostgreSQL DB에 넣는 과정에서 syntax error 를 확인했습니다.

[ERROR] SyntaxError: syntax error at or near "01"
LINE 1: ...S ('user456@example.com', '000001', 1, 2023-04-19 01:29:51.5...
                                                             ^

Traceback (most recent call last):
  File "/var/task/src/utility/websocket.py", line 33, in wrapper
    return func(event, context, wsclient)
  File "/var/task/src/lambda/handler.py", line 183, in send_handler
    psql_cursor.execute(insert_query)

이에 대한 해결로 timestamp 값을 따옴표 기호로 감싸겠습니다.

Jaewook-Lee commented 1 year ago
[ERROR] ForeignKeyViolation: insert or update on table "opinion" violates foreign key constraint "fk_2"
DETAIL:  Key (battleid, roundno)=(000001, 1) is not present in table "round".

Traceback (most recent call last):
  File "/var/task/src/utility/websocket.py", line 37, in wrapper
    return func(event, context, wsclient)
  File "/var/task/src/lambda/handler.py", line 183, in send_handler
    psql_cursor.execute(insert_query)

이 에러는 Postman에서 아래와 같이 sendOpinion 요청을 보냈을 때 얻은 CloudWatch log message입니다.

{
    "action": "sendOpinion",
    "round": 1,
    "opinion": "Hello"
}
ChangHoon-Sung commented 1 year ago

Round가 없어서 그런 것 같아요 On Apr 19, 2023 11:02 +0900, Jaewook-Lee @.***>, wrote:

[ERROR] ForeignKeyViolation: insert or update on table "opinion" violates foreign key constraint "fk_2" DETAIL: Key (battleid, roundno)=(000001, 1) is not present in table "round".

Traceback (most recent call last): File "/var/task/src/utility/websocket.py", line 37, in wrapper return func(event, context, wsclient) File "/var/task/src/lambda/handler.py", line 183, in send_handler psql_cursor.execute(insert_query) 이 에러는 Postman에서 아래와 같이 sendOpinion 요청을 보냈을 때 얻은 CloudWatch log message입니다. { "action": "sendOpinion", "round": 1, "opinion": "Hello" } — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were assigned.Message ID: @.***>

Jaewook-Lee commented 1 year ago

sendOpinion 테스트를 하면서 2가지 문제점이 있습니다.

  1. 같은 battleID로 'initJoin'할 경우 가장 최근에 'initJoin'을 한 connection id만 남는다.
  2. 아래와 같은 에러 메시지 출력
    [ERROR] AttributeError: 'psycopg2.extensions.connection' object has no attribute 'client'
    Traceback (most recent call last):
      File "/var/task/src/utility/websocket.py", line 37, in wrapper
        return func(event, context, wsclient)
      File "/var/task/src/lambda/handler.py", line 184, in send_handler
        psql_ctx.client.commit()

    1번의 원인으로는 ws-connections dynamodb의 키 설정 같습니다. 이 DB의 키 설정은 battle ID를 파티션 키로 설정한 것이 전부입니다.
    테스트 결과, battle ID 값만 같으면 덮어 쓰는 문제가 발생합니다.
    그러나 제가 만들었던 websocket-connections-jwlee-test dynamodb를 보면, battle ID를 파티션 키, connection ID를 정렬 키로 설정해서 모든 connection을 확인할 수 있었습니다.
    따라서 ws-connections DB의 키 설정을 변경할 것을 제안합니다.
    2번의 원인은 잘 모르겠네요. context.py 코드에 분명이 client를 멤버 변수로 선언했는데...