Skip to content

Commit

Permalink
Refactored Zope2 dependencies from ForkLoop into Zope2ForkLoop.
Browse files Browse the repository at this point in the history
  • Loading branch information
datakurre committed Oct 3, 2011
1 parent 0d5406d commit 360d7c0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 38 deletions.
4 changes: 2 additions & 2 deletions sauna/reload/__init__.py
Expand Up @@ -28,7 +28,7 @@
import os


from sauna.reload.forkloop import ForkLoop
from sauna.reload.forkloop import Zope2ForkLoop as ForkLoop
from sauna.reload.reloadpaths import ReloadPaths

reload_paths = ReloadPaths([os.path.join(os.getcwd(), p)
Expand All @@ -37,6 +37,6 @@
forkloop = ForkLoop()
forkloop.startBootTimer()

# Hook into PEP 302 laoder
# Hook into PEP 302 loader
from sauna.reload.monkeypatcher import MonkeyPatchingLoader
__loader__ = MonkeyPatchingLoader(sys.modules[__name__])
102 changes: 66 additions & 36 deletions sauna/reload/forkloop.py
Expand Up @@ -52,9 +52,6 @@ def __init__(self):
self.boot_started = None
self.child_started = None

self.cfg = None
self.storage_index = None

def isChild(self):
return self.child_pid == 0

Expand All @@ -66,12 +63,7 @@ def startChildBooTimer(self):
self.child_started = time.time()

def isChildAlive(self):

if self.isChild():
return True

return (self.child_pid is not None
and os.path.exists("/proc/%i" % self.child_pid))
return self.isChild()

def _scheduleFork(self, signum=None, frame=None):
self.fork = True
Expand All @@ -84,14 +76,6 @@ def start(self):
Start fork loop.
"""

# Load configuration here to make sure that everything for it is loaded
self.cfg = getConfiguration()

# Must import here because we don't have DB on bootup yet
from Globals import DB
# TODO: Fetch adapter with interface
self.storage_index = FileStorageIndex(DB.storage)

# SIGCHLD tells us that child process has really died and we can spawn
# new child
registerHandler(signal.SIGCHLD, self._waitChildToDieAndScheduleNew)
Expand Down Expand Up @@ -166,8 +150,6 @@ def loop(self):
logger.info("Booted up new child in %s seconds. PID %i" % (
time.time() - self.child_started, os.getpid()))

notify(NewChildIsReady(self))

def _prepareNewChild(self):
"""
Prepare newly forked child. Make sure that it can properly read DB
Expand All @@ -178,18 +160,6 @@ def _prepareNewChild(self):
# get a modified event. Must wait that child has closed database etc.
atexit.register(self._childExitHandler)

# Make sure that PID files and locks stay here, because dying child
# will clear them.
self.makeLockFile()
self.makePidFile()

self.storage_index.restore()

notify(NewChildForked(self))

autoinclude.include_deferred()
fiveconfigure.install_deferred()

def spawnNewChild(self):
"""
STEP 1 (parent): New child spawning starts by killing the current
Expand Down Expand Up @@ -239,11 +209,7 @@ def _parentExitHandler(self, signum=None, frame=None):
self._killChild()

def _childExitHandler(self):
"""
STEP 2 (child): Child is about to die. Fix DB.
"""

self.storage_index.save()
pass

def _waitChildToDieAndScheduleNew(self, signal=None, frame=None):
"""
Expand All @@ -258,6 +224,70 @@ def _waitChildToDieAndScheduleNew(self, signal=None, frame=None):
# Schedule new
self._scheduleFork()


class Zope2ForkLoop(ForkLoop):

def __init__(self):
super(Zope2ForkLoop, self).__init__()
self.cfg = None
self.storage_index = None

def isChildAlive(self):

if self.isChild():
return True

return (self.child_pid is not None
and os.path.exists("/proc/%i" % self.child_pid))

def start(self):
"""
Start fork loop.
"""

# Load configuration here to make sure that everything for it is loaded
self.cfg = getConfiguration()

# Must import here because we don't have DB on bootup yet
from Globals import DB
# TODO: Fetch adapter with interface
self.storage_index = FileStorageIndex(DB.storage)

super(Zope2ForkLoop, self).start()

def loop(self):
"""
Magic happens here
"""
super(Zope2ForkLoop, self).loop()
notify(NewChildIsReady(self))

def _prepareNewChild(self):
"""
Prepare newly forked child. Make sure that it can properly read DB
and install deferred products.
"""
super(Zope2ForkLoop, self)._prepareNewChild()

# Make sure that PID files and locks stay here, because dying child
# will clear them.
self.makeLockFile()
self.makePidFile()

self.storage_index.restore()

notify(NewChildForked(self))

autoinclude.include_deferred()
fiveconfigure.install_deferred()

def _childExitHandler(self):
"""
STEP 2 (child): Child is about to die. Fix DB.
"""
super(Zope2ForkLoop, self).loop()
self.storage_index.save()

# Modified from Zope2/Startup/__init__.py
def makePidFile(self):

Expand Down

0 comments on commit 360d7c0

Please sign in to comment.