Skip to content

Commit

Permalink
Merge pull request #98 from plone/use-std-library-signal-handlers
Browse files Browse the repository at this point in the history
Modernize signal handling
  • Loading branch information
ale-rt authored Feb 7, 2019
2 parents 3a7d3e7 + 1c25bb2 commit 23fd509
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ New features:

Bug fixes:

- Use the standard library signal module instead of the ZServer Signal module
(Fixes #97)
[ale-rt]

- Fix the travis build which is broken since we are picking a random port
(fixes #100)
[ale-rt]
Expand Down
32 changes: 18 additions & 14 deletions src/plone/app/robotframework/reload.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# -*- coding: utf-8 -*-

from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

import os
import signal
import time

from Signals.SignalHandler import SignalHandler
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

def TIME():
return time.strftime('%H:%M:%S')


registerHandler = SignalHandler.registerHandler
def WAIT(msg):
return '{0} [\033[33m wait \033[0m] {1}'.format(TIME(), msg)


TIME = lambda: time.strftime('%H:%M:%S')
WAIT = lambda msg: '{0} [\033[33m wait \033[0m] {1}'.format(TIME(), msg)
ERROR = lambda msg: '{0} [\033[31m ERROR \033[0m] {1}'.format(TIME(), msg)
def ERROR(msg):
return '{0} [\033[31m ERROR \033[0m] {1}'.format(TIME(), msg)


class Watcher(FileSystemEventHandler):
Expand All @@ -33,8 +36,8 @@ def start(self):
"""Start file monitoring thread
"""

registerHandler(signal.SIGINT, self._exitHandler)
registerHandler(signal.SIGTERM, self._exitHandler)
signal.signal(signal.SIGINT, self._exitHandler)
signal.signal(signal.SIGTERM, self._exitHandler)

for path in self.paths:
print(WAIT("Watchdog is watching for changes in %s" % path))
Expand Down Expand Up @@ -116,23 +119,24 @@ def start(self):
"""
# SIGCHLD tells us that child process has really died and we can spawn
# new child
registerHandler(signal.SIGCHLD, self._waitChildToDieAndScheduleNew)
signal.signal(signal.SIGCHLD, self._waitChildToDieAndScheduleNew)

# With SIGUSR1 child can tell that it dies by request, not by exception
# etc.
registerHandler(signal.SIGUSR1, self._childIsGoingToDie)
signal.signal(signal.SIGUSR1, self._childIsGoingToDie)

self.loop()

def loop(self):
"""Magic happens
"""
registerHandler(signal.SIGINT, self._parentExitHandler)
registerHandler(signal.SIGTERM, self._parentExitHandler)
signal.signal(signal.SIGINT, self._parentExitHandler)
signal.signal(signal.SIGTERM, self._parentExitHandler)

self.active = True

print(WAIT("Fork loop now starting on parent process %i" % os.getpid()))
msg = "Fork loop now starting on parent process %i" % os.getpid()
print(WAIT(msg))
while True:
self.forking = False

Expand Down

0 comments on commit 23fd509

Please sign in to comment.