start celery periodic task from specific time

51 views
Skip to first unread message

pgopa...@gmail.com

unread,
Aug 2, 2018, 8:29:51 AM8/2/18
to Django users
 My task is: 
    Suppose one activity has to be started at specific time. The person who started activity will change the activity status from Open to In progress. I've to check the activity status and if the status is still Not started at that time, then I've to send mails periodically for every 15 or 20 minutes.


Code I try to execute for every 15 sec from a specific time using apply_async(eta)
I triggered this periodic task in signals (when ever activity created or updated) used apply_async(args, kwargs ,eta). I'm sending activity instance(Activity Object having all activity details including status) in kwargs to the task above I mentioned. 
If I update the activity status then only for one time the task is taking the args, kwargs from the signal
In second beat task is taking args, kwargs from the CELERYBEAT_SCHEDULE settings (My periodic task should not use these args and kwargs)

issue is: celery task is taking args, kwargs from the CELERYBEAT_SCHEDULE settings in the second beat.
Pls help someone 


Below is the code for your reference 
1.celery beat settings
2.my signals triggers celery task upon Activity model updation
3.celery task


CELERYBEAT_SCHEDULE = {
    'periodic_send_email': {
        'task': 'cloud_app.tasks.periodic_send_email',
        'schedule': timedelta(seconds=15),
        'args': (16, 16)
    },
}
@receiver(signals.post_save, sender=PlannedActivity)
def on_PlannedActivity_model_changed(sender, instance, **kwargs):
    """
    receiver for sending email upon Activity creation and updatation
    """
    eta = None
    if instance.severity == "High":
        eta = instance.start

    if kwargs['created']:
        subject = "New Activity creation success"
        periodic_send_email.apply_async(["arg1", ], {'subject': subject, 'instance': instance, 'action': 'create'}, countdown=10, expires=60, headers={'header': 'myheader'} )
    else:
        subject = "Activity updation success"
        periodic_send_email.apply_async(args=["arg1", "arg2"], kwargs={'subject': subject, 'instance': instance, 'action': 'update'}, eta=instance.start, retry= True)

#celery task 
@shared_task
def periodic_send_email(self, *args, **kwargs):

    print 'args: periodic_send_email:', args
    print 'kwargs: periodic_send_email:', kwargs
    print 'this is periodic send email task'
    # print "self.args: ", args
    # print "self.kwargs: ", kwargs
    # team = instance.team
    # title = instance.title
    # description = instance.description
    # start = str(instance.start)
    # end = str(instance.end)
    # severity = instance.severity
    # status = instance.status
    #
    # body = "\nActivity details: \n\n"  + '\nTitle:       ' + title  + '\nDescription: ' + description + '\nStart at:    ' + start + '\nEnd at:      ' + end + '\nSeverity:    ' + severity + '\nStatus:      ' + status
    #
    # print body
    to_mail = 'pgopa...@gmail.com'
    # print "body:", body
    # send_mail(subject, body, 'vgopa...@gmail.com', [to_mail, 'vgopa...@gmail.com'])
    return "success email send"

worker.PNG
Reply all
Reply to author
Forward
0 new messages