mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-05-11 10:34:30 +08:00
Avoid deep recursion when handling a large queue of events
Because each event handler wrapper would call scheduler.run() at the end before
returning, with a queue of 100 events to process we'd end up
with:
Event handler 1 wrapper
scheduler.run()
Event handler 2 wrapper
scheduler.run()
.....
.... Event handler 100 wrapper
Which would eventually exhaust the Python stack limit. Fix this by making scheduler.run()
no-op if the scheduler is already processing the queue in the current thread.
This commit is contained in:
@@ -15,6 +15,7 @@ however Brew sends to topic brew.build.complete, etc.
|
|||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import sched
|
import sched
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from module_build_service.common import log
|
from module_build_service.common import log
|
||||||
@@ -40,6 +41,10 @@ class Scheduler(sched.scheduler):
|
|||||||
are executed.
|
are executed.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
sched.scheduler.__init__(self, *args, **kwargs)
|
||||||
|
self.local = threading.local()
|
||||||
|
|
||||||
def add(self, handler, arguments=()):
|
def add(self, handler, arguments=()):
|
||||||
"""
|
"""
|
||||||
Schedule execution of `handler` with `arguments`.
|
Schedule execution of `handler` with `arguments`.
|
||||||
@@ -50,10 +55,20 @@ class Scheduler(sched.scheduler):
|
|||||||
"""
|
"""
|
||||||
Runs scheduled handlers.
|
Runs scheduled handlers.
|
||||||
"""
|
"""
|
||||||
log.debug("Running event scheduler with following events:")
|
if getattr(self.local, 'running', False):
|
||||||
for event in self.queue:
|
return
|
||||||
log.debug(" %r", event)
|
|
||||||
sched.scheduler.run(self)
|
try:
|
||||||
|
self.local.running = True
|
||||||
|
# Note that events that are added during the execution of the
|
||||||
|
# handlers are executed as part of the .run() call without
|
||||||
|
# further logging.
|
||||||
|
log.debug("Running event scheduler with following events:")
|
||||||
|
for event in self.queue:
|
||||||
|
log.debug(" %r", event)
|
||||||
|
sched.scheduler.run(self)
|
||||||
|
finally:
|
||||||
|
self.local.running = False
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user