cic-internal-integration/apps/cic-notify/cic_notify/api.py

41 lines
1.4 KiB
Python
Raw Normal View History

2021-02-07 03:29:24 +01:00
# standard imports
import logging
import re
# third-party imports
import cic_notify.tasks.sms.db
2021-04-07 08:31:26 +02:00
from celery.app.control import Inspect
2021-02-07 03:29:24 +01:00
import celery
# local imports
from cic_notify.tasks import sms
app = celery.current_app
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
2021-04-07 08:31:26 +02:00
2021-02-07 03:29:24 +01:00
class Api:
def __init__(self, queue: any = 'cic-notify'):
2021-02-07 03:29:24 +01:00
"""
:param queue: The queue on which to execute notification tasks
:type queue: str
"""
self.queue = queue
2021-02-07 03:29:24 +01:00
def sms(self, message: str, recipient: str):
2021-02-07 03:29:24 +01:00
"""This function chains all sms tasks in order to send a message, log and persist said data to disk
:param message: The message to be sent to the recipient.
:type message: str
:param recipient: The phone number of the recipient.
:type recipient: str
:return: a celery Task
:rtype: Celery.Task
"""
s_send = celery.signature('cic_notify.tasks.sms.africastalking.send', [message, recipient], queue=self.queue)
s_log = celery.signature('cic_notify.tasks.sms.log.log', [message, recipient], queue=self.queue)
s_persist_notification = celery.signature(
'cic_notify.tasks.sms.db.persist_notification', [message, recipient], queue=self.queue)
signatures = [s_send, s_log, s_persist_notification]
return celery.group(signatures)()