Skip to content

Commit

Permalink
Add new Zope2.Startup.config module to hold configuration.
Browse files Browse the repository at this point in the history
This avoids various import dependencies between the Startup code
and ZServer/webdav.
  • Loading branch information
hannosch committed Jul 23, 2016
1 parent eec9a47 commit 8e52f97
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 116 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Features Added
Restructuring
+++++++++++++

- Add new `Zope2.Startup.config` module to hold configuration.

- Remove `Control_Panel` `/DebugInfo` and `/DavLocks`.

- Remove profiling support via `publisher-profile-file` directive.
Expand Down
4 changes: 2 additions & 2 deletions src/Lifetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def shutdown(exit_code,fast = 0):
global _shutdown_timeout
if _shutdown_phase == 0:
# Thread safety? proably no need to care
import ZServer
ZServer.exit_code = exit_code
from Zope2.Startup import config
config.ZSERVER_EXIT_CODE = exit_code
_shutdown_phase = 1
if fast:
# Someone wants us to shutdown fast. This is hooked into SIGTERM - so
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/ZopeTestCase/threadutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
def setNumberOfThreads(number_of_threads):
'''Sets number of ZServer threads.'''
try:
from ZServer.PubCore import setNumberOfThreads
from Zope2.Startup.config import setNumberOfThreads
setNumberOfThreads(number_of_threads)
except ImportError:
pass
Expand Down
6 changes: 3 additions & 3 deletions src/ZPublisher/HTTPResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,20 +560,20 @@ def redirect(self, location, status=302, lock=0):
# The following two methods are part of a private protocol with the
# publisher for handling fatal import errors and TTW shutdown requests.
_shutdown_flag = None

def _requestShutdown(self, exitCode=0):
""" Request that the server shut down with exitCode after fulfilling
the current request.
"""
import ZServer
ZServer.exit_code = exitCode
from Zope2.Startup import config
config.ZSERVER_EXIT_CODE = exitCode
self._shutdown_flag = 1

def _shutdownRequested(self):
""" Returns true if this request requested a server shutdown.
"""
return self._shutdown_flag is not None


def _encode_unicode(self,body,
charset_re=re.compile(
r'(?:application|text)/[-+0-9a-z]+\s*;\s*' +
Expand Down
4 changes: 2 additions & 2 deletions src/ZServer/HTTPServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def get_http_header_max_length():

class zhttp_collector:
def __init__(self, handler, request, size):
from ZServer import LARGE_FILE_THRESHOLD
from Zope2.Startup.config import ZSERVER_LARGE_FILE_THRESHOLD
self.handler = handler
self.request = request
if size > LARGE_FILE_THRESHOLD:
if size > ZSERVER_LARGE_FILE_THRESHOLD:
# write large upload data to a file
from tempfile import TemporaryFile
self.data = TemporaryFile('w+b')
Expand Down
10 changes: 7 additions & 3 deletions src/ZServer/PCGIServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
from Producers import ShutdownProducer, LoggingProducer, CallbackProducer
import DebugLogger

from Zope2.Startup import config

from cStringIO import StringIO
from tempfile import TemporaryFile
import socket, string, os, sys, time
Expand Down Expand Up @@ -388,9 +390,11 @@ def close(self):
lambda t=('E', id(self._channel)): apply(DebugLogger.log,t)), 0)

if self._shutdown:
try: r=self._shutdown[0]
except: r=0
ZServer.exit_code=r
try:
r = self._shutdown[0]
except:
r = 0
config.ZSERVER_EXIT_CODE = r
self._channel.push(ShutdownProducer(), 0)
Wakeup(lambda: asyncore.close_all())
else:
Expand Down
23 changes: 10 additions & 13 deletions src/ZServer/PubCore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@
#
##############################################################################

import ZRendezvous
from Zope2.Startup.config import ( # NOQA
setNumberOfThreads,
ZSERVER_THREADS as _n,
)
from ZServer.PubCore import ZRendezvous

_handle = None

_handle=None
_n=1

def handle(*args, **kw):
global _handle

if _handle is None: _handle=ZRendezvous.ZRendevous(_n).handle

return apply(_handle, args, kw)
if _handle is None:
_handle = ZRendezvous.ZRendevous(_n).handle

def setNumberOfThreads(n):
"""This function will self-destruct in 4 statements.
"""
global _n
_n=n
global setNumberOfThreads
del setNumberOfThreads
return _handle(*args, **kw)
26 changes: 6 additions & 20 deletions src/ZServer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,18 @@
#
##############################################################################

import sys
import utils

