Say our blog has a lot of people who don't use RSS, and prefer mailing lists. 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 blog/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(7 - date.weekday())
else:
date = date - datetime.timedelta(date.weekday())
delta = datetime.timedelta(days=(week - 1) * 7)
start, end = date + delta, date ...