Slightly optimize persistent tasks and serialization
This commit is contained in:
parent
87b6cee596
commit
e0eb490814
1 changed files with 15 additions and 6 deletions
|
|
@ -27,6 +27,7 @@ class PersistentTasks(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tasks = {}
|
self.tasks = {}
|
||||||
|
self.to_save = {}
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
"""Load from the ServerConfig.
|
"""Load from the ServerConfig.
|
||||||
|
|
@ -42,8 +43,9 @@ class PersistentTasks(object):
|
||||||
else:
|
else:
|
||||||
tasks = value
|
tasks = value
|
||||||
|
|
||||||
# At this point, `tasks` contains a unserialized dictionary of tasks
|
# At this point, `tasks` contains a dictionary of still-serialized tasks
|
||||||
for task_id, (date, callback, args, kwargs) in tasks.items():
|
for task_id, value in tasks.items():
|
||||||
|
date, callback, args, kwargs = dbunserialize(value)
|
||||||
if isinstance(callback, tuple):
|
if isinstance(callback, tuple):
|
||||||
# `callback` can be an object and name for instance methods
|
# `callback` can be an object and name for instance methods
|
||||||
obj, method = callback
|
obj, method = callback
|
||||||
|
|
@ -52,8 +54,10 @@ class PersistentTasks(object):
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
"""Save the tasks in ServerConfig."""
|
"""Save the tasks in ServerConfig."""
|
||||||
to_save = {}
|
|
||||||
for task_id, (date, callback, args, kwargs) in self.tasks.items():
|
for task_id, (date, callback, args, kwargs) in self.tasks.items():
|
||||||
|
if task_id in self.to_save:
|
||||||
|
continue
|
||||||
|
|
||||||
if getattr(callback, "__self__", None):
|
if getattr(callback, "__self__", None):
|
||||||
# `callback` is an instance method
|
# `callback` is an instance method
|
||||||
obj = callback.__self__
|
obj = callback.__self__
|
||||||
|
|
@ -72,9 +76,8 @@ class PersistentTasks(object):
|
||||||
else:
|
else:
|
||||||
safe_callback = callback
|
safe_callback = callback
|
||||||
|
|
||||||
to_save[task_id] = (date, safe_callback, args, kwargs)
|
self.to_save[task_id] = dbserialize((date, safe_callback, args, kwargs))
|
||||||
to_save = dbserialize(to_save)
|
ServerConfig.objects.conf("delayed_tasks", self.to_save)
|
||||||
ServerConfig.objects.conf("delayed_tasks", to_save)
|
|
||||||
|
|
||||||
def add(self, timedelay, callback, *args, **kwargs):
|
def add(self, timedelay, callback, *args, **kwargs):
|
||||||
"""Add a new persistent task in the configuration.
|
"""Add a new persistent task in the configuration.
|
||||||
|
|
@ -137,6 +140,9 @@ class PersistentTasks(object):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
del self.tasks[task_id]
|
del self.tasks[task_id]
|
||||||
|
if task_id in self.to_save:
|
||||||
|
del self.to_save[task_id]
|
||||||
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def do_task(self, task_id):
|
def do_task(self, task_id):
|
||||||
|
|
@ -150,6 +156,9 @@ class PersistentTasks(object):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
date, callback, args, kwargs = self.tasks.pop(task_id)
|
date, callback, args, kwargs = self.tasks.pop(task_id)
|
||||||
|
if task_id in self.to_save:
|
||||||
|
del self.to_save[task_id]
|
||||||
|
|
||||||
self.save()
|
self.save()
|
||||||
callback(*args, **kwargs)
|
callback(*args, **kwargs)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue