|
|
@ -2,6 +2,7 @@
|
|
|
|
from __future__ import (absolute_import, division, print_function,
|
|
|
|
from __future__ import (absolute_import, division, print_function,
|
|
|
|
unicode_literals)
|
|
|
|
unicode_literals)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
import errno
|
|
|
|
import errno
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import os
|
|
|
@ -20,7 +21,7 @@ from .exceptions import DequeueTimeout, NoQueueError
|
|
|
|
from .job import Job, JobStatus
|
|
|
|
from .job import Job, JobStatus
|
|
|
|
from .logutils import setup_loghandlers
|
|
|
|
from .logutils import setup_loghandlers
|
|
|
|
from .queue import get_failed_queue, Queue
|
|
|
|
from .queue import get_failed_queue, Queue
|
|
|
|
from .registry import FinishedJobRegistry, StartedJobRegistry
|
|
|
|
from .registry import clean_registries, FinishedJobRegistry, StartedJobRegistry
|
|
|
|
from .suspension import is_suspended
|
|
|
|
from .suspension import is_suspended
|
|
|
|
from .timeouts import UnixSignalDeathPenalty
|
|
|
|
from .timeouts import UnixSignalDeathPenalty
|
|
|
|
from .utils import enum, import_attribute, make_colorizer, utcformat, utcnow, utcparse
|
|
|
|
from .utils import enum, import_attribute, make_colorizer, utcformat, utcnow, utcparse
|
|
|
@ -145,6 +146,7 @@ class Worker(object):
|
|
|
|
self._stopped = False
|
|
|
|
self._stopped = False
|
|
|
|
self.log = logger
|
|
|
|
self.log = logger
|
|
|
|
self.failed_queue = get_failed_queue(connection=self.connection)
|
|
|
|
self.failed_queue = get_failed_queue(connection=self.connection)
|
|
|
|
|
|
|
|
self.maintenance_date = None
|
|
|
|
|
|
|
|
|
|
|
|
# By default, push the "move-to-failed-queue" exception handler onto
|
|
|
|
# By default, push the "move-to-failed-queue" exception handler onto
|
|
|
|
# the stack
|
|
|
|
# the stack
|
|
|
@ -408,6 +410,9 @@ class Worker(object):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.check_for_suspension(burst)
|
|
|
|
self.check_for_suspension(burst)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.should_run_maintenance_tasks:
|
|
|
|
|
|
|
|
self.clean_registries()
|
|
|
|
|
|
|
|
|
|
|
|
if self.stopped:
|
|
|
|
if self.stopped:
|
|
|
|
self.log.info('Stopping on request.')
|
|
|
|
self.log.info('Stopping on request.')
|
|
|
|
break
|
|
|
|
break
|
|
|
@ -614,7 +619,7 @@ class Worker(object):
|
|
|
|
'arguments': job.args,
|
|
|
|
'arguments': job.args,
|
|
|
|
'kwargs': job.kwargs,
|
|
|
|
'kwargs': job.kwargs,
|
|
|
|
'queue': job.origin,
|
|
|
|
'queue': job.origin,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
for handler in reversed(self._exc_handlers):
|
|
|
|
for handler in reversed(self._exc_handlers):
|
|
|
|
self.log.debug('Invoking exception handler %s' % (handler,))
|
|
|
|
self.log.debug('Invoking exception handler %s' % (handler,))
|
|
|
@ -652,6 +657,21 @@ class Worker(object):
|
|
|
|
"""The hash does not take the database/connection into account"""
|
|
|
|
"""The hash does not take the database/connection into account"""
|
|
|
|
return hash(self.name)
|
|
|
|
return hash(self.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clean_registries(self):
|
|
|
|
|
|
|
|
"""Runs maintenance jobs on each Queue's registries."""
|
|
|
|
|
|
|
|
for queue in self.queues:
|
|
|
|
|
|
|
|
clean_registries(queue)
|
|
|
|
|
|
|
|
self.maintenance_date = utcnow()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def should_run_maintenance_tasks(self):
|
|
|
|
|
|
|
|
"""Maintenance tasks should run on first startup or every hour."""
|
|
|
|
|
|
|
|
if self.maintenance_date is None:
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
if (utcnow() - self.maintenance_date) > timedelta(seconds=3600):
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleWorker(Worker):
|
|
|
|
class SimpleWorker(Worker):
|
|
|
|
def main_work_horse(self, *args, **kwargs):
|
|
|
|
def main_work_horse(self, *args, **kwargs):
|
|
|
|