LoginSignup
12
10

More than 3 years have passed since last update.

LINE Bot + Python + Heroku で「Error R10 (Boot timeout) 」エラーが出たときの解消方法

Posted at

背景

なんかLINEボット作りたくなってきた...ムズムズ
お、LINEの公式サイト充実の極みじゃん、サンプルもあるし親切すぎる〜:joy:
...と思いきやスムーズに行かない〜!

対象

ということで、LINE Developersの公式ドキュメント内の「Herokuでサンプルボットを作成する」をPythonで試そうとして、以下のようなエラーが出た方向けに書きました。

発生したエラー

デプロイ時

heroku_log
2019-10-07T11:42:01.490866+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
[中略]
2019-10-07T11:42:01.64401+00:00 heroku[web.1]: Process exited with status 137
2019-10-07T11:42:01.692739+00:00 heroku[web.1]: State changed from starting to crashed

LINE Bot でメッセージ送信時

heroku_log
2019-10-07T11:42:02.884452+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/callback" host={herokuのapp名}.herokuapp.com request_id={xxxxxx} fwd="147.92.150.194" dyno= connect= service= status=503 bytes= protocol=https

原因

ホスト名とポート番号に原因がある可能性がある。

ソースコードの原因箇所と変更点

元ソースコードはこちら使わせていただいております。
https://github.com/line/line-bot-sdk-python/tree/master/examples

変更前

app.py[抜粋]
if __name__ == "__main__":
    arg_parser = ArgumentParser(
        usage='Usage: python ' + __file__ + ' [--port <port>] [--help]'
    )
    arg_parser.add_argument('-p', '--port', type=int, default=8000, help='port')
    arg_parser.add_argument('-d', '--debug', default=False, help='debug')
    options = arg_parser.parse_args()

    app.run(debug=options.debug, port=options.port)

変更後

app.py[抜粋]
if __name__ == "__main__":
    arg_parser = ArgumentParser(
        usage='Usage: python ' + __file__ + ' [--port <port>] [--help]'
    )
    arg_parser.add_argument('-p', '--port', type=int, default=int(os.environ.get('PORT', 8000)), help='port')
    arg_parser.add_argument('-d', '--debug', default=False, help='debug')
    arg_parser.add_argument('--host', default='0.0.0.0', help='host')
    options = arg_parser.parse_args()

    app.run(debug=options.debug, host=options.host, port=options.port)

変更点

  • ポート番号を「8000」固定値ではなく、Herokuが毎回動的に割り当てている環境変数$PORTを使用するようにしました。
  • ホスト名を指定していないと、http://127.0.0.1:xxxx/が割り当てられ、外部からアクセスができなくなっていたので、「0.0.0.0」と指定するようにしました。

参考: Herokuドキュメントの説明箇所

Webサーバー
Each web process simply binds to a port, and listens for requests coming in on that port. The port to bind to is assigned by Heroku as the PORT environment variable.
(ウェブプロセスはポートにバインドし、そのポートにリクエストが来るのを待っている。そのポート番号はHerokuが環境変数$PORTとして割り当てている)
https://devcenter.heroku.com/articles/runtime-principles#web-servers

Web dynos
A web dyno must bind to its assigned $PORT within 60 seconds of startup. If it doesn’t, it is terminated by the dyno manager and a R10 Boot Timeout error is logged. Processes can bind to other ports before and after binding to $PORT.
https://devcenter.heroku.com/articles/dynos#web-dynos

R10 - Boot timeout
A web process took longer than 60 seconds to bind to its assigned `$PORTP. When this happens, the dyno’s process is killed and the dyno is considered crashed. Crashed dynos are restarted according to the dyno manager’s restart policy.
This error is often caused by a process being unable to reach an external resource, such as a database, or the application doing too much work, such as parsing and evaluating numerous, large code dependencies, during startup.
https://devcenter.heroku.com/articles/error-codes#r10-boot-timeout

おわりに

サンプル真似するだけでもつまずく点ってやっぱりあるもんだな、と思いました。
でもおかげで一つわかることが増えた気がします。
もし記述内容に私の誤認や記述間違いがありましたら、ご教授お願いいたします:ok_woman:

12
10
0

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
12
10