This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2: Imitation of sending letters
- Loading branch information
Showing
10 changed files
with
122 additions
and
52 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from logging import info | ||
from sys import exit | ||
|
||
from pika.adapters.blocking_connection import BlockingChannel | ||
|
||
from connect import init | ||
from services.rabbitmq.models import Contact | ||
|
||
|
||
def send(email: str) -> bool: | ||
'''Stub function. | ||
:param email: An E-mail address. | ||
:return: True if a letter has been sent successfully. | ||
''' | ||
... | ||
|
||
|
||
def callback(channel: BlockingChannel, method, properties, body) -> None: | ||
if (contact := Contact.objects(id=body.decode(), delivered=False).first()): | ||
info(f'Sending letter to {contact.email}...') | ||
|
||
send(contact.email) | ||
|
||
contact.update(set__delivered=True) | ||
|
||
|
||
def main() -> None: | ||
if not init() or not (data := init(True)): | ||
return | ||
|
||
_, channel, queue_name = data | ||
|
||
channel.basic_qos(prefetch_count=1) | ||
channel.basic_consume(queue_name, callback, True) | ||
|
||
info('Waiting for messages. Press CTRL+C to exit.') | ||
|
||
channel.start_consuming() | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
main() | ||
except KeyboardInterrupt: | ||
exit(0) |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from mongoengine import connect | ||
from mongoengine.connection import ConnectionFailure | ||
from pymongo.errors import ConfigurationError | ||
|
||
|
||
def handler(credentials: list) -> bool: | ||
URI = 'mongodb+srv://{}:{}@{}/?retryWrites=true&w=majority' | ||
|
||
try: | ||
connect( | ||
db=credentials.pop(), | ||
host=URI.format(*credentials), | ||
tls=True, | ||
tlsAllowInvalidCertificates=True | ||
) | ||
except (ConfigurationError, ConnectionFailure): | ||
raise Exception('Invalid credentials.') | ||
|
||
return True |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from pika import BlockingConnection, ConnectionParameters, PlainCredentials | ||
|
||
|
||
def handler(credentials: list) -> tuple: | ||
connection = BlockingConnection( | ||
ConnectionParameters( | ||
credentials[2], | ||
credentials[3], | ||
credentials=PlainCredentials(credentials[0], credentials[1]), | ||
), | ||
) | ||
|
||
channel = connection.channel() | ||
channel.queue_declare(credentials[4], durable=True) | ||
|
||
return connection, channel, credentials[4] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from mongoengine import Document | ||
from mongoengine.fields import BooleanField, EmailField, StringField | ||
|
||
|
||
class Contact(Document): | ||
fullname = StringField(max_length=50, required=True) | ||
email = EmailField(required=True) | ||
address = StringField(max_length=200) | ||
delivered = BooleanField(required=True, default=False) | ||
meta = {'collection': 'contacts'} |