0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure Bot Framework SDK Python sample を App Service にデプロイした際に起動できないエラーの対処法

Posted at

はじめに

Azure Bot Service を試す場合、Bot Framweork SDK を使ったチュートリアルとしては下記が提供されています。

この記事では、以下の Echo Bot がサンプルコードとして提供されている。

この記事自体は、Bot Framework Emulator を利用して Bot の動作確認をするところまでが紹介されており、そこまでは問題なく動作する。

ただし、記事にリンクされた App Service へのデプロイで上記サンプルをそのままデプロイすると、App Service 上では、正しく起動できず、Web Chat をはじめ Botのバックエンドが動作しない状態となります。

発生しうるエラー

App Service 上で、アプリが起動できず、.azurewebsites.net には 504.0 GatewayTimeout 応答が返されることになります。
Bot Service からのエンドポイントに指定してある アプリ名.azurewebsites.net/api/messages も応答を得られない状態となります。
その結果、Web Chat をはじめ、Bot が何の応答も返さないという状態となります。

Bot Service のポータルからは以下のような表示となります。

image.png

App Service 側の起動ログは以下のような内容で再起動が繰り返されることになります。

image.png

App Service のログについては以下の記事を参考

回避策

スタートアップコマンドに以下を追記することで正常にアプリケーションが起動できます。

gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP

image.png

解説

最初のチュートリアルでローカル起動させる際は、python app.py でプログラムをスタートさせていました。
一方 App Service では、下記の記事にあるとおり、Python アプリ用のスタートアップコマンドが自動で生成されるものとなります。

サンプルの EchoBot は Flask プロジェクトではありませんが、app.pyが含まれるため、以下の起動スクリプトがデフォルトで適用されます。

gunicorn --bind=0.0.0.0 --timeout 600 app:app

一方で app.py の中身は以下の通り、APP という名前で aiohttp のサーバーを提供しています。

app.py(L79~)
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)

if __name__ == "__main__":
    try:
        web.run_app(APP, host="localhost", port=CONFIG.PORT)
    except Exception as error:
        raise error

そのため、app.pygunicorn から起動できるようにするためには、前述のカスタムスタートアップコマンドが必要となります。

Oryx によるスタートアップコマンドとカスタムコマンドについては下記記事も参考にしてください。

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?