TaskHandler.do_task is now state aware & can return callback's return
TaskHandler.do_task is now state aware and can be called manually. It can now return the callbacks returns. added unit tests to verify early callback is functional. Both persistent and non-persistent tasks. All evennia unit tests pass.
This commit is contained in:
parent
bbc60b0340
commit
16f6edb18d
2 changed files with 49 additions and 12 deletions
|
|
@ -256,7 +256,6 @@ class TaskHandler(object):
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def cancel(self, task_id):
|
def cancel(self, task_id):
|
||||||
"""
|
"""
|
||||||
Stop a task from automatically executing.
|
Stop a task from automatically executing.
|
||||||
|
|
@ -298,7 +297,7 @@ class TaskHandler(object):
|
||||||
Returns:
|
Returns:
|
||||||
True (bool): if the removal completed successfully or if the a
|
True (bool): if the removal completed successfully or if the a
|
||||||
task with the id does not exist.
|
task with the id does not exist.
|
||||||
None, if there was a raised exception
|
None: if there was a raised exception
|
||||||
|
|
||||||
"""
|
"""
|
||||||
d = None
|
d = None
|
||||||
|
|
@ -317,22 +316,41 @@ class TaskHandler(object):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def do_task(self, task_id):
|
def do_task(self, task_id):
|
||||||
"""Execute the task (call its callback).
|
"""
|
||||||
|
Execute the task (call its callback).
|
||||||
|
If calling before timedelay cancel the deferral affliated to this task.
|
||||||
|
Remove the task from the dictionary of current tasks on a successful
|
||||||
|
callback.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
task_id (int): a valid task ID.
|
task_id (int): a valid task ID.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
False (bool): if the:
|
||||||
|
task no longer exists,
|
||||||
|
has no affliated instance of deferral
|
||||||
|
The return of the callback passed on task creation.
|
||||||
|
This makes it possible for the callback to also return False
|
||||||
|
None: if there was a raised exception
|
||||||
|
|
||||||
Note:
|
Note:
|
||||||
This will also remove it from the list of current tasks.
|
On a successful call the task will be removed from the dictionary
|
||||||
|
of current tasks.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
date, callback, args, kwargs, persistent, d = self.tasks.pop(task_id)
|
callback_return = False
|
||||||
|
if task_id in self.tasks:
|
||||||
if task_id in self.to_save:
|
date, callback, args, kwargs, persistent, d = self.tasks.get(task_id)
|
||||||
del self.to_save[task_id]
|
else: # the task does not exist
|
||||||
|
return False
|
||||||
self.save()
|
if d: # it is remotely possible for a task to not have a deferral
|
||||||
callback(*args, **kwargs)
|
if not d.called: # the task has not been called yet
|
||||||
|
d.cancel() # cancel the automated callback
|
||||||
|
else: # this task has no deferral, and should not be called
|
||||||
|
return False
|
||||||
|
callback_return = callback(*args, **kwargs)
|
||||||
|
self.remove(task_id)
|
||||||
|
return callback_return
|
||||||
|
|
||||||
def get_deferred(self, task_id):
|
def get_deferred(self, task_id):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -314,6 +314,7 @@ def dummy_func(obj):
|
||||||
obj = obj[0]
|
obj = obj[0]
|
||||||
# make changes to object
|
# make changes to object
|
||||||
obj.ndb.dummy_var = 'dummy_func ran'
|
obj.ndb.dummy_var = 'dummy_func ran'
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class TestDelay(EvenniaTest):
|
class TestDelay(EvenniaTest):
|
||||||
|
|
@ -335,12 +336,30 @@ class TestDelay(EvenniaTest):
|
||||||
_TASK_HANDLER.clock.advance(timedelay) # make time pass
|
_TASK_HANDLER.clock.advance(timedelay) # make time pass
|
||||||
self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')
|
self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')
|
||||||
self.char1.ndb.dummy_var = False
|
self.char1.ndb.dummy_var = False
|
||||||
|
# test a persistent deferral, that completes on a manual call
|
||||||
|
task_id = utils.delay(timedelay, dummy_func, self.char1.dbref, persistent=True)
|
||||||
|
self.assertTrue(_TASK_HANDLER.active(task_id))
|
||||||
|
result = _TASK_HANDLER.do_task(task_id)
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertFalse(_TASK_HANDLER.exists(task_id))
|
||||||
|
_TASK_HANDLER.clock.advance(timedelay) # make time pass, important keep
|
||||||
|
self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')
|
||||||
|
self.char1.ndb.dummy_var = False
|
||||||
# test a non persisten deferral, that completes after delay time.
|
# test a non persisten deferral, that completes after delay time.
|
||||||
utils.delay(timedelay, dummy_func, self.char1.dbref)
|
utils.delay(timedelay, dummy_func, self.char1.dbref)
|
||||||
self.assertTrue(_TASK_HANDLER.active(task_id))
|
self.assertTrue(_TASK_HANDLER.active(task_id))
|
||||||
_TASK_HANDLER.clock.advance(timedelay) # make time pass
|
_TASK_HANDLER.clock.advance(timedelay) # make time pass
|
||||||
self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')
|
self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')
|
||||||
self.char1.ndb.dummy_var = False
|
self.char1.ndb.dummy_var = False
|
||||||
|
# test a non-persistent deferral, that completes on a manual call
|
||||||
|
task_id = utils.delay(timedelay, dummy_func, self.char1.dbref)
|
||||||
|
self.assertTrue(_TASK_HANDLER.active(task_id))
|
||||||
|
result = _TASK_HANDLER.do_task(task_id)
|
||||||
|
self.assertTrue(result)
|
||||||
|
self.assertFalse(_TASK_HANDLER.exists(task_id))
|
||||||
|
_TASK_HANDLER.clock.advance(timedelay) # make time pass, important keep
|
||||||
|
self.assertEqual(self.char1.ndb.dummy_var, 'dummy_func ran')
|
||||||
|
self.char1.ndb.dummy_var = False
|
||||||
# test a non persisten deferral, with a short timedelay
|
# test a non persisten deferral, with a short timedelay
|
||||||
utils.delay(.1, dummy_func, self.char1.dbref)
|
utils.delay(.1, dummy_func, self.char1.dbref)
|
||||||
self.assertTrue(_TASK_HANDLER.active(task_id))
|
self.assertTrue(_TASK_HANDLER.active(task_id))
|
||||||
|
|
@ -367,7 +386,7 @@ class TestDelay(EvenniaTest):
|
||||||
self.assertEqual(self.char1.ndb.dummy_var, False)
|
self.assertEqual(self.char1.ndb.dummy_var, False)
|
||||||
self.assertFalse(_TASK_HANDLER.exists(task_id))
|
self.assertFalse(_TASK_HANDLER.exists(task_id))
|
||||||
self.char1.ndb.dummy_var = False
|
self.char1.ndb.dummy_var = False
|
||||||
# test removing a canceled active task
|
# test removing a canceled task
|
||||||
task_id = utils.delay(timedelay, dummy_func, self.char1.dbref)
|
task_id = utils.delay(timedelay, dummy_func, self.char1.dbref)
|
||||||
self.assertTrue(_TASK_HANDLER.active(task_id))
|
self.assertTrue(_TASK_HANDLER.active(task_id))
|
||||||
deferal_inst = _TASK_HANDLER.get_deferred(task_id)
|
deferal_inst = _TASK_HANDLER.get_deferred(task_id)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue