Created
January 13, 2014 15:55
-
-
Save ionelmc/8402669 to your computer and use it in GitHub Desktop.
Idle autoshutdown celery bootstep, from http://pastie.org/8629675
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from celery import Celery | |
| from celery import bootsteps | |
| from celery.worker import consumer | |
| class MaxIdleTimeStep(bootsteps.StartStopStep): | |
| idle_max = 1.0 | |
| last_active = None | |
| requires = (consumer.Tasks, ) | |
| def start(self, consumer): | |
| self.tref = consumer.hub.call_repeatedly( | |
| self.idle_max, self.check_active, consumer, | |
| ) | |
| def stop(self, consumer): | |
| if self.tref is not None: | |
| self.tref.cancel() | |
| self.tref = None | |
| shutdown = stop | |
| def check_active(self, consumer): | |
| current = consumer.controller.state.all_total_count[0] | |
| if self.last_active is not None and self.last_active == current: | |
| consumer.controller.state.should_stop = True | |
| self.tref.cancel() | |
| self.last_active = current | |
| app = Celery() | |
| app.steps['consumer'].add(MaxIdleTimeStep) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment