From 8d93501005eb80a05e0cdb668b1ede1fdfbd67cf Mon Sep 17 00:00:00 2001
From: Kevin Deldycke <kevin@nexedi.com>
Date: Mon, 8 Jan 2007 14:31:21 +0000
Subject: [PATCH] Second patch

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11928 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/TimerService/TimerService.py          | 24 ++++++++++++-------
 .../TimerService/timerserver/TimerServer.py   |  5 +---
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/product/TimerService/TimerService.py b/product/TimerService/TimerService.py
index 8c15877fb8..0f767c929f 100644
--- a/product/TimerService/TimerService.py
+++ b/product/TimerService/TimerService.py
@@ -3,7 +3,7 @@
 # Authors: Nik Kim <fafhrd@legco.biz> 
 __version__ = '$Revision: 1.3 $'[11:-2]
 
-import sys, time
+import sys, time, threading
 from DateTime import DateTime
 from Globals import InitializeClass
 from OFS.SimpleItem import SimpleItem
@@ -16,6 +16,8 @@ from Products.PageTemplates.PageTemplateFile import PageTemplateFile
 
 current_version = 1
 
+processing_lock = threading.Lock()
+
 class TimerService(SimpleItem):
     """ timer service, all objects that wants timer
     event subscribe here """
@@ -49,23 +51,31 @@ class TimerService(SimpleItem):
 
     def process_timer(self, interval):
         """ """
+        # Try to acquire a lock, to make sure we only run one processing at a
+        # time, and abort if another processing is currently running
+        acquired = processing_lock.acquire(0)
+        if not acquired:
+          return
+
         subscriptions = [self.unrestrictedTraverse(path)
                          for path in self._subscribers]
 
         tick = time.time()
-#        prev_tick = tick - interval
-#        next_tick = tick + interval
+        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))
-              subscriber.process_timer(tick, interval)
+                subscriber.process_timer(
+                    interval, DateTime(tick), DateTime(prev_tick), DateTime(next_tick))
             except:
                 LOG('TimerService', ERROR, 'Process timer error', error = sys.exc_info())
 
+        # When processing is done, release the lock
+        processing_lock.release()
+
     def subscribe(self, ob):
         """ """
         path = '/'.join(ob.getPhysicalPath())
@@ -103,8 +113,6 @@ class TimerService(SimpleItem):
         """ """
         subs = self.lisSubscriptions()
 
-        #LOG('asdd',INFO,subs)
-
         remove_list = [subs[n] for n in [int(n) for n in no]]
 
         for sub in remove_list:
diff --git a/product/TimerService/timerserver/TimerServer.py b/product/TimerService/timerserver/TimerServer.py
index 0c6d5343e5..6b17c380a2 100644
--- a/product/TimerService/timerserver/TimerServer.py
+++ b/product/TimerService/timerserver/TimerServer.py
@@ -6,10 +6,7 @@ __version__ = 'TimerServer for Zope 0.1'
 import traceback
 
 import thread
-
 import sys, os, errno, time, socket
-
-import ZPublisher.Client
 from StringIO import StringIO
 from zLOG import LOG, INFO
 
@@ -20,7 +17,7 @@ from ZPublisher.HTTPRequest import HTTPRequest
 from ZPublisher import Client
 
 class TimerServer:
-    def __init__(self, module, interval=5):
+    def __init__(self, module, interval=600):
         self.module = module
 
         self.interval = interval
-- 
2.30.9