Creating a weekly digest
Say our blog has a lot of people who don't use RSS and prefer mailing lists, which is a large number of users. We need some way to create a list of new posts at the end of every week to increase our site's traffic. To solve this problem, we will create a digest task that will be called by a beat worker at 10 am every Saturday.
First, in tasks.py
, let's create our task as follows:
@celery.task( bind=True, ignore_result=True, default_retry_delay=300, max_retries=5 ) def digest(self): # find the start and end of this week year, week = datetime.datetime.now().isocalendar()[0:2] date = datetime.date(year, 1, 1) if (date.weekday() > 3): date = date + datetime.timedelta(days=7 - date.weekday()) else: date = date - datetime.timedelta(days=date.weekday()) delta = datetime.timedelta(days=(week - 1) * 7) start, end = date + delta, date + delta + datetime.timedelta(days=6) posts = Post.query.filter...