<s> from datetime import datetime , timedelta <EOL> from celery . schedules import crontab <EOL> from celery . task . base import periodic_task <EOL> from django . core . mail import mail_admins <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models . aggregates import Count <EOL> from django . template . loader import render_to_string <EOL> from dimagi . utils . web import get_url_base <EOL> from pillow_retry . models import PillowError <EOL> from django . conf import settings <EOL> @ periodic_task ( run_every = crontab ( minute = <NUM_LIT:0> ) , queue = '<STR_LIT>' ) <EOL> def pillow_retry_notifier ( ) : <EOL> enddate = datetime . utcnow ( ) <EOL> startdate = enddate - timedelta ( hours = <NUM_LIT:1> ) <EOL> results = PillowError . objects . filter ( date_last_attempt__gte = startdate ) . filter ( current_attempt__gte = settings . PILLOW_RETRY_QUEUE_MAX_PROCESSING_ATTEMPTS ) . values_list ( '<STR_LIT>' , '<STR_LIT>' ) . annotate ( Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' ) . all ( ) <EOL> if results : <EOL> results = list ( results ) <EOL> text_rows = format_text_table ( [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] + results ) <EOL> context = { <EOL> '<STR_LIT>' : startdate , <EOL> '<STR_LIT>' : enddate , <EOL> '<STR_LIT>' : text_rows , <EOL> '<STR_LIT:url>' : get_url_base ( ) + reverse ( '<STR_LIT>' , args = ( '<STR_LIT>' , ) ) <EOL> } <EOL> text_message = render_to_string ( '<STR_LIT>' , context ) <EOL> context [ '<STR_LIT>' ] = results <EOL> html_message = render_to_string ( '<STR_LIT>' , context ) <EOL> mail_admins ( '<STR_LIT>' , text_message , html_message = html_message ) <EOL> def format_text_table ( table ) : <EOL> col_width = [ max ( len ( str ( x ) ) for x in col ) for col in zip ( * table ) ] <EOL> output = [ ] <EOL> for row in table : <EOL> inner = "<STR_LIT>" . join ( "<STR_LIT>" . format ( x , col_width [ i ] ) for i , x in enumerate ( row ) ) <EOL> output . append ( "<STR_LIT>" . format ( inner ) ) <EOL> return output </s>
