Commit 47eea1f9 authored by Kevin Deldycke's avatar Kevin Deldycke

Original v0.2 code

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11926 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 6bc68efb
Author
Nikolay Kim <fafhrd@legco.biz>
Many thanks to Igor Stroh <jenner@dpost.de>
Prerequisites
* Zope 2.7.0+ (www.zope.org)
Instalation
1) go to timerserver directory and run:
a) python setup.py install
b) or create link to timerserver in $ZOPE/lib/python
timerserver module must be available in python path
2) add to zope.conf
%import timerserver
<timer-server>
</timer-server>
# -*- coding: UTF-8 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
# Authors: Nik Kim <fafhrd@legco.biz>
__version__ = '$Revision: 1.3 $'[11:-2]
import sys, time
from DateTime import DateTime
from Globals import InitializeClass
from OFS.SimpleItem import SimpleItem
from OFS.PropertyManager import PropertyManager
from zLOG import LOG, INFO, ERROR
from AccessControl import ClassSecurityInfo, Permissions
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
current_version = 1
class TimerService(SimpleItem):
""" timer service, all objects that wants timer
event subscribe here """
id='timer_service'
title = 'TimerService'
security = ClassSecurityInfo()
icon = 'misc_/TimerService/timer_icon.gif'
max_size = 0
manage_options = (
({'label': 'Subscribers', 'action':'manage_viewSubscriptions'},))
security.declareProtected(
Permissions.view_management_screens, 'manage_viewSubscriptions')
manage_viewSubscriptions = PageTemplateFile(
'zpt/view_subscriptions',
globals(),
__name__='manage_viewSubscriptions'
)
_version = 0
def __init__(self, id='timer_service'):
""" """
self._subscribers = []
self._version = 1
def process_timer(self, interval):
""" """
subscriptions = [self.unrestrictedTraverse(path)
for path in self._subscribers]
tick = time.time()
prev_tick = tick - interval
next_tick = tick + interval
LOG('TimerService', INFO, 'Ttimer tick at %s\n'%time.ctime(tick))
for subscriber in subscriptions:
try:
subscriber.process_timer(
interval, DateTime(tick), DateTime(prev_tick), DateTime(next_tick))
except:
LOG('TimerService', ERROR, 'Process timer error', error = sys.exc_info())
def subscribe(self, ob):
""" """
path = '/'.join(ob.getPhysicalPath())
#if not ISMTPHandler.isImplementedBy(ob):
# raise ValueError, 'Object not support ISMTPHandler'
subscribers = self._subscribers
if path not in subscribers:
subscribers.append(path)
self._subscribers = subscribers
def unsubscribe(self, ob):
""" """
path = '/'.join(ob.getPhysicalPath())
subscribers = self._subscribers
if path in subscribers:
subscribers.remove(path)
self._subscribers = subscribers
security.declareProtected(
Permissions.view_management_screens, 'lisSubscriptions')
def lisSubscriptions(self):
""" """
return self._subscribers
def manage_removeSubscriptions(self, no, REQUEST=None):
""" """
subs = self.listAllSubscriptions()
remove_list = [subs[n] for n in [int(n) for n in no]]
for subs, event, fl in remove_list:
self.unsubscribe( subs, event_type=event )
if REQUEST is not None:
REQUEST.RESPONSE.redirect('manage_viewSubscriptions')
InitializeClass(TimerService)
# -*- coding: UTF-8 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
# Authors: Nik Kim <fafhrd@legco.biz>
import Globals
from AccessControl import ModuleSecurityInfo, allow_module
from AccessControl.Permissions import view
from TimerService import TimerService, current_version
misc_ = { 'timer_icon.gif':
Globals.ImageFile('zpt/timer_icon.gif', globals())}
cp_id = 'timer_service'
def getTimerService(context):
""" returns the SMTP srevice instance """
return context.Control_Panel.timer_service
def make_timer_service(cp):
"""Control_Panel smtp service"""
timer_service = TimerService(cp_id)
cp._setObject(cp_id, timer_service)
return getattr(cp, cp_id)
def initialize(context):
# hook into the Control Panel
cp = context._ProductContext__app.Control_Panel
if cp_id in cp.objectIds():
#cp._delObject(cp_id)
timer = getattr(cp, cp_id)
timer_service = timer
if not isinstance(timer_service, TimerService):
timer = make_timer_service(cp)
else:
timer = make_timer_service(cp)
if timer._version < current_version:
cp._delObject(cp_id)
timer = make_timer_service(cp)
# -*- coding: UTF-8 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
# Authors: Nik Kim <fafhrd@legco.biz>
__version__ = 'TimerServer for Zope 0.1'
import traceback
import thread
import sys, os, errno, time
from StringIO import StringIO
from zLOG import LOG, INFO
from ZServer.PubCore import handle
from ZPublisher.BaseRequest import BaseRequest
from ZPublisher.BaseResponse import BaseResponse
from ZPublisher.HTTPRequest import HTTPRequest
class TimerServer:
def __init__(self, module, interval=600):
self.module = module
self.interval = interval
sync = thread.allocate_lock()
self._a = sync.acquire
self._r = sync.release
self._a()
thread.start_new_thread(self.run, ())
self._r()
LOG('ZServer', INFO,
'Timer server started at %s\n'
'\tInterval: %s seconds.\n'%(time.ctime(time.time()), interval))
def run(self):
module = self.module
interval = self.interval
# minutes = time.gmtime(time.time()[4], seconds = time.gmtime(time.time()[5]
# max interval is therefore 59*60 + 59 = 208919 seconds
wait = ((time.gmtime(time.time())[4] * 60) + time.gmtime(time.time())[5]) % interval
sleep = interval - wait
if sleep > 0:
time.sleep(sleep)
LOG('ZServer', INFO, 'Timerserver ready, starting timer services.')
while 1:
time.sleep(interval)
# send message to zope
try:
out = StringIO()
err = StringIO()
response = TimerResponse(out, err)
handle(module, TimerRequest(response, interval), response)
except:
pass
class TimerResponse(BaseResponse):
def _finish(self):
pass
def unauthorized(self):
pass
class TimerRequest(HTTPRequest):
retry_max_count = 0
def __init__(self, response, interval):
stdin=StringIO()
environ=self._get_env(stdin)
HTTPRequest.__init__(self, stdin, environ, response, clean=1)
self.other['interval'] = interval
def _get_env(self, stdin):
"Returns a CGI style environment"
env={}
env['REQUEST_METHOD']='GET'
env['SERVER_SOFTWARE']= 'TimerServer for Zope'
env['SERVER_NAME'] = ''
env['SERVER_PORT'] = ''
env['REMOTE_ADDR'] = ''
env['GATEWAY_INTERFACE'] = 'CGI/1.1'
env['PATH_INFO']= '/Control_Panel/timer_service/process_timer'
return env
from ZServer.datatypes import ServerFactory
class TimerServerFactory(ServerFactory):
def __init__(self, section):
ServerFactory.__init__(self)
self.interval = section.interval
def create(self):
from timerserver.TimerServer import TimerServer
return TimerServer(self.module, self.interval)
modname = 'timerserver'
version = open('version.txt').read().strip()
numversion = version.split('.')
license = 'GPL'
copyright = '''Nikolay Kim (c) 2004'''
author = "Nikolay Kim"
author_email = "fafhrd@legco.biz"
short_desc = "Timer Server for Zope"
long_desc = short_desc
web = ""
ftp = ""
mailing_list = ""
<component>
<import package="ZServer" />
<sectiontype name="timer-server"
datatype="timerserver.TimerServerFactory"
implements="ZServer.server">
<key name="interval" datatype="integer" default="600">
<description>
Interval in seconds.
</description>
</key>
</sectiontype>
</component>
#!/usr/bin/env python
import sys
from distutils import util
from distutils.core import setup, Extension
from __pkginfo__ import modname, version, license, short_desc, long_desc,\
web, author, author_email
if __name__ == '__main__' :
dist = setup(name = modname,
version = version,
license =license,
description = short_desc,
long_description = long_desc,
author = author,
author_email = author_email,
url = web,
package_dir = {modname: '.'},
packages = [modname,],
data_files = [
('./lib/python%s.%s/site-packages/%s'%(sys.version_info[0], sys.version_info[1], modname),
['component.xml'])]
)
0.2
\ No newline at end of file
<h1 tal:replace="structure here/manage_page_header">Header</h1>
<h2 tal:define="manage_tabs_message options/manage_tabs_message | nothing"
tal:replace="structure here/manage_tabs">Tabs</h2>
<h4 class="form-label">Subscriptions</h4>
<h4 tal:define="global subscriptions here/lisSubscriptions"
tal:condition="not:subscriptions">There are no subscriptions.</h4>
<form method="post" action="manage_removeSubscriptions">
<table width="100%" cellspacing="0" cellpadding="2" border="0"
tal:condition="subscriptions">
<tr align="left">
<th>&nbsp;</th>
<th align="left">Subscriber</th>
</tr>
<tbody tal:repeat="subscription subscriptions">
<tr align="left" class="form-help">
<td><input type="checkbox" name="no:list"
tal:attributes="value repeat/subscription/index" /></td>
<td tal:content="subscription">Subscriber</td>
</tr>
</tbody>
</table>
<br />
<input type="submit" value="Remove" />
</form>
<h1 tal:replace="structure here/manage_page_footer">Footer</h1>
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