Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
c6ed1908
Commit
c6ed1908
authored
Jul 24, 2016
by
Hanno Schlichting
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make the dependency between Signals and Lifetime optional.
parent
966f7dfa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
40 deletions
+60
-40
src/Lifetime/__init__.py
src/Lifetime/__init__.py
+33
-0
src/Signals/Signals.py
src/Signals/Signals.py
+12
-33
src/Signals/WinSignalHandler.py
src/Signals/WinSignalHandler.py
+15
-7
No files found.
src/Lifetime/__init__.py
View file @
c6ed1908
import
asyncore
import
logging
import
sys
import
time
from
Signals.threads
import
dump_threads
logger
=
logging
.
getLogger
(
"Z2"
)
_shutdown_phase
=
0
_shutdown_timeout
=
30
# seconds per phase
...
...
@@ -84,3 +90,30 @@ def graceful_shutdown_loop():
# No vetos? That is one step closer to shutting down
_shutdown_phase
+=
1
timestamp
=
time
.
time
()
def
shutdownFastHandler
():
"""Shutdown cleanly on SIGTERM. This is registered first,
so it should be called after all other handlers."""
logger
.
info
(
"Shutting down fast"
)
shutdown
(
0
,
fast
=
1
)
def
shutdownHandler
():
"""Shutdown cleanly on SIGINT. This is registered first,
so it should be called after all other handlers."""
logger
.
info
(
"Shutting down"
)
sys
.
exit
(
0
)
def
restartHandler
():
"""Restart cleanly on SIGHUP. This is registered first, so it
should be called after all other SIGHUP handlers."""
logger
.
info
(
"Restarting"
)
shutdown
(
1
)
def
showStacks
():
"""Dump a stracktrace of all threads on the console."""
print
(
dump_threads
())
sys
.
stdout
.
flush
()
src/Signals/Signals.py
View file @
c6ed1908
...
...
@@ -16,11 +16,17 @@ Zope signal handlers for clean shutdown, restart and log rotation.
import
logging
import
os
import
sys
import
Lifetime
from
.threads
import
dump_threads
LIFETIME
=
True
try
:
from
Lifetime
import
(
shutdownFastHandler
,
shutdownHandler
,
restartHandler
,
showStacks
,
)
except
ImportError
:
LIFETIME
=
False
logger
=
logging
.
getLogger
(
"Z2"
)
...
...
@@ -37,33 +43,6 @@ else:
from
SignalHandler
import
SignalHandler
def
shutdownFastHandler
():
"""Shutdown cleanly on SIGTERM. This is registered first,
so it should be called after all other handlers."""
logger
.
info
(
"Shutting down fast"
)
Lifetime
.
shutdown
(
0
,
fast
=
1
)
def
shutdownHandler
():
"""Shutdown cleanly on SIGINT. This is registered first,
so it should be called after all other handlers."""
logger
.
info
(
"Shutting down"
)
sys
.
exit
(
0
)
def
restartHandler
():
"""Restart cleanly on SIGHUP. This is registered first, so it
should be called after all other SIGHUP handlers."""
logger
.
info
(
"Restarting"
)
Lifetime
.
shutdown
(
1
)
def
showStacks
():
"""Dump a stracktrace of all threads on the console."""
print
(
dump_threads
())
sys
.
stdout
.
flush
()
class
LogfileReopenHandler
(
object
):
"""Reopen log files on SIGUSR2.
...
...
@@ -120,11 +99,11 @@ def registerZopeSignals(loggers):
except
ImportError
:
mod_wsgi
=
False
if
not
mod_wsgi
:
if
not
mod_wsgi
and
LIFETIME
:
SignalHandler
.
registerHandler
(
SIGTERM
,
shutdownFastHandler
)
SignalHandler
.
registerHandler
(
SIGINT
,
shutdownHandler
)
if
os
.
name
!=
'nt'
:
if
not
mod_wsgi
:
if
not
mod_wsgi
and
LIFETIME
:
SignalHandler
.
registerHandler
(
SIGHUP
,
restartHandler
)
SignalHandler
.
registerHandler
(
SIGUSR1
,
showStacks
)
SignalHandler
.
registerHandler
(
SIGUSR2
,
LogfileReopenHandler
(
loggers
))
...
...
src/Signals/WinSignalHandler.py
View file @
c6ed1908
...
...
@@ -41,13 +41,13 @@
# to add yet another asyncore handler - the thread in this module could
# then "post" the request to the main thread via this asyncore handler.
import
asyncore
import
atexit
import
logging
import
os
import
sys
import
signal
import
threading
import
asyncore
import
atexit
import
Lifetime
# As at pywin32-204, we must ensure pywintypes is the first win32 module
# imported in our process, otherwise we can end up with 2 pywintypesxx.dll
...
...
@@ -62,7 +62,12 @@ import win32con
import
win32event
import
ntsecuritycon
import
logging
LIFETIME
=
True
try
:
from
Lifetime
import
shutdown
except
ImportError
:
LIFETIME
=
False
logger
=
logging
.
getLogger
(
"WinSignalHandler"
)
# We simulate signals via win32 named events. This is the event name
...
...
@@ -233,9 +238,12 @@ class SignalHandler(object):
# SystemExit does the right thing. On Windows, we are on
# our own thread, so throwing SystemExit there isn't a great
# idea. Just shutdown the main loop.
logger
.
debug
(
"Trapped SystemExit(%s) - doing Lifetime shutdown"
%
rc
)
Lifetime
.
shutdown
(
rc
)
if
LIFETIME
:
logger
.
debug
(
"Trapped SystemExit(%s) - "
"doing Lifetime shutdown"
%
rc
)
shutdown
(
rc
)
else
:
raise
except
:
logger
.
exception
(
"A handler for %s failed!'"
%
signame
)
wakeSelect
()
# trigger a walk around the Lifetime loop.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment