Created
December 1, 2019 00:14
-
-
Save filipecosta90/2b6926c74bc05aa18016f331a6d0d5c0 to your computer and use it in GitHub Desktop.
Module and script to parse pub-sub events and write them to Redis hashes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Module and script to parse pub-sub events and write them to Redis hashes.""" | |
import json | |
import redis | |
from wikiutils import current_timestamp, pubsub_channel, redis_url | |
r = redis.Redis.from_url(redis_url) | |
def process(): | |
"""Process messages from the pubsub stream.""" | |
ps = r.pubsub() | |
ps.subscribe(pubsub_channel) | |
for raw_message in ps.listen(): | |
if raw_message["type"] != "message": | |
continue | |
message = json.loads(raw_message["data"]) | |
process_message(message) | |
def process_message(message): | |
"""Process single decoded message.""" | |
domain = message["meta"]["domain"] | |
ts = current_timestamp() | |
keys = ["ev", f"ev:{domain}"] | |
pipe = r.pipeline() | |
for key in keys: | |
pipe.hincrby(key, ts, 1) | |
pipe.sadd("known_domains", domain) | |
pipe.execute() | |
if __name__ == "__main__": | |
process() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment