OnComplete handler for events and async re-use
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from axl.axel import Event
|
||||
from couchpotato.core.helpers.variable import mergeDicts, natcmp
|
||||
from couchpotato.core.logger import CPLog
|
||||
import thread
|
||||
import threading
|
||||
import traceback
|
||||
|
||||
@@ -53,6 +54,13 @@ def fireEvent(name, *args, **kwargs):
|
||||
is_after_event = True
|
||||
except: pass
|
||||
|
||||
# onComplete event
|
||||
on_complete = False
|
||||
try:
|
||||
on_complete = kwargs['on_complete']
|
||||
del kwargs['on_complete']
|
||||
except: pass
|
||||
|
||||
# Return single handler
|
||||
single = False
|
||||
try:
|
||||
@@ -129,24 +137,23 @@ def fireEvent(name, *args, **kwargs):
|
||||
if not is_after_event:
|
||||
fireEvent('%s.after' % name, is_after_event = True)
|
||||
|
||||
if on_complete:
|
||||
on_complete()
|
||||
|
||||
return results
|
||||
except KeyError, e:
|
||||
pass
|
||||
except Exception:
|
||||
log.error('%s: %s' % (name, traceback.format_exc()))
|
||||
|
||||
def fireEventAsync(name, *args, **kwargs):
|
||||
#log.debug('Async "%s": %s, %s' % (name, args, kwargs))
|
||||
def fireEventAsync(*args, **kwargs):
|
||||
try:
|
||||
e = events[name]
|
||||
e.lock.acquire()
|
||||
e.asynchronous = True
|
||||
e.error_handler = errorHandler
|
||||
e(*args, **kwargs)
|
||||
e.lock.release()
|
||||
my_thread = threading.Thread(target = fireEvent, args = args, kwargs = kwargs)
|
||||
my_thread.setDaemon(True)
|
||||
my_thread.start()
|
||||
return True
|
||||
except Exception, e:
|
||||
log.error('%s: %s' % (name, e))
|
||||
log.error('%s: %s' % (args[0], e))
|
||||
|
||||
def errorHandler(error):
|
||||
etype, value, tb = error
|
||||
|
||||
Reference in New Issue
Block a user