mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-09 12:38:12 +08:00
Django's timezone is back since it can't conflict with JsonPickle anymore
This commit is contained in:
+11
-19
@@ -5,9 +5,6 @@ from __future__ import division
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from builtins import dict
|
from builtins import dict
|
||||||
from builtins import range
|
from builtins import range
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
from django.utils.timezone import make_aware
|
|
||||||
from future import standard_library
|
from future import standard_library
|
||||||
|
|
||||||
standard_library.install_aliases()
|
standard_library.install_aliases()
|
||||||
@@ -34,9 +31,10 @@ import redis
|
|||||||
|
|
||||||
# Django
|
# Django
|
||||||
from django.core import signing
|
from django.core import signing
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
# Local
|
# Local
|
||||||
from .conf import LOG_LEVEL, SECRET_KEY, SAVE_LIMIT, WORKERS, COMPRESSED, PREFIX, USE_TZ
|
from .conf import LOG_LEVEL, SECRET_KEY, SAVE_LIMIT, WORKERS, COMPRESSED, PREFIX
|
||||||
from .humanhash import uuid
|
from .humanhash import uuid
|
||||||
from .models import Task, Success
|
from .models import Task, Success
|
||||||
|
|
||||||
@@ -53,12 +51,6 @@ STOPPING = 'Stopping'
|
|||||||
|
|
||||||
r = redis.StrictRedis()
|
r = redis.StrictRedis()
|
||||||
|
|
||||||
# JsonPickle seems to have a problem with django's timezone.now
|
|
||||||
def time_zone(value):
|
|
||||||
if USE_TZ:
|
|
||||||
return make_aware(value)
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
class Cluster(object):
|
class Cluster(object):
|
||||||
def __init__(self, list_key=Q_LIST):
|
def __init__(self, list_key=Q_LIST):
|
||||||
@@ -143,7 +135,7 @@ class Sentinel(object):
|
|||||||
self.list_key = list_key
|
self.list_key = list_key
|
||||||
self.status = None
|
self.status = None
|
||||||
self.reincarnations = 0
|
self.reincarnations = 0
|
||||||
self.tob = datetime.utcnow()
|
self.tob = timezone.now()
|
||||||
self.stop_event = stop_event
|
self.stop_event = stop_event
|
||||||
self.start_event = start_event
|
self.start_event = start_event
|
||||||
self.pool_size = WORKERS
|
self.pool_size = WORKERS
|
||||||
@@ -275,7 +267,7 @@ def worker(task_queue, done_queue):
|
|||||||
continue
|
continue
|
||||||
except signing.BadSignature as e:
|
except signing.BadSignature as e:
|
||||||
task['name'] = task['name'].rsplit(":", 1)[0]
|
task['name'] = task['name'].rsplit(":", 1)[0]
|
||||||
task['stopped'] = datetime.utcnow()
|
task['stopped'] = timezone.now()
|
||||||
task['result'] = e
|
task['result'] = e
|
||||||
task['success'] = False
|
task['success'] = False
|
||||||
done_queue.put(task)
|
done_queue.put(task)
|
||||||
@@ -286,13 +278,13 @@ def worker(task_queue, done_queue):
|
|||||||
m = importlib.import_module(module)
|
m = importlib.import_module(module)
|
||||||
f = getattr(m, func)
|
f = getattr(m, func)
|
||||||
task['result'] = f(*task['args'], **task['kwargs'])
|
task['result'] = f(*task['args'], **task['kwargs'])
|
||||||
task['stopped'] = datetime.utcnow()
|
task['stopped'] = timezone.now()
|
||||||
task['success'] = True
|
task['success'] = True
|
||||||
done_queue.put(task)
|
done_queue.put(task)
|
||||||
gc.collect()
|
gc.collect()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
task['result'] = e
|
task['result'] = e
|
||||||
task['stopped'] = datetime.utcnow()
|
task['stopped'] = timezone.now()
|
||||||
task['success'] = False
|
task['success'] = False
|
||||||
done_queue.put(task)
|
done_queue.put(task)
|
||||||
logger.info('{} stopped doing work'.format(name))
|
logger.info('{} stopped doing work'.format(name))
|
||||||
@@ -306,8 +298,8 @@ def save_task(task):
|
|||||||
hook=task['hook'],
|
hook=task['hook'],
|
||||||
args=task['args'],
|
args=task['args'],
|
||||||
kwargs=task['kwargs'],
|
kwargs=task['kwargs'],
|
||||||
started=time_zone(task['started']),
|
started=task['started'],
|
||||||
stopped=time_zone(task['stopped']),
|
stopped=task['stopped'],
|
||||||
result=task['result'],
|
result=task['result'],
|
||||||
success=task['success'])
|
success=task['success'])
|
||||||
|
|
||||||
@@ -328,7 +320,7 @@ def async(func, *args, **kwargs):
|
|||||||
del kwargs['list_key']
|
del kwargs['list_key']
|
||||||
else:
|
else:
|
||||||
list_key = Q_LIST
|
list_key = Q_LIST
|
||||||
task = {'name': uuid()[0], 'func': func, 'hook': hook, 'args': args, 'kwargs': kwargs, 'started': datetime.utcnow()}
|
task = {'name': uuid()[0], 'func': func, 'hook': hook, 'args': args, 'kwargs': kwargs, 'started': timezone.now()}
|
||||||
pack = SignedPackage.dumps(task)
|
pack = SignedPackage.dumps(task)
|
||||||
r.rpush(list_key, pack)
|
r.rpush(list_key, pack)
|
||||||
logger.debug('Pushed {}'.format(pack))
|
logger.debug('Pushed {}'.format(pack))
|
||||||
@@ -384,7 +376,7 @@ class Status(object):
|
|||||||
self.monitor = 0
|
self.monitor = 0
|
||||||
self.task_q_size = 0
|
self.task_q_size = 0
|
||||||
self.pusher = 0
|
self.pusher = 0
|
||||||
self.timestamp = datetime.utcnow()
|
self.timestamp = timezone.now()
|
||||||
|
|
||||||
|
|
||||||
class Stat(Status):
|
class Stat(Status):
|
||||||
@@ -404,7 +396,7 @@ class Stat(Status):
|
|||||||
self.workers.append(w.pid)
|
self.workers.append(w.pid)
|
||||||
|
|
||||||
def uptime(self):
|
def uptime(self):
|
||||||
return (datetime.utcnow() - self.tob).total_seconds()
|
return (timezone.now() - self.tob).total_seconds()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def key(self):
|
def key(self):
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class Command(BaseCommand):
|
|||||||
else:
|
else:
|
||||||
status = term.yellow(stat.status)
|
status = term.yellow(stat.status)
|
||||||
# format uptime
|
# format uptime
|
||||||
uptime = (timezone.now() - timezone.make_aware(stat.tob)).total_seconds()
|
uptime = (timezone.now() - stat.tob).total_seconds()
|
||||||
hours, remainder = divmod(uptime, 3600)
|
hours, remainder = divmod(uptime, 3600)
|
||||||
minutes, seconds = divmod(remainder, 60)
|
minutes, seconds = divmod(remainder, 60)
|
||||||
uptime = '%d:%02d:%02d' % (hours, minutes, seconds)
|
uptime = '%d:%02d:%02d' % (hours, minutes, seconds)
|
||||||
|
|||||||
Reference in New Issue
Block a user