0

I am using this class to push notification via celery. The class definition goes like this.

from celery import Task
class NotifyUser(Task):
    """
    send push notification, entry point for this purpose is
    def send_notification() 
    """
    def __init__(self, users=None, notification_code=None, context=None, merchant_flag=False,
        listing_flag=False, object_type=None, object_id=None, category_id=None, *args, **kwargs):

        Task.__init__(self, *args, **kwargs)

        self.notification_code = notification_code
        self.users = users
        self.context = context
        self.merchant_flag = merchant_flag
        self.object_type = object_type
        self.object_id = object_id
        self.category_id = category_id
        self.listing_flag = listing_flag
        self.object_map = {
            'event':Event,
            'experience':Experience,
            'restaurant':Restaurant
            }

    def get_devices(self):
        gd = GCMDevice.objects.filter(user__in=self.users)
        ad = APNSDevice.objects.filter(user__in=self.users)
        return list(chain(ad, gd))

    def get_notification_message(self):
        if self.notification_code:
            return NotificationMessage.objects.get(message_code=self.notification_code)
        elif self.listing_flag:
            if self.category_id != u'0':
                return NotificationMessage.objects.get(listing_category=self.category_id)
        elif self.merchant_flag:
            if self.object_type:
                obj = self.object_map[self.object_type].objects.get(**{'id':self.object_id})
                return  NotificationMessage.objects.get(**{self.object_type:obj})

    def get_message_string(self, nf):
        template = Template(nf.message)
        context = Context(self.context)
        return template.render(context)

    def send_notification(self):

        devices = self.get_devices()
        nf = self.get_notification_message()
        nf.__dict__['message'] = self.get_message_string(nf) 
        self.send(devices, nf.__dict__)

    def run(self, source, *args, **kwargs): 
        self.send_notification()

    def send(self, devices, payload):
        print payload
        for dev in devices:
            #dev.send_message(payload)
            logger.debug("notification sent to {0} at {1}".format(dev.user.email, datetime.now()))

i make a object of NotifyUser, passing parameters to it like this:

notify_user = NotifyUser(users=queryset.values_list('id', flat=True), category_id=category_id, listing_flag=True)

and then use delay over it.

notify_user.delay(10)

I have tried pdb;pdb.set_trace() after i make a object the data that i used to instantiate is there,

{'object_map': {'event': <class 'merchant.models.Event'>, 'experience': <class 'merchant.models.Experience'>, 'restaurant': <class 'merchant.models.Restaurant'>}, 'users': [2], 'notification_code': None, 'object_type': None, 'object_id': None, 'merchant_flag': False, 'context': None, 'listing_flag': True, 'category_id': u'1'}

but once i use notify_user.delay(10), (using pdb again !), in my run method, when i do self.dict it gives me all the default values that i use in making object of the NotifyUSer class, like this:

{'object_map': {'event': <class 'merchant.models.Event'>, 'experience': <class 'merchant.models.Experience'>, 'restaurant': <class 'merchant.models.Restaurant'>}, 'users': None, 'notification_code': None, 'object_type': None, 'object_id': None, 'merchant_flag': False, 'context': None, 'listing_flag': False, 'category_id': None}

why is this happening I am new to django celery, pleaes guide me to correct solution. Thanks

5
  • Where did you get the idea to use the Task class? Normally you define a function which you decorate with @app.task. I would be surprised if the class is instantiated on each call. Commented Feb 1, 2016 at 13:19
  • i read it from some blog, and it works NotifyUser().delay(**kwargs) :) Commented Feb 2, 2016 at 6:30
  • @vijayshanker you may want to read this: docs.celeryproject.org/en/latest/userguide/… Commented Feb 2, 2016 at 14:40
  • @DanielRoseman custom Task subclasses are a documented feature of Celery. The OP is just not using the feature correctly... Commented Feb 2, 2016 at 14:42
  • hence, is this shulhi.com/class-based-celery-task wrong? Commented Nov 11, 2016 at 14:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.