[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r21940: {weather} Work around TorCtl's thread leak (in weather/trunk: . lib/weather)



Author: kaner
Date: 2010-03-14 15:55:14 +0000 (Sun, 14 Mar 2010)
New Revision: 21940

Modified:
   weather/trunk/Weather.py
   weather/trunk/lib/weather/config.py
   weather/trunk/lib/weather/poller.py
   weather/trunk/lib/weather/utils.py
Log:
Work around TorCtl's thread leak


Modified: weather/trunk/Weather.py
===================================================================
--- weather/trunk/Weather.py	2010-03-14 02:07:11 UTC (rev 21939)
+++ weather/trunk/Weather.py	2010-03-14 15:55:14 UTC (rev 21940)
@@ -14,6 +14,7 @@
 from twisted.internet import reactor
 from twisted.internet.task import LoopingCall 
 
+from weather.torping import TorPing
 from weather.constants import PAGE_TEMPLATE, PAGE_SIGNUP, CONFIRMATION_MAIL, THANKS_OUT, PAGE_SUB_FIN
 from weather.queries import CHECK_SUBS_Q, INSERT_SUBS_Q, CHECK_SUBS_AUTH_Q, ACK_SUB_Q, UNSUBSCRIBE_Q
 from weather.poller import WeatherPoller
@@ -50,9 +51,9 @@
 
     def _isSubscribedAlready(self):
         dbQuery = CHECK_SUBS_Q % (self.email, self.node)
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._checkHasSubRet)
-        q.addErrback(self._errback)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._checkHasSubRet).addErrback(
+            self._errback)
 
     def _checkHasSubRet(self, result):
         # Do we already have a subscription for this address for this node?
@@ -62,15 +63,14 @@
             self.request.finish()
         else:
             # Alright, subscribe it
-            return self._runSaveQuery()
+            self._runSaveQuery()
 
     def _runSaveQuery(self):
         dbQuery = INSERT_SUBS_Q % (self.email, self.node, \
                               self.subs_auth, self.unsubs_auth, self.downtime)
-        q = self.dbConn.runOperation(dbQuery)
-        q.addCallback(self._saved)
-        q.addErrback(self._errback)
-        return q
+        self.dbConn.runOperation(dbQuery).addCallback(
+            self._saved).addErrback(
+            self._errback)
 
     def _saved(self, result):
         url = URLbase + "/confirm-subscribe?auth=" + self.subs_auth
@@ -91,9 +91,9 @@
 
     def _rollBack(self):
         dbQuery = CHECK_SUBS_Q % (self.email, self.node)
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._errOut)
-        q.addErrback(self._errback)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._errOut).addErrback(
+            self._errback)
 
     def _errOut(self):
         self.request.setResponseCode(http.INTERNAL_SERVER_ERROR)
@@ -116,9 +116,9 @@
 
     def _lookupSubsAuth(self, request):
         dbQuery = CHECK_SUBS_AUTH_Q % self.subs_auth
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._checkRet, request)
-        q.addErrback(self._errback, request)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._checkRet, request).addErrback(
+            self._errback, request)
 
     def _checkRet(self, result, request):
         if len(result) is 0:
@@ -132,9 +132,9 @@
 
     def _ackSubscription(self, request):
         dbQuery = ACK_SUB_Q % self.subs_auth
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._subDone, request)
-        q.addErrback(self._errback, request)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._subDone, request).addErrback(
+            self._errback, request)
 
     def _subDone(self, result, request):
         url = URLbase + "/unsubscribe?auth=" + self.unsubs_auth
@@ -161,9 +161,9 @@
 
     def _deleteSub(self, request):
         dbQuery = UNSUBSCRIBE_Q % self.unsubs_auth
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._deleteDone, request)
-        q.addErrback(self._errback, request)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._deleteDone, request).addErrback(
+            self._errback, request)
 
     def _deleteDone(self, result, request):
         request.setResponseCode(http.OK)
@@ -192,7 +192,9 @@
     # Set up database connection
     dbConn = utils.setupDBConn(databaseName)
     # Set up polling timer
