Skip to content

Instantly share code, notes, and snippets.

@ionelmc
Created January 13, 2014 15:55
Show Gist options
  • Select an option

  • Save ionelmc/8402669 to your computer and use it in GitHub Desktop.

Select an option

Save ionelmc/8402669 to your computer and use it in GitHub Desktop.
Idle autoshutdown celery bootstep, from http://pastie.org/8629675
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