def go(): # Retrieve and process email-to-diagnostic-info records. # Note that `_email_diagnostic_info_records` throttles itself if/when # there are no records immediately available. for email_diagnostic_info in _email_diagnostic_info_records_iterator(): # Check if there is (yet) a corresponding diagnostic info record diagnostic_info = datastore.find_diagnostic_info( email_diagnostic_info['diagnostic_info_record_id']) if not diagnostic_info: continue # Modifies diagnostic_info _clean_diagnostic_info_for_yaml_dumping(diagnostic_info) # Convert the modified YAML back into a string for emailing. diagnostic_info_text = yaml.safe_dump(diagnostic_info, default_flow_style=False, width=75) try: diagnostic_info_html = mailformatter.format(diagnostic_info) except Exception as e: logger.error('format failed: %s' % str(e)) diagnostic_info_html = None # If we get to here, then we have a valid diagnostic email. # Reply with the decrypted content. # If this is not a reply, set a subject # If no subject is pre-determined, create one. if email_diagnostic_info.get('email_id') is None: subject = u'DiagnosticInfo: %s (%s)' % ( diagnostic_info['Metadata'].get('platform', '[NO_PLATFORM]').capitalize(), diagnostic_info['Metadata'].get('id', '[NO_ID]')) else: subject = u'Re: %s' % (email_diagnostic_info['email_subject'] or '') try: sender.send_response( config['decryptedEmailRecipient'], config['emailUsername'], subject, diagnostic_info_text, diagnostic_info_html, email_diagnostic_info.get('email_id'), # may be None None) # no attachment logger.log('decrypted formatted email sent') except smtplib.SMTPException as e: logger.exception() logger.error(str(e)) # Delete the processed record. (Note that sending the email might have # failed, but we're deleting it anyway. This is a debatable decision.) datastore.remove_email_diagnostic_info(email_diagnostic_info)
def go(): # Retrieve and process email-to-diagnostic-info records. # Note that `_email_diagnostic_info_records` throttles itself if/when # there are no records immediately available. for email_diagnostic_info in _email_diagnostic_info_records_iterator(): # Check if there is (yet) a corresponding diagnostic info record diagnostic_info = datastore.find_diagnostic_info(email_diagnostic_info['diagnostic_info_record_id']) if not diagnostic_info: continue # Modifies diagnostic_info _clean_diagnostic_info_for_yaml_dumping(diagnostic_info) # Convert the modified YAML back into a string for emailing. diagnostic_info_text = yaml.safe_dump(diagnostic_info, default_flow_style=False, width=75) try: diagnostic_info_html = mailformatter.format(diagnostic_info) except Exception as e: logger.error('format failed: %s' % str(e)) diagnostic_info_html = None # If we get to here, then we have a valid diagnostic email. # Reply with the decrypted content. # If this is not a reply, set a subject # If no subject is pre-determined, create one. if email_diagnostic_info.get('email_id') is None: subject = u'DiagnosticInfo: %s (%s)' % (diagnostic_info['Metadata'].get('platform', '[NO_PLATFORM]').capitalize(), diagnostic_info['Metadata'].get('id', '[NO_ID]')) else: subject = u'Re: %s' % (email_diagnostic_info['email_subject'] or '') try: sender.send_response(config['decryptedEmailRecipient'], config['emailUsername'], subject, diagnostic_info_text, diagnostic_info_html, email_diagnostic_info.get('email_id'), # may be None None) # no attachment logger.log('decrypted formatted email sent') except smtplib.SMTPException as e: logger.exception() logger.error(str(e)) # Delete the processed record. (Note that sending the email might have # failed, but we're deleting it anyway. This is a debatable decision.) datastore.remove_email_diagnostic_info(email_diagnostic_info)
def _autoresponder_record_iter(): logger.debug_log('_autoresponder_record_iter: enter') while True: for rec in datastore.get_autoresponder_iterator(): logger.debug_log('_autoresponder_record_iter: %s' % repr(rec)) if rec.get('diagnostic_info_record_id'): rec['diagnostic_info'] = datastore.find_diagnostic_info(rec.get('diagnostic_info_record_id')) logger.debug_log('_autoresponder_record_iter: yielding rec: %s' % rec['_id']) yield rec logger.debug_log('_diagnostic_record_iter: sleeping') time.sleep(_SLEEP_TIME_SECS) logger.debug_log('_autoresponder_record_iter: exit')
def _process_work_items(work_queue): ''' This runs in the multiprocessing forks to do the actual work. It is a long-lived loop. ''' while True: if terminate: logger.debug_log('got terminate; stopping work') break logger.debug_log('_process_work_items: dequeueing work item') # This blocks if the queue is empty email_diagnostic_info = work_queue.get() logger.debug_log('_process_work_items: dequeued work item') logger.debug_log('feedback object id: %s' % email_diagnostic_info['diagnostic_info_record_id']) # Check if there is (yet) a corresponding diagnostic info record diagnostic_info = datastore.find_diagnostic_info( email_diagnostic_info['diagnostic_info_record_id']) if not diagnostic_info: logger.debug_log('diagnostic_info not found; skipping') continue logger.log('feedback id: %s' % diagnostic_info.get('Metadata', {}).get('id')) diagnostic_info_text = pprint.pformat(diagnostic_info, indent=1, width=75) try: diagnostic_info_html = mailformatter.format(diagnostic_info) except Exception as e: logger.error('format failed: %s' % str(e)) diagnostic_info_html = None # If we get to here, then we have a valid diagnostic email. # Reply with the decrypted content. # If this is not a reply, set a subject # If no subject is pre-determined, create one. if email_diagnostic_info.get('email_id') is None: subject = u'DiagnosticInfo: %s (%s)' % ( diagnostic_info['Metadata'].get('platform', '[NO_PLATFORM]').capitalize(), diagnostic_info['Metadata'].get('id', '[NO_ID]')) else: subject = u'Re: %s' % (email_diagnostic_info['email_subject'] or '') try: sender.send_response( config['decryptedEmailRecipient'], config['emailUsername'], subject, diagnostic_info_text, diagnostic_info_html, email_diagnostic_info.get('email_id'), # may be None None) # no attachment logger.log('decrypted formatted email sent') except Exception as e: logger.exception() logger.error(str(e)) # Delete the processed record. (Note that sending the email might have # failed, but we're deleting it anyway. This is a debatable decision.) datastore.remove_email_diagnostic_info(email_diagnostic_info)