def get(self): print '[Cron::SendReportCronJob] started' # get the last report time global global_last_report_time if not global_last_report_time: global_last_report_time = datetime.now() return print '[Cron::SendReportCronJob] global_last_report_time:', global_last_report_time # get the sending rate reportRate = ReportSendingRate.query().fetch() if len(reportRate) != 0: reportRate = reportRate[0].sending_rate print '[Cron::SendReportCronJob] reportRate:', reportRate interval_in_mins = -99 if reportRate == 0: # No reports. Return return elif reportRate == 1: interval_in_mins = 5 elif reportRate == 2: interval_in_mins = 60 elif reportRate == 3: interval_in_mins = 1440 # one day else: print 'Error in report rate : ', reportRate # comparing the sending rate with the time interval since the last report time_passed = (datetime.now() - global_last_report_time).seconds print '[Cron::SendReportCronJob] time_passed:', time_passed if time_passed < int(interval_in_mins)*60: return # send the report mail trending_stream_lst = Stream.query().order(-Stream.views_cnt).fetch(3) sender_mail_addr = REPORT_SENDER_NAME + "@" + APP_ID + MAILBOX_SURFIX subject = "Trending Streams from Connexus " + str(global_last_report_time) body = """ Here are the trending streams on Connexus now: \n 1) %s - %s \n 2) %s - %s \n 3) %s - %s \n """ % (trending_stream_lst[0].stream_name, trending_stream_lst[0].user_email, trending_stream_lst[1].stream_name, trending_stream_lst[1].user_email, trending_stream_lst[2].stream_name, trending_stream_lst[2].user_email) for receiver_addr in report_email_list: mail.send_mail(sender_mail_addr, receiver_addr, subject, body) print "Successfully sent the trending email to " + str(report_email_list) #reset the timer global_last_report_time = datetime.now()
def get(self): upd_send_rate = self.request.get("sending_rate") print '[UpdateReportSendingRate]:: get sending_rate update: ', upd_send_rate reportRate = ReportSendingRate.query().fetch() if len(reportRate) != 0: ori_send_rate = reportRate[0].sending_rate reportRate[0].update_sending_rate(int(upd_send_rate)) print '[UpdateReportSendingRate]:: change sending rate from ', ori_send_rate, ' to ', upd_send_rate else: print "[UpdateReportSendingRate]:: cannot find the sending rate configuration. Setting the sending rate " \ "to 1 hour" dft_send_rate = ReportSendingRate(user_email = report_email_list[0], sending_rate = DEFAULT_SENDING_RATE) dft_send_rate.put() self.redirect('/trending')