@periodic_task(run_every=crontab(minute=0), queue='background_queue')
def pillow_retry_notifier():
    enddate = datetime.utcnow()
    startdate = enddate - timedelta(hours=1)
    results = PillowError.objects \
        .filter(date_last_attempt__gte=startdate) \
        .filter(current_attempt__gte=settings.PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS) \
        .values_list('pillow', 'error_type') \
        .annotate(Count('pillow')) \
        .order_by('-pillow__count').all()

    if results:
        results = list(results)
        text_rows = format_text_table([('Pillow', 'Error', 'Count')] + results)
        context = {
            'startdate': startdate,
            'enddate': enddate,
            'rows': text_rows,
            'url': get_url_base() + reverse('admin_report_dispatcher', args=('pillow_errors',))
        }
        text_message = render_to_string('hqpillow_retry/email.txt', context)
        context['rows'] = results
        html_message = render_to_string('hqpillow_retry/email.html', context)
        mail_admins('PillowTop errors in the last hour', text_message, html_message=html_message)
dataset/ETHPy150Open dimagi/commcare-hq/corehq/apps/hqpillow_retry/tasks.py/pillow_retry_notifier/original