Commit 5eeb52d5 authored by Bryton Lacquement's avatar Bryton Lacquement :door: Committed by Julien Muchembled

TimerService: make timerserver compatible with WSGI

At the same time, timerserver is no longer distributed as an egg.
parent f46d70f9
...@@ -8,7 +8,7 @@ from tempfile import TemporaryFile ...@@ -8,7 +8,7 @@ from tempfile import TemporaryFile
import time import time
from urllib import quote from urllib import quote
from waitress import serve from waitress.server import create_server
import ZConfig import ZConfig
import Zope2 import Zope2
from Zope2.Startup.run import make_wsgi_app from Zope2.Startup.run import make_wsgi_app
...@@ -134,6 +134,8 @@ def app_wrapper(large_file_threshold, use_webdav): ...@@ -134,6 +134,8 @@ def app_wrapper(large_file_threshold, use_webdav):
def runwsgi(): def runwsgi():
global server
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-w', '--webdav', action='store_true') parser.add_argument('-w', '--webdav', action='store_true')
parser.add_argument('address', help='<ip>:<port>') parser.add_argument('address', help='<ip>:<port>')
...@@ -146,7 +148,7 @@ def runwsgi(): ...@@ -146,7 +148,7 @@ def runwsgi():
make_wsgi_app({}, zope_conf=args.zope_conf) make_wsgi_app({}, zope_conf=args.zope_conf)
serve( server = create_server(
TransLogger(app_wrapper(conf.large_file_threshold, args.webdav), TransLogger(app_wrapper(conf.large_file_threshold, args.webdav),
logger=logging.getLogger("access")), logger=logging.getLogger("access")),
listen=args.address, listen=args.address,
...@@ -155,3 +157,4 @@ def runwsgi(): ...@@ -155,3 +157,4 @@ def runwsgi():
trusted_proxy_headers=('x-forwarded-for',), trusted_proxy_headers=('x-forwarded-for',),
clear_untrusted_proxy_headers=True, clear_untrusted_proxy_headers=True,
) )
server.run()
...@@ -8,10 +8,10 @@ import traceback ...@@ -8,10 +8,10 @@ import traceback
import thread import thread
import re import re
import sys, os, errno, time, socket import sys, os, errno, time, socket
from functools import partial
from StringIO import StringIO from StringIO import StringIO
from zLOG import LOG, INFO from zLOG import LOG, INFO
from ZServer.PubCore import handle
from ZPublisher.BaseRequest import BaseRequest from ZPublisher.BaseRequest import BaseRequest
from ZPublisher.BaseResponse import BaseResponse from ZPublisher.BaseResponse import BaseResponse
from ZPublisher.HTTPRequest import HTTPRequest from ZPublisher.HTTPRequest import HTTPRequest
...@@ -38,21 +38,46 @@ class TimerServer: ...@@ -38,21 +38,46 @@ class TimerServer:
'\tInterval: %s seconds.\n'%(time.ctime(time.time()), interval)) '\tInterval: %s seconds.\n'%(time.ctime(time.time()), interval))
def run(self): def run(self):
# wait until the zhttp_server exist in socket_map try:
# because TimerService has to be started after the Zope HTTPServer zopewsgi = sys.modules['Products.ERP5.bin.zopewsgi']
from asyncore import socket_map except KeyError:
ip = port = '' # wait until the zhttp_server exist in socket_map
while 1: # because TimerService has to be started after the Zope HTTPServer
time.sleep(5) from asyncore import socket_map
for k, v in socket_map.items(): ip = port = ''
if hasattr(v, 'addr'): while 1:
time.sleep(5)
for k, v in socket_map.items():
if hasattr(v, 'addr'):
# see Zope/lib/python/App/ApplicationManager.py: def getServers(self) # see Zope/lib/python/App/ApplicationManager.py: def getServers(self)
type = str(getattr(v, '__class__', 'unknown')) type = str(getattr(v, '__class__', 'unknown'))
if type == 'ZServer.HTTPServer.zhttp_server': if type == 'ZServer.HTTPServer.zhttp_server':
ip, port = v.addr ip, port = v.addr
break break
if port: if port:
break break
from ZServer.PubCore import handle
else:
while 1:
time.sleep(5)
try:
server = zopewsgi.server
break
except AttributeError:
pass
ip, port = server.addr
start_response = lambda *_: None
class handle(object):
def __init__(self, module_name, request, response):
self.service = partial(zopewsgi.publish_module,
request.environ,
start_response,
_module_name=module_name,
_request=request,
_response=response)
server.add_task(self)
if ip == '0.0.0.0': if ip == '0.0.0.0':
ip = socket.gethostbyname(socket.gethostname()) ip = socket.gethostbyname(socket.gethostname())
...@@ -99,6 +124,9 @@ class TimerServer: ...@@ -99,6 +124,9 @@ class TimerServer:
class TimerResponse(BaseResponse): class TimerResponse(BaseResponse):
after_list = ()
def _finish(self): def _finish(self):
pass pass
...@@ -108,6 +136,9 @@ class TimerResponse(BaseResponse): ...@@ -108,6 +136,9 @@ class TimerResponse(BaseResponse):
def _unauthorized(self): def _unauthorized(self):
pass pass
def finalize(self):
return None, None
# This is taken from ZPublisher.HTTPResponse # This is taken from ZPublisher.HTTPResponse
# I don't think it's safe to make TimerResponse a subclass of HTTPResponse, # I don't think it's safe to make TimerResponse a subclass of HTTPResponse,
# so I inline here the method . This is required it you want unicode page # so I inline here the method . This is required it you want unicode page
...@@ -148,6 +179,7 @@ class TimerRequest(HTTPRequest): ...@@ -148,6 +179,7 @@ class TimerRequest(HTTPRequest):
env['SERVER_PORT'] = '' env['SERVER_PORT'] = ''
env['REMOTE_ADDR'] = '' env['REMOTE_ADDR'] = ''
env['GATEWAY_INTERFACE'] = 'CGI/1.1' env['GATEWAY_INTERFACE'] = 'CGI/1.1'
env['SERVER_PROTOCOL'] = 'HTTP/1.0'
env['PATH_INFO']= '/Control_Panel/timer_service/process_timer' env['PATH_INFO']= '/Control_Panel/timer_service/process_timer'
return env return env
......
#!/usr/bin/env python
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
setup(name='timerserver',
version='2.0.4',
license='GPL',
description='Timer Server for Zope',
long_description='',
author='Nikolay Kim',
author_email='fafhrd@legco.biz',
packages=['timerserver'],
zip_safe=False,
package_data={'timerserver': ['component.xml']},
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment