Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polling機能の追加 #2

Merged
merged 3 commits into from
Sep 17, 2022
Merged

Polling機能の追加 #2

merged 3 commits into from
Sep 17, 2022

Conversation

EndoNrak
Copy link
Owner

sqsを指定秒数ごとにポーリングする

@EndoNrak EndoNrak self-assigned this Sep 17, 2022
@@ -0,0 +1,20 @@
# -*- coding:utf-8 -*-
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/management/commands 以下にBaseCommandを継承したクラスを作ると自作コマンドを登録できる
https://docs.djangoproject.com/en/4.1/howto/custom-management-commands/

help = 'polling sqs and execute score_evaluations'

def add_arguments(self, parser):
parser.add_argument('interval', nargs='?', type=int, default=60)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ポーリングの際、sqsにメッセージがなかった時はinterval秒だけ次のメッセージ取得まで待つ
faultは60秒で任意の秒数を設定可能

print("start polling")
while True:
_eval = self.execute()
if _eval is not None:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqsにメッセージがなかった場合はNoneが返される
メッセージが取得できた場合には、評価が終了するとすぐに次のメッセージ取得を試行する

}
return response

message = str(msg.SerializeToString())
message = str(base64.b64encode(msg.SerializeToString()))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api-gateway経由のリクエストも一度base64decodeしてパースした後、
シリアライズしたメッセージをbase64encodeしてからsqsに登録する。

@@ -33,13 +33,13 @@ def lambda_handler(event: dict, context):
"body": response,
"code": 200
}
except:
except Exception as e:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tryで発生したエラーをすべてキャッチしてレスポンスに返す

@EndoNrak
Copy link
Owner Author

コンテナ内でpollingがうまく動くことを確認した

# make polling
python manage.py polling 60
start polling
2022-09-17 17:23:18.045158
no message in sqs, wait for 60 s
2022-09-17 17:24:18.339383
no message in sqs, wait for 60 s
2022-09-17 17:25:18.495844
start evaluation:  https://github.com/seigot/tetris | master
finish evaluation:  https://github.com/seigot/tetris | master
result   {"id": "6e560182-9077-47d3-81fd-4c4330b539ed", "created_at": "2022-09-17 17:23:17.548400", "ended_at": "2022-09-17 17:25:32.778862", "repository_url": "https://github.com/seigot/tetris", "branch": "master", "game_time": 10, "level": 1, "error_message": "", "status": "S", "drop_interval": 1000, "game_mode": "default", "value_predict_weight": "", "trial_num": 1, "score_mean": 167, "score_stdev": 0, "score_max": 167, "score_min": 167}
2022-09-17 17:25:32.815003
start evaluation:  https://github.com/seigot/tetris |
finish evaluation:  https://github.com/seigot/tetris |
result   {"id": "6e560182-9077-47d3-81fd-4c4330b539ed", "created_at": "2022-09-17 17:23:17.548400", "ended_at": "2022-09-17 17:25:33.661485", "repository_url": "https://github.com/seigot/tetris", "branch": "", "game_time": 0, "level": 1000, "error_message": "Cloning into 'tetris'...\nwarning: Could not find remote branch tetris to clone.\nfatal: Remote branch tetris not found in upstream origin\n", "status": "ER", "drop_interval": 0, "game_mode": "", "value_predict_weight": "", "trial_num": 0, "score_mean": 0, "score_stdev": 0, "score_max": 0, "score_min": 0}
2022-09-17 17:25:33.661984
no message in sqs, wait for 60 s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant