skip to Main Content

I am trying to send notifications using celery.

@shared_task(name='send_notifis')
def send_notifs(device_ids, title, message):
    from pills_reminder.models import UserNotifications, UserDevice
    devices = UserDevice.objects.filter(id__in=device_ids)
    print(devices)
    device_tokens = []
    for device in devices:
        UserNotifications.objects.create(
            uid=device.device_id,
            title=title,
            message=message,
        )
        print(UserNotifications)
        device_tokens.append(device.registration_token)

    if len(device_tokens) > 1:
        device_tokens = ["".join(token.split()) for token in device_tokens]
        response = push_service.notify_multiple_devices(registration_ids=device_tokens,
                                             message_title=title,
                                             message_body=message)
    elif len(device_tokens) == 1:
        registration_id = "".join(device_tokens[0].split())
        response = push_service.notify_single_device(registration_id=registration_id,
                                          message_title=title,
                                          message_body=message)
    else:
        pass
    print(response)
    return True

this works without .delay() and when running using
python manage.py shell

>>> send_notifs.delay(devices, title='title', message='message')
<AsyncResult: f54188f8-cec6-42dd-a840-b097abffd7f4>

but it freezes when i call using Django Model post_save signal.

@receiver(post_save, sender=Notification)
def Notification_post_save_handler(sender, instance, **kwargs):
    print('hello from post_save signal')
    devices = instance.get_devices()
    # send_notifs(devices)
    if len(devices)>0:
        send_notifs.delay(devices,
                    title=instance.title,
                    message=instance.message)

This above code freezes execution, but without .delay. it works fine.

UPADATE:1

the above task with .delay is running from python manage.py shell not from runserver . so the problem is with celery and Django settings. Hence i dig deep and found out

while running from shell i get,

>>> add.app.conf #(add is a async task here)
{'broker_url': 'redis://localhost:6379/1'}, ...

but running from runserver gives:

`{'broker_url': None}`

Now i am looking for how to set the settings properly ? I am using django-configurations with celery.py as

from __future__ import absolute_import, unicode_literals

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Development')

import configurations
configurations.setup()

app = Celery('core')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

Any help is appreciated. Thank you for your time and patience.

2

Answers


  1. Chosen as BEST ANSWER

    Turns out the settings sepecified in django-configuration docs for including celery was causing the problem. Modified celery.py, and now it is working fine.

    from __future__ import absolute_import, unicode_literals
    
    import os
    
    from celery import Celery
    
    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
    os.environ.setdefault('DJANGO_CONFIGURATION', 'Development')
    
    from configurations import importer
    importer.install()
    
    # import configurations
    # configurations.setup()
    
    app = Celery('core')
    app.config_from_object('django.conf:settings', namespace='CELERY')
    app.autodiscover_tasks()
    
    
    @app.task(bind=True)
    def debug_task(self):
        print('Request: {0!r}'.format(self.request))
    

  2. use this

    from configurations import importer
    importer.install()
    

    in place of

    import configurations
    configurations.setup()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search