#########################################################
### declarations used by external packages

# the exit code used to exit a Zope process cleanly
exit_code = 0
from Zope2.Startup.config import ( # NOQA
ZSERVER_CONNECTION_LIMIT as CONNECTION_LIMIT,
ZSERVER_EXIT_CODE as exit_code,
ZSERVER_LARGE_FILE_THRESHOLD as LARGE_FILE_THRESHOLD,
setNumberOfThreads,
)

# the ZServer version number
ZSERVER_VERSION = '1.1'

# the maximum number of incoming connections to ZServer
CONNECTION_LIMIT = 1000 # may be reset by max_listen_sockets handler in Zope

# request bigger than this size get saved into a
# temporary file instead of being read completely into memory
LARGE_FILE_THRESHOLD = 1 << 19 # may be reset by large_file_threshold
# handler in Zope

# the Zope version string
ZOPE_VERSION = utils.getZopeVersion()

Expand All @@ -42,17 +34,11 @@
from PCGIServer import PCGIServer
from FCGIServer import FCGIServer
from FTPServer import FTPServer
from PubCore import setNumberOfThreads
from medusa.monitor import secure_monitor_server

### end declarations
##########################################################

# we need to patch asyncore's dispatcher class with a new
# log_info method so we see medusa messages in the zLOG log
utils.patchAsyncoreLogger()

# we need to patch the 'service name' of the medusa syslog logger
utils.patchSyslogServiceName()


3 changes: 2 additions & 1 deletion src/ZServer/medusa/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
# The ZConfig machinery may sets this attribute on initialization
# if any trusted-proxies

trusted_proxies = []
from Zope2.Startup.config import TRUSTED_PROXIES as trusted_proxies # NOQA


class http_request:

Expand Down
18 changes: 11 additions & 7 deletions src/Zope2/Startup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from zope.event import notify
from zope.processlifetime import ProcessStarting

import Zope2.Startup.config

try:
IO_ERRORS = (IOError, WindowsError)
except NameError:
Expand All @@ -35,6 +37,7 @@
logger = logging.getLogger("Zope")
started = False


def get_starter():
if sys.platform[:3].lower() == "win":
return WindowsZopeStarter()
Expand Down Expand Up @@ -105,10 +108,10 @@ def run(self):
try:
from App.config import getConfiguration
config = getConfiguration()
import ZServer
import Lifetime
Lifetime.loop()
sys.exit(ZServer.exit_code)
from Zope2.Startup.config import ZSERVER_EXIT_CODE
sys.exit(ZSERVER_EXIT_CODE)
finally:
self.shutdown()

Expand Down Expand Up @@ -137,10 +140,11 @@ def setupPublisher(self):
ZPublisher.Publish.set_default_authentication_realm(
self.cfg.http_realm)
if self.cfg.trusted_proxies:
# DM 2004-11-24: added host name mapping (such that examples in conf file really have a chance to work
mapped = []
for name in self.cfg.trusted_proxies: mapped.extend(_name2Ips(name))
for name in self.cfg.trusted_proxies:
mapped.extend(_name2Ips(name))
ZPublisher.HTTPRequest.trusted_proxies = tuple(mapped)
Zope2.Startup.config.TRUSTED_PROXIES = tuple(mapped)

def setupSecurityOptions(self):
import AccessControl
Expand Down Expand Up @@ -183,9 +187,9 @@ def setupLocale(self):

def setupZServer(self):
# Increase the number of threads
import ZServer
ZServer.setNumberOfThreads(self.cfg.zserver_threads)
ZServer.CONNECTION_LIMIT = self.cfg.max_listen_sockets
Zope2.Startup.config.setNumberOfThreads(self.cfg.zserver_threads)
Zope2.Startup.config.ZSERVER_CONNECTION_LIMIT = \
self.cfg.max_listen_sockets

def serverListen(self):
for server in self.cfg.servers:
Expand Down
30 changes: 30 additions & 0 deletions src/Zope2/Startup/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

TRUSTED_PROXIES = []

ZSERVER_CONNECTION_LIMIT = 1000
ZSERVER_ENABLE_MS_PUBLIC_HEADER = False
ZSERVER_EXIT_CODE = 0
ZSERVER_LARGE_FILE_THRESHOLD = 524288
ZSERVER_THREADS = 1


def setNumberOfThreads(n):
"""This function will self-destruct in 4 statements.
"""
global ZSERVER_THREADS
ZSERVER_THREADS = n
global setNumberOfThreads
del setNumberOfThreads
Loading

0 comments on commit 8e52f97

Please sign in to comment.