consumer logs again idle time spent waiting for events
authorAsko Oja <ascoja@gmail.com>
Tue, 18 Sep 2012 11:12:26 +0000 (14:12 +0300)
committerAsko Oja <ascoja@gmail.com>
Tue, 18 Sep 2012 11:12:26 +0000 (14:12 +0300)
python/pgq/consumer.py

index 77c018c79b31eb4cd74a9769cd636dfdf2c9e987..8ec9a18a5b2c5ce950924e6da550e7d981159246 100644 (file)
@@ -157,6 +157,9 @@ class Consumer(skytools.DBScript):
 
     consumer_filter = None
 
+    # statistics: time spent waiting for events
+       idle_start = None
+
     def __init__(self, service_name, db_name, args):
         """Initialize new consumer.
         
@@ -191,6 +194,8 @@ class Consumer(skytools.DBScript):
             raise skytools.UsageError("pgq_autocommit is not compatible with pgq_lazy_fetch")
         self.set_database_defaults(self.db_name, autocommit = self.pgq_autocommit)
 
+        self.idle_start = time.time()
+
     def reload(self):
         skytools.DBScript.reload(self)
 
@@ -380,5 +385,6 @@ class Consumer(skytools.DBScript):
         t = time.time()
         self.stat_put('count', count)
         self.stat_put('duration', t - self.stat_batch_start)
-
-
+        if count > 0: # reset timer if we got some events
+            self.stat_put('idle', self.stat_batch_start - self.idle_start)
+            self.idle_start = t
\ No newline at end of file