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

[Bug]: (python) how to gracefully shutdown worker? #2199

Closed
1 task done
klaemo opened this issue Sep 26, 2023 · 2 comments
Closed
1 task done

[Bug]: (python) how to gracefully shutdown worker? #2199

klaemo opened this issue Sep 26, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@klaemo
Copy link
Contributor

klaemo commented Sep 26, 2023

Version

1.14.0

Platform

Python

What happened?

I think there is code missing to gracefully shutdown a worker. The Worker.close() method kills the redis connection directly and doesn't wait for pending jobs to finish. As a result the (eventually) computed results gets never reported back.

Maybe I'm holding it wrong but how do you shutdown a worker gracefully the official way?

My workaround looks like this:

class MyWorker(Worker):
    async def close(self, force: bool = False):
        self.closing = True
        if len(self.processing) > 0:
            await asyncio.wait(self.processing, return_when=asyncio.ALL_COMPLETED)
        return await super().close()
    def stop_on_signal(self, signum=signal.SIGINT):
        def _handler_sigint(signum, frame):
            print(' !!! SIGINT - GRACEFUL EXITING... !!!')
            self.closing = True
        signal.signal(signum, _handler_sigint)

async def main():
    worker = MyWorker("channel", handler, {'autorun': False})
    worker.stop_on_signal()
    try:
        print('STARTED')
        await worker.run()
    finally:
        await worker.close()
        print('DONE')

Thank you 🙏

How to reproduce.

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@klaemo klaemo added the bug Something isn't working label Sep 26, 2023
@simonwindt
Copy link

The close() should wait for current jobs to finalise as per documentation;
https://api.docs.bullmq.io/classes/v4.Worker.html#close

This works for us. The problem we have is that close() stop emitting failed event. So jobs that fail after close() are never properly handled in our application.

@klaemo
Copy link
Contributor Author

klaemo commented May 7, 2024

This should be fixed by #2545

@klaemo klaemo closed this as completed May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants