Skip to content

Commit

Permalink
add pubsub fuctionallity using a python script, meh
Browse files Browse the repository at this point in the history
  • Loading branch information
daTokenizer committed Nov 26, 2016
1 parent ecc5eee commit 1ff3b93
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ In this repository there are two python files that exemplify the usage of the mo
* [helloworld.py](tests/helloworld.py) - very simple usage example of all the functions exposed by the module
* [test.py](tests/test.py) - run internal as well as external functional tests, load test and print it all to stdout.

### 3. klib [khash](src/khash.h)
### 3. Redis PubSub utility script

[pubsub.py](src/pubsub.py) - A workaround for the lack of redis background tasks for providing PUB/SUB functionality

### 4. klib [khash](src/khash.h)

A set of macros to create the hash maps used to implement the dehydrator type.

### 4. LibRMUtil
### 5. LibRMUtil

From [Redis Modules SDK](https://github.com/RedisLabs/RedisModulesSDK) README:

Expand All @@ -67,9 +71,9 @@ The dehydrator is an effective 'snooze button' for events, you push an event int
**The module include 7 commands:**

* [`REDE.PUSH`](docs/Commands.md/#push) - Insert an element. The command takes an id for the element, the element itself and dehydration time in milliseconds.
* [`REDE.GIDPUSH`](docs/Commands.md/#gidpush) - Insert an element. The command generates an id for the element, but still needs the element itself and dehydration time in milliseconds.
* [`REDE.PULL`](docs/Commands.md/#pull) - Remove the element with the appropriate id before it expires.
* [`REDE.POLL`](docs/Commands.md/#poll) - Pull and return all the expired elements.
* [`REDE.GIDPUSH`](docs/Commands.md/#gidpush) - Insert an element. The command generates an id for the element, but still needs the element itself and dehydration time in milliseconds.
* [`REDE.LOOK`](docs/Commands.md/#look) - Search the dehydrator for an element with the given id and if found return it's payload (without pulling).
* [`REDE.TTN`](docs/Commands.md/#ttn) - Return the minimal time between now and the first expiration
* [`REDE.UPDATE`](docs/Commands.md/#update) - Set the element represented by a given id, the current element will be returned, and the new element will inherit the current expiration.
Expand Down Expand Up @@ -129,7 +133,7 @@ Enjoy!

## Future work

* add some sort of pub/sub mechanism to POLL
* add some sort of pub/sub mechanism to POLL - waiting for some sort of "reactor" pattern or background tasks in redis (maybe this should be a module).. right now this functionality can be achieved by using [this python script](src/pubsub.py)
* Additional / more thorough / automatic tests

## About This Module
Expand Down
34 changes: 34 additions & 0 deletions src/pubsub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#! /usr/bin/python
import redis
import time
import random
import sys

if __name__ == "__main__":
r = redis.StrictRedis(host='localhost', port=6379, db=0)

if (not (3 <= len(sys.argv) <= 4)) or (sys.argv[1] in ["-?", "-h", "--help"]):
print "usage: %s <dehydrator_name> <channel> [Hz]" % sys.argv[0]
exit()

dehydrator_name = sys.argv[1]
channel = sys.argv[2]
if len(sys.argv) > 3:
Hz = sys.argv[3]
sleep_time = 1.0/Hz
else:
sleep_time = 1.0

print "Polling and Publishing (press Ctrl-C to quit)"
user_notified = False
while True:
try:
poll_result = r.execute_command("REDE.POLL", dehydrator_name)
except:
if not user_notified:
print "an error occured in POLL, probably bad dehydrator name (this message will not be repeted)"
user_notified = True
else:
for element in poll_result:
poll_result = r.execute_command("PUBLISH", channel, element)
time.sleep(sleep_time)

0 comments on commit 1ff3b93

Please sign in to comment.