Class based Task in django celery

巧了我就是萌 提交于 2020-01-07 02:56:05

问题


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

来源:https://stackoverflow.com/questions/35128946/class-based-task-in-django-celery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!