如何解决使用 Heroku 启动的 R12(退出超时)问题?

How can I resolve an R12 (Exit timeout) launching with Heroku?

我正在使用 Heroku 来托管一个我一直在使用的机器人,当我在本地启动代码时,代码本身就可以完美运行。但是,我昨天更新了机器人,添加了一些新功能,我在检查日志时收到此错误代码:

2021-08-29T13:42:44.000000+00:00 app[api]: Build succeeded
2021-08-29T13:43:05.090490+00:00 heroku[worker.1]: Error R12 (Exit timeout) -> At least one process failed to exit within 30 seconds of SIGTERM
2021-08-29T13:43:05.095578+00:00 heroku[worker.1]: Stopping remaining processes with SIGKILL
2021-08-29T13:43:05.174771+00:00 heroku[worker.1]: Process exited with status 137
2021-08-29T13:56:00.000000+00:00 app[api]: Build started by user ty.unsworth@gmail.com
2021-08-29T13:56:24.413396+00:00 app[api]: Deploy 5ff2d18b by user ty.unsworth@gmail.com
2021-08-29T13:56:24.413396+00:00 app[api]: Release v39 created by user ty.unsworth@gmail.com
2021-08-29T13:56:26.596593+00:00 heroku[worker.1]: Restarting
2021-08-29T13:56:26.610525+00:00 heroku[worker.1]: State changed from up to starting
2021-08-29T13:56:27.284912+00:00 heroku[worker.1]: Stopping all processes with SIGTERM
2021-08-29T13:56:29.770892+00:00 heroku[worker.1]: Starting process with command `python WagonCounterBot.py`
2021-08-29T13:56:30.497452+00:00 heroku[worker.1]: State changed from starting to up
2021-08-29T13:56:34.000000+00:00 app[api]: Build succeeded
2021-08-29T13:56:57.473450+00:00 heroku[worker.1]: Error R12 (Exit timeout) -> At least one process failed to exit within 30 seconds of SIGTERM
2021-08-29T13:56:57.481783+00:00 heroku[worker.1]: Stopping remaining processes with SIGKILL
2021-08-29T13:56:57.535059+00:00 heroku[worker.1]: Process exited with status 137

我认为这个错误可能是我添加的新功能引起的:

@client.listen()
async def on_message(message):
    """
    Looks for when a member calls 'bhwagon', and after 24 minutes, sends them a message
    :param message: the message sent by the user
    :return: a DM to the user letting them know their cooldown ended
    """
    channel = client.get_channel(int(WAGON_CHANNEL))  # sets the

    if message.content.startswith("bhwagon"):
        channel = message.channel
        await cool_down_ended(message)


async def cool_down_ended(message):
    """
    Sends the author of the message a personal DM 24 minutes after they type 'bhwagon' in the guild
    :param message: is the message the author sent
    :return: a message to the author
    """
    time.sleep(1440)  # sets a time for 24 minutes = 1440 seconds

    await message.author.send("Your wagon steal timer is up  time for another materials run!")

所以我想我理解这个错误是 Heroku 不允许函数自身延迟超过 30 秒?与延迟 24 分钟的 cool_down_ended(message) 冲突。 有什么简单的方法可以解决这个问题吗?

  1. 不要在异步代码中使用 time.sleep,它会阻塞整个线程。请改用 await asyncio.sleep(delay)
  2. 这是从 Heroku 处理 SIGTERM 的代码(您可以在 client.run 调用之前添加它)
import signal

signal.signal(signal.SIGTERM, lambda *_: client.loop.create_task(client.close()))