diff options
author | 2018-05-30 22:33:42 +0100 | |
---|---|---|
committer | 2018-05-30 22:33:42 +0100 | |
commit | 3f5433c5c41cf9a6ff2bbe6f5784d1286ef87a24 (patch) | |
tree | 5d3e9ee7c8b598c21f72479275cb18c9a86b25d8 | |
parent | Escape unicode (#92) (diff) |
RabbitMQ Cog (#90)
* Add aio-pika reqs and remove flake8-bandit so I can lint again
* RMQ cog
* Address review
* Address review; remove unnecessary function
-rw-r--r-- | Pipfile | 2 | ||||
-rw-r--r-- | Pipfile.lock | 80 | ||||
-rw-r--r-- | bot/__main__.py | 1 | ||||
-rw-r--r-- | bot/cogs/rmq.py | 56 | ||||
-rw-r--r-- | bot/constants.py | 9 | ||||
-rw-r--r-- | config-default.yml | 7 |
6 files changed, 96 insertions, 59 deletions
@@ -17,11 +17,11 @@ yarl = "==1.1.1" fuzzywuzzy = "*" python-levenshtein = "*" pillow = "*" +aio-pika = "*" [dev-packages] "flake8" = "*" "flake8-bugbear" = "*" -"flake8-bandit" = "*" "flake8-import-order" = "*" "flake8-tidy-imports" = "*" "flake8-todo" = "*" diff --git a/Pipfile.lock b/Pipfile.lock index f84c9e7ae..5285bc3d8 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "4d01fc2c93d23bfdeddf3dc68c723aa7eb8598380c5fdb0a6bf67a39f84b03a6" + "sha256": "2599ad9a90ffd20ee43b33582acee7e124146b6de58f648fd3a0175f19b45007" }, "pipfile-spec": 6, "requires": { @@ -16,6 +16,14 @@ ] }, "default": { + "aio-pika": { + "hashes": [ + "sha256:ba019bf310f3c35c800a8977cb7c3ed3b40ff6b906e055584f0fbe345a563d2d", + "sha256:d84aa052dea2b9d365b8915953f558de8bdc69830b2aca1531e4d9a5d099daaa" + ], + "index": "pypi", + "version": "==2.8.1" + }, "aiodns": { "hashes": [ "sha256:99d0652f2c02f73bfa646bf44af82705260a523014576647d7959e664830b26b", @@ -122,6 +130,13 @@ "index": "pypi", "version": "==4.3.1" }, + "pika": { + "hashes": [ + "sha256:63131aaeec48a6c8f1db1fe657e1e74cf384c3927eb7d1725e31edae4220dea4", + "sha256:7277b4d12a99efa4058782614d84138983f9f89d690bdfcea66290d810806459" + ], + "version": "==0.10.0" + }, "pillow": { "hashes": [ "sha256:00633bc2ec40313f4daf351855e506d296ec3c553f21b66720d0f1225ca84c6f", @@ -231,6 +246,12 @@ "index": "pypi", "version": "==3.12" }, + "shortuuid": { + "hashes": [ + "sha256:d08fd398f40f8baf87e15eef8355e92fa541bca4eb8465fefab7ee22f92711b9" + ], + "version": "==0.5.0" + }, "sympy": { "hashes": [ "sha256:ac5b57691bc43919dcc21167660a57cc51797c28a4301a6144eff07b751216a4" @@ -295,13 +316,6 @@ ], "version": "==18.1.0" }, - "bandit": { - "hashes": [ - "sha256:cb977045497f83ec3a02616973ab845c829cdab8144ce2e757fe031104a9abd4", - "sha256:de4cc19d6ba32d6f542c6a1ddadb4404571347d83ef1ed1e7afb7d0b38e0c25b" - ], - "version": "==1.4.0" - }, "certifi": { "hashes": [ "sha256:13e698f54293db9f89122b0581843a782ad0934a4fe0172d2a980ba77fc61bb7", @@ -345,14 +359,6 @@ "index": "pypi", "version": "==3.5.0" }, - "flake8-bandit": { - "hashes": [ - "sha256:a66c7b42af9530d5e988851ccee02958a51a85d46f1f4609ecc3546948f809b8", - "sha256:f7c3421fd9aebc63689c0693511e16dcad678fd4a0ce624b78ca91ae713eacdc" - ], - "index": "pypi", - "version": "==1.0.2" - }, "flake8-bugbear": { "hashes": [ "sha256:541746f0f3b2f1a8d7278e1d2d218df298996b60b02677708560db7c7e620e3b", @@ -369,13 +375,6 @@ "index": "pypi", "version": "==0.17.1" }, - "flake8-polyfill": { - "hashes": [ - "sha256:12be6a34ee3ab795b19ca73505e7b55826d5f6ad7230d31b18e106400169b9e9", - "sha256:e44b087597f6da52ec6393a709e7108b2905317d0c0b744cdca6208e670d8eda" - ], - "version": "==1.0.2" - }, "flake8-string-format": { "hashes": [ "sha256:68ea72a1a5b75e7018cae44d14f32473c798cf73d75cbaed86c6a9a907b770b2", @@ -399,20 +398,6 @@ "index": "pypi", "version": "==0.7" }, - "gitdb2": { - "hashes": [ - "sha256:b60e29d4533e5e25bb50b7678bbc187c8f6bcff1344b4f293b2ba55c85795f09", - "sha256:cf9a4b68e8c4da8d42e48728c944ff7af2d8c9db303ac1ab32eac37aa4194b0e" - ], - "version": "==2.0.3" - }, - "gitpython": { - "hashes": [ - "sha256:1ec4c44846cf76a1e55769560673a97731849c9d05401e035e607495f10db959", - "sha256:b60b045cf64a321e5b620debb49890099fa6c7be6dfb7fb249027e5d34227301" - ], - "version": "==2.1.10" - }, "idna": { "hashes": [ "sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f", @@ -434,13 +419,6 @@ ], "version": "==17.1" }, - "pbr": { - "hashes": [ - "sha256:680bf5ba9b28dd56e08eb7c267991a37c7a5f90a92c2e07108829931a50ff80a", - "sha256:6874feb22334a1e9a515193cba797664e940b763440c88115009ec323a7f2df5" - ], - "version": "==4.0.3" - }, "pycodestyle": { "hashes": [ "sha256:682256a5b318149ca0d2a9185d365d8864a768a28db66a84a2ea946bcc426766", @@ -510,20 +488,6 @@ ], "version": "==1.11.0" }, - "smmap2": { - "hashes": [ - "sha256:b78ee0f1f5772d69ff50b1cbdb01b8c6647a8354f02f23b488cf4b2cfc923956", - "sha256:c7530db63f15f09f8251094b22091298e82bf6c699a6b8344aaaef3f2e1276c3" - ], - "version": "==2.0.3" - }, - "stevedore": { - "hashes": [ - "sha256:e3d96b2c4e882ec0c1ff95eaebf7b575a779fd0ccb4c741b9832bed410d58b3d", - "sha256:f1c7518e7b160336040fee272174f1f7b29a46febb3632502a8f2055f973d60b" - ], - "version": "==1.28.0" - }, "urllib3": { "hashes": [ "sha256:06330f386d6e4b195fbfc736b297f58c5a892e4440e54d294d7004e3a9bbea1b", diff --git a/bot/__main__.py b/bot/__main__.py index c4e42dd45..f6e5dfe0c 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -38,6 +38,7 @@ bot.http_session = ClientSession( # Internal/debug bot.load_extension("bot.cogs.logging") +bot.load_extension("bot.cogs.rmq") bot.load_extension("bot.cogs.security") bot.load_extension("bot.cogs.events") diff --git a/bot/cogs/rmq.py b/bot/cogs/rmq.py new file mode 100644 index 000000000..f6cf7332b --- /dev/null +++ b/bot/cogs/rmq.py @@ -0,0 +1,56 @@ +import json +import logging + +import aio_pika +from discord.ext.commands import AutoShardedBot + +from bot.constants import Channels, RabbitMQ + +log = logging.getLogger(__name__) + + +class RMQ: + """ + RabbitMQ event handling + """ + + rmq = None # type: aio_pika.Connection + channel = None # type: aio_pika.Channel + queue = None # type: aio_pika.Queue + + def __init__(self, bot: AutoShardedBot): + self.bot = bot + + async def on_ready(self): + self.rmq = await aio_pika.connect_robust( + host=RabbitMQ.host, port=RabbitMQ.port, login=RabbitMQ.username, password=RabbitMQ.password + ) + + log.info("Connected to RabbitMQ") + + self.channel = await self.rmq.channel() + self.queue = await self.channel.declare_queue("bot_events", durable=True) + + log.debug("Channel opened, queue declared") + + async for message in self.queue: + with message.process(): + await self.handle_message(message, message.body.decode()) + + async def handle_message(self, message, data): + log.debug(f"Message: {message}") + log.debug(f"Data: {data}") + + try: + data = json.loads(data) + await self.send_test(f"JSON: {data}") + except Exception: + await self.send_test(f"Non-JSON: {data}") + + async def send_test(self, data): + await self.bot.get_channel(Channels.devtest).send(data) + + +def setup(bot): + bot.add_cog(RMQ(bot)) + log.info("Cog loaded: RMQ") diff --git a/bot/constants.py b/bot/constants.py index cbad02ad1..50a316d26 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -240,6 +240,15 @@ class Papertrail(metaclass=YAMLGetter): port: str +class RabbitMQ(metaclass=YAMLGetter): + section = "rabbitmq" + + host: str + port: int + username: str + password: str + + class URLs(metaclass=YAMLGetter): section = "urls" diff --git a/config-default.yml b/config-default.yml index 5670ac1da..eda77c2d0 100644 --- a/config-default.yml +++ b/config-default.yml @@ -60,6 +60,13 @@ papertrail: port: !ENV 'PAPERTRAIL_PORT' +rabbitmq: + host: "pdrmq" + port: 5672 + username: !ENV "RABBITMQ_DEFAULT_USER" + password: !ENV "RABBITMQ_DEFAULT_PASS" + + urls: deploy: !ENV 'DEPLOY_URL' status: !ENV 'STATUS_URL' |