Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

monitor command, transaction tests

  • Loading branch information...
commit 73175c122d42aebd5a609f398ef9dcf2409b2e39 1 parent 2d7a4ab
Gleicon Moraes authored July 20, 2012
4  .gitignore
... ...
@@ -1,5 +1,7 @@
1 1
 *.swp
2 2
 *.pyc
3 3
 build
4  
-_trial_temp/
  4
+_trial_temp
5 5
 tmp
  6
+dist
  7
+txredisapi.egg-info
49  examples/monitor.py
... ...
@@ -0,0 +1,49 @@
  1
+#!/usr/bin/env twistd -ny
  2
+# coding: utf-8
  3
+# Copyright 2012 Gleicon Moraes/Alexandre Fiori
  4
+#
  5
+# Licensed under the Apache License, Version 2.0 (the "License");
  6
+# you may not use this file except in compliance with the License.
  7
+# You may obtain a copy of the License at
  8
+#
  9
+# http://www.apache.org/licenses/LICENSE-2.0
  10
+#
  11
+# Unless required by applicable law or agreed to in writing, software
  12
+# distributed under the License is distributed on an "AS IS" BASIS,
  13
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14
+# See the License for the specific language governing permissions and
  15
+# limitations under the License.
  16
+#
  17
+# run: twistd -ny monitor.tac
  18
+# it takes the full connection so no extra commands can be issued
  19
+
  20
+
  21
+import txredisapi
  22
+
  23
+from twisted.application import internet
  24
+from twisted.application import service
  25
+
  26
+
  27
+class myMonitor(txredisapi.MonitorProtocol):
  28
+    def connectionMade(self):
  29
+        print "waiting for monitor data"
  30
+        print "use the redis client to send commands in another terminal"
  31
+        self.monitor()
  32
+
  33
+    def messageReceived(self, message):
  34
+        print ">> %s" % message
  35
+
  36
+    def connectionLost(self, reason):
  37
+        print "lost connection:", reason
  38
+
  39
+
  40
+class myFactory(txredisapi.MonitorFactory):
  41
+    # also a wapper for the ReconnectingClientFactory
  42
+    maxDelay = 120
  43
+    continueTrying = True
  44
+    protocol = myMonitor
  45
+
  46
+
  47
+application = service.Application("monitor")
  48
+srv = internet.TCPClient("127.0.0.1", 6379, myFactory())
  49
+srv.setServiceParent(application)
2  setup.py
@@ -18,7 +18,7 @@
18 18
 
19 19
 setuptools.setup(
20 20
     name="txredisapi",
21  
-    version="0.6",
  21
+    version="0.7",
22 22
     py_modules=["txredisapi"],
23 23
     install_requires=["twisted"],
24 24
     author="Alexandre Fiori",
41  tests/test_transactions.py
... ...
@@ -0,0 +1,41 @@
  1
+# coding: utf-8
  2
+# Copyright 2009 Alexandre Fiori
  3
+#
  4
+# Licensed under the Apache License, Version 2.0 (the "License");
  5
+# you may not use this file except in compliance with the License.
  6
+# You may obtain a copy of the License at
  7
+#
  8
+# http://www.apache.org/licenses/LICENSE-2.0
  9
+#
  10
+# Unless required by applicable law or agreed to in writing, software
  11
+# distributed under the License is distributed on an "AS IS" BASIS,
  12
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+# See the License for the specific language governing permissions and
  14
+# limitations under the License.
  15
+
  16
+import txredisapi
  17
+from twisted.trial import unittest
  18
+from twisted.internet import defer, reactor
  19
+from twisted.python import log                                                  
  20
+import sys
  21
+
  22
+log.startLogging(sys.stdout)
  23
+
  24
+redis_host="localhost"
  25
+redis_port=6379
  26
+
  27
+class TestRedisConnections(unittest.TestCase):
  28
+    @defer.inlineCallbacks
  29
+    def testRedisConnection(self):
  30
+        rapi = yield txredisapi.Connection(redis_host, redis_port)
  31
+        
  32
+        # test set() operation
  33
+        transaction = yield rapi.multi()
  34
+        print "oi" 
  35
+        for key, value in (("txredisapi:test_transaction", "foo"), ("txredisapi:test_transaction", "bar")):
  36
+            yield transaction.set(key, value)
  37
+        yield transaction.commit()
  38
+        result = yield rapi.get("txredisapi:test_transaction")
  39
+        self.assertEqual(result, "bar")
  40
+
  41
+        yield rapi.disconnect()
27  txredisapi.py
@@ -1224,8 +1224,29 @@ def info(self):
1224 1224
         """
1225 1225
         return self.execute_command("INFO")
1226 1226
 
1227  
-    # monitor and slaveof should are missing
  1227
+    # slaveof is missing
1228 1228
 
  1229
+class MonitorProtocol(RedisProtocol):
  1230
+    """
  1231
+    monitor command has the same behavior as subscribe: takes the whole connection.
  1232
+    take care with the performance impact:
  1233
+    http://redis.io/commands/monitor
  1234
+    """
  1235
+
  1236
+    def connectionLost(self, why):
  1237
+        pass
  1238
+
  1239
+    def messageReceived(self, message):
  1240
+        pass
  1241
+
  1242
+    def replyReceived(self, reply):
  1243
+        self.messageReceived(reply)
  1244
+
  1245
+    def monitor(self):
  1246
+        return self.execute_command("MONITOR")
  1247
+
  1248
+    def stop(self):
  1249
+        self.transport.loseConnection()
1229 1250
 
1230 1251
 class SubscriberProtocol(RedisProtocol):
1231 1252
     def connectionLost(self, why):
@@ -1267,6 +1288,10 @@ class SubscriberFactory(protocol.ReconnectingClientFactory):
1267 1288
     continueTrying = True
1268 1289
     protocol = SubscriberProtocol
1269 1290
 
  1291
+class MonitorFactory(protocol.ReconnectingClientFactory):
  1292
+    maxDelay = 120
  1293
+    continueTrying = True
  1294
+    protocol = MonitorProtocol
1270 1295
 
1271 1296
 class ConnectionHandler(object):
1272 1297
     def __init__(self, factory):

0 notes on commit 73175c1

Please sign in to comment.
Something went wrong with that request. Please try again.