-    weatherPoller = WeatherPoller(dbConn)
+    # XXX Have one main TorPing instance until TotCtl fixes its thread leak
+    torPing = TorPing()
+    weatherPoller = WeatherPoller(dbConn, torPing)
     pollTimer = LoopingCall(weatherPoller.poller)
     pollTimer.start(pollPeriod)
     # Set up webserver

Modified: weather/trunk/lib/weather/config.py
===================================================================
--- weather/trunk/lib/weather/config.py	2010-03-14 02:07:11 UTC (rev 21939)
+++ weather/trunk/lib/weather/config.py	2010-03-14 15:55:14 UTC (rev 21940)
@@ -13,6 +13,6 @@
 failureThreshold = 4 # this number of failures in a row counts as being
                       # down
 
-pollPeriod = 10 # Check every hour
+pollPeriod = 3600 # Check every hour
 
 databaseName = "subscriptions.db"

Modified: weather/trunk/lib/weather/poller.py
===================================================================
--- weather/trunk/lib/weather/poller.py	2010-03-14 02:07:11 UTC (rev 21939)
+++ weather/trunk/lib/weather/poller.py	2010-03-14 15:55:14 UTC (rev 21940)
@@ -10,8 +10,9 @@
 import weather.utils as utils
 
 class WeatherPoller():
-    def __init__(self, dbConn):
+    def __init__(self, dbConn, torPing):
         self.dbConn = dbConn
+        self.torPing = torPing
 
     def poller(self):
         self._checkAll()
@@ -19,9 +20,9 @@
 
     def _checkAll(self):
         dbQuery = queries.GETALL_SUBS_Q
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._checkRet)
-        q.addErrback(self._errBack)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._checkRet).addErrback(
+            self._errBack)
 
     def _checkRet(self, resultList):
         # Loop through result list and check each node
@@ -40,8 +41,7 @@
         print "Error: ", failure.getErrorMessage()
 
     def _checkHost(self, hostID):
-        torPing = TorPing()
-        return torPing.ping(hostID)
+        return self.torPing.ping(hostID)
 
     def _handleOfflineNode(self, dbRow):
         self._getDowntimes(dbRow[0])
@@ -49,9 +49,9 @@
 
     def _getDowntimes(self, id):
         dbQuery = queries.ALL_BY_ID_Q % id
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._decideNotice)
-        q.addErrback(self._errBack)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._decideNotice).addErrback(
+            self._errBack)
 
     def _decideNotice(self, result):
         downGrace = result[0][6] 
@@ -79,21 +79,21 @@
 
     def _setNoticed(self, id):
         dbQuery = queries.UPDATE_NOTICED_Q % id
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._updateDone)
-        q.addErrback(self._errBack)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._updateDone).addErrback(
+            self._errBack)
 
     def _updateDowntime(self, id):
         dbQuery =  queries.UPDATE_DOWN_Q % id
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._updateDone)
-        q.addErrback(self._errBack)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._updateDone).addErrback(
+            self._errBack)
 
     def _updateDone(self, result):
         print "Query ok"        
 
     def _resetSeendown(self, dbRow):
         dbQuery = queries.RESET_COUNT_Q % dbRow[0]
-        q = self.dbConn.runQuery(dbQuery)
-        q.addCallback(self._updateDone)
-        q.addErrback(self._errBack)
+        self.dbConn.runQuery(dbQuery).addCallback(
+            self._updateDone).addErrback(
+            self._errBack)

Modified: weather/trunk/lib/weather/utils.py
===================================================================
--- weather/trunk/lib/weather/utils.py	2010-03-14 02:07:11 UTC (rev 21939)
+++ weather/trunk/lib/weather/utils.py	2010-03-14 15:55:14 UTC (rev 21940)
@@ -20,7 +20,7 @@
     db = sqlite3.connect(databaseName)
     db.execute(CREATE_TABLE_Q % "subscriptions")
     db.close()
-    dbConn = adbapi.ConnectionPool("sqlite3", databaseName)
+    dbConn = adbapi.ConnectionPool("sqlite3", databaseName, check_same_thread=False)
     return dbConn
 
 def checkMail(email):