Yuvipanda has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/77300


Change subject: [WIP] Store canonical info about subscriptions in mysql
......................................................................

[WIP] Store canonical info about subscriptions in mysql

Redis is transient storage and should be treated as such

Change-Id: Iff63c68bbcdb0d2fce00a9226f22ae86e43ae392
---
M config.yaml.sample
M src/registrar.py
A src/storage.py
M src/subscriptions.py
A src/subscriptions.sql
5 files changed, 33 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/labs/tools/gerrit-to-redis 
refs/changes/00/77300/1

diff --git a/config.yaml.sample b/config.yaml.sample
index e379015..a3c2cdf 100644
--- a/config.yaml.sample
+++ b/config.yaml.sample
@@ -5,6 +5,9 @@
 redis:
     host: tools-mc
     db: 7
+mysql:
+    host: tools-db
+    db: someprefixhere_subscriptions
 stream_receiver:
     redis_prefix: "some-long-prefix-that-was-generated-by-openssl"
     clients_key: "key-to-use-to-store-clients-list"
diff --git a/src/registrar.py b/src/registrar.py
index d10be20..1285ae3 100644
--- a/src/registrar.py
+++ b/src/registrar.py
@@ -8,6 +8,8 @@
 import zmq
 import redis
 
+import storage
+
 
 BASE_PATH = os.path.join(os.path.dirname(__file__), '..')
 ADDRESS_PATH = os.path.join(BASE_PATH, 'registrar')
@@ -19,12 +21,18 @@
 REDIS_HOST = config['redis']['host']
 PREFIX = config['stream_receiver']['redis_prefix']
 CLIENTS_KEY = config['stream_receiver']['clients_key']
+MYSQL_DB = config['mysql']['db']
+MYSQL_HOST = config['mysql']['host']
 
 logging.basicConfig(format='%(asctime)s %(message)s', 
filename=os.path.expanduser('~/logs/stream-subscriptions'), level=logging.INFO)
 
 logging.info('Attempting to Redis connection to %s', REDIS_HOST)
 red = redis.StrictRedis(host=REDIS_HOST, db=REDIS_DB)
 logging.info('Redis connection to %s succeded', REDIS_HOST)
+
+logging.info('Attempting to connect to MySql on %s', MYSQL_HOST)
+db = storage.DB(MYSQL_HOST, MYSQL_DB)
+logging.info('MySql connection to %s suceceded', MYSQL_HOST)
 
 def make_key(*key_parts):
     return PREFIX + "_" + '.'.join(key_parts)
@@ -33,6 +41,7 @@
     # Save these explicitly, to avoid them from getting lost
     key = generate_random_string(32)
     red.pipeline().sadd(make_key(CLIENTS_KEY), key).save().execute()
+    db.insert(key, msg['service'], msg['description'], msg['createdby'])
     logging.info('Added key %s' % key)
     return "Created key: %s and started subscription" % key
 
diff --git a/src/storage.py b/src/storage.py
new file mode 100644
index 0000000..bfd16ca
--- /dev/null
+++ b/src/storage.py
@@ -0,0 +1,11 @@
+import MySQLdb as mysql
+
+class DB(object):
+    INSERT_SQL = "INSERT INTO subscriptions (queue_key, service, description, 
createdby) VALUES (%s, %s, %s, %s)"
+
+    def __init__(self, host, db, defaults_file="~/replica.my.cnf"):
+        self.conn = mysql.connect(host=host, db=db, 
read_default_file=defaults_file)
+
+    def insert(self, queue_key, service, description, createdby):
+        con.execute(INSERT_SQL, (queue_key, service, description, createdby))
+        con.commit()
diff --git a/src/subscriptions.py b/src/subscriptions.py
index 80e3862..c64b814 100755
--- a/src/subscriptions.py
+++ b/src/subscriptions.py
@@ -29,7 +29,7 @@
 
 @command
 def add_subscription(args):
-    return {'action': 'add'}
+    return {'action': 'add', 'description': args.description}
 
 @command
 def delete_subscription(args):
diff --git a/src/subscriptions.sql b/src/subscriptions.sql
new file mode 100644
index 0000000..4089e6a
--- /dev/null
+++ b/src/subscriptions.sql
@@ -0,0 +1,9 @@
+-- Holds meta information about the subscriptions
+CREATE TABLE subscriptions (
+    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
+    queue_key VARCHAR(1024) NOT NULL,
+    service VARCHAR(255) NOT NULL,
+    description VARCHAR(1024) NOT NULL,
+    createdby VARCHAR(255) NOT NULL, -- Is not very reliable, really
+    started TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
+);

-- 
To view, visit https://gerrit.wikimedia.org/r/77300
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iff63c68bbcdb0d2fce00a9226f22ae86e43ae392
Gerrit-PatchSet: 1
Gerrit-Project: labs/tools/gerrit-to-redis
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to