def dispatch(tasks): try: logger.debug("dispatch cert_query trans begin %s" % len(tasks)) logger.debug("dispatch cert_query trans begin task_ids %s" % [i['_id'] for i in tasks]) dev_id, devs, devs_dict = init_cert_dev(tasks[0]) logger.debug("devs is %s" % devs) logger.debug("cert_devs dev_id %s, devs len %s" % (dev_id, len(devs))) save_tasks = [] for task in tasks: task['dev_id'] = dev_id save_task = {} save_task['_id'] = ObjectId(task['_id']) save_task['status'] = "PROGRESS" save_task['dev_id'] = dev_id save_task['query_path'] = task.get('query_path') save_task['username'] = task.get('username') save_task['q_id'] = task.get('q_id') save_task['query_type'] = task.get('query_type') save_task['send_devs'] = task.get('query_dev_ip') save_task['created_time'] = task.get('created_time') save_task['query_config_path'] = task.get('query_config_path') save_task['query_cert_name'] = task.get('query_cert_name') save_task['worker_host'] = WORKER_HOST save_tasks.append(save_task) make_result_cache(task['_id'], devs_dict) s1_db.cert_query_tasks.insert(save_tasks) worker(tasks, devs) logger.debug("query cert trans end") except Exception, e: logger.debug("query cert trans error:%s " % traceback.format_exc(e))
def dispatch(tasks): ''' 分发证书 [{‘key’:xx,’cert’:xx, seed: xxx, dir:xxx, send_devs:’all_hpcc’, 'task_id':xxx} ] ''' try: logger.debug("dispatch cert trans begin %s" % len(tasks)) logger.debug("dispatch cert trans begin task_ids %s" % [i['_id'] for i in tasks]) dev_id, devs, devs_dict = init_cert_devs(tasks[0]['send_devs']) logger.debug("cert_devs dev_id %s, devs len %s" % (dev_id, len(devs))) save_tasks = [] for task in tasks: task['dev_id'] = dev_id save_task = {} save_task['_id'] = ObjectId(task['_id']) save_task['status'] = "PROGRESS" save_task['dev_id'] = dev_id save_task['username'] = task.get('username') save_task['user_id'] = task.get('user_id') save_task['c_id'] = task.get('c_id', '') save_task['o_c_id'] = task.get('o_c_id', '') save_task['cert_alias'] = task.get('cert_alias', '') save_task['send_devs'] = task.get('send_devs', '') save_task['created_time'] = task.get('created_time') save_task['recev_host'] = task.get('recev_host') save_task['worker_host'] = WORKER_HOST save_task['rcms_callback_time'] = '' save_task['portal_callback_time'] = '' save_tasks.append(save_task) make_result_cache(task['_id'], devs_dict) s1_db.cert_trans_tasks.insert(save_tasks) worker(tasks, devs) logger.debug("dispatch cert trans end") except Exception: logger.debug("dispatch cert trans error:%s " % traceback.format_exc())
def dispatch(tasks): try: logger.debug("transfer expired_cert begin %s" % len(tasks)) logger.debug("transfer expired_cert begin task_ids %s" % [i['_id'] for i in tasks]) dev_id, devs, devs_dict = init_cert_dev(tasks[0]) logger.debug("devs is %s" % devs) logger.debug("cert_devs dev_id %s, devs len %s" % (dev_id, len(devs))) save_tasks = [] for task in tasks: task['dev_id'] = dev_id save_task = {} save_task['_id'] = ObjectId(task['_id']) save_task['status'] = "PROGRESS" save_task['dev_id'] = dev_id save_task['path'] = task.get('path') save_task['username'] = task.get('username') save_task['t_id'] = task.get('t_id') save_task['send_devs'] = task.get('send_dev') save_task['created_time'] = task.get('created_time') save_task['save_name'] = task.get('save_name') save_task['worker_host'] = WORKER_HOST save_tasks.append(save_task) make_result_cache(task['_id'], devs_dict) s1_db.transfer_cert_tasks.insert(save_tasks) worker(tasks, devs) # try: # task_id_list = [] # for task in tasks: # for sav_name in task['save_name'].split(','): # info_cert = s1_db.cert_detail.find_one({'save_name': sav_name}) # task_id_list.append(str(info_cert['_id'])) # status = cert_cms_delete(task_id_list) # if status: # cert_portal_delete(task_id_list) # except Exception,e: # logger.error('callback error %s'%(e.message)) logger.debug("transfer cert trans end") except Exception, e: logger.debug("trasnfer cert trans error:%s " % traceback.format_exc(e))
def send_2_days_to_0_Original_Script(doc_id, email_to_list, email_cc_list, email_bcc_list, email_from): """given the doc_id, send email """ #couchdb settings s = couchdb.Server(settings.COUCH_DSN) db = s['client_docs'] doc = db.get(doc_id) if doc == None: raise Exception('Failed to Send Invoice from send_2_days_to_0', '%s not found' % doc_id) t = Template(file="templates/prepaid_invoice_2_days_before_0.tmpl") #assign variables to the template file conn = engine.connect() task_client_name = send_task('EmailSender.GetAddressTo', [doc['client_id']]) t.client_name = task_client_name.get() t.order_id = doc['order_id'] t.currency = doc['currency'] sql = text("""SELECT sign from currency_lookup WHERE code = :currency """) currency_sign = conn.execute(sql, currency=doc['currency']).fetchone()['sign'] t.currency_sign = currency_sign t.total_amount = locale.format('%0.2f', Decimal(doc['total_amount']), True) t.doc_id = doc['_id'] #get the CSRO task = send_task('EmailSender.GetCsroDetails', [ doc['client_id'], ]) csro = task.get() t.admin_fname = csro.admin_fname t.admin_lname = csro.admin_lname t.signature_notes = csro.signature_notes t.signature_contact_nos = csro.signature_contact_nos t.signature_company = csro.signature_company t.signature_websites = csro.signature_websites #get client timezone for tracking date added sql = text("""SELECT t.timezone FROM leads AS l JOIN timezone_lookup AS t ON l.timezone_id = t.id WHERE l.id = :client_id """) data = conn.execute(sql, client_id=doc['client_id']).fetchone() if data == None: tz_string = 'Australia/Sydney' else: tz_string = data['timezone'] ph_tz = timezone('Asia/Manila') client_tz = timezone(tz_string) a = doc['added_on'] added_on = datetime(a[0], a[1], a[2], a[3], a[4], a[5], tzinfo=ph_tz) added_on_client_tz = added_on.astimezone(client_tz) t.added_on_client_tz = added_on_client_tz.strftime('%B %d, %Y') msg = MIMEMultipart() part1 = MIMEText('%s' % t, 'html', 'UTF-8') part1.add_header('Content-Disposition', 'inline') msg.attach(part1) #attach the invoice html invoice_header_and_items = generate_html_header_and_items(doc) invoice_attachment = MIMEText( '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />%s' % invoice_header_and_items, 'html', 'UTF-8') invoice_attachment.add_header('Content-Disposition', 'attachment', filename='invoice-%s.html' % doc['order_id']) msg.attach(invoice_attachment) #attach the pdf invoice task = send_task('generate_pdf_invoice.generate', [ doc_id, ]) result = task.get() pdf_attachment = MIMEApplication(result) pdf_attachment.add_header('Content-Disposition', 'attachment', filename='invoice-%s.pdf' % doc['order_id']) msg.attach(pdf_attachment) msg['To'] = string.join(email_to_list, ',') recipients = [] for email in email_to_list: recipients.append(email) if email_cc_list != None: if len(email_cc_list) > 0: for email in email_cc_list: recipients.append(email) msg['Cc'] = string.join(email_cc_list, ',') if email_bcc_list != None: if len(email_bcc_list) > 0: for email in email_bcc_list: recipients.append(email) msg['From'] = email_from subject = 'Invoice Payment Follow Up [%s]' % doc['order_id'] if settings.DEBUG or settings.STAGING: subject = 'TEST %s' % subject recipients = [settings.EMAIL_ALERT] msg['Subject'] = subject s = smtplib.SMTP(host=settings.MAILGUN_CONFIG['server'], port=settings.MAILGUN_CONFIG['port']) s.login(settings.MAILGUN_CONFIG['username'], settings.MAILGUN_CONFIG['password']) s.sendmail('*****@*****.**', recipients, msg.as_string()) s.quit() logging.info("Sending email using Mailgun %s" % doc_id) #add history that the document was sent doc_saved = False while doc_saved == False: try: doc = db.get(doc_id) logging.info('updating %s rev %s' % (doc_id, doc['_rev'])) if doc.has_key('history') == False: history = [] else: history = doc['history'] now = get_ph_time() attachment = 'payment-follow-up-%s:%s.html' % ( now.strftime('%F %H:%M:%S'), now.microsecond) history.append( dict( timestamp=now.strftime('%F %H:%M:%S'), changes= 'Email sent To: %s Cc:%s Bcc:%s From:%s. Attached doc %s' % (email_to_list, email_cc_list, email_bcc_list, email_from, attachment), by='task celery prepaid_send_invoice.send_2_days_to_0')) doc['history'] = history doc['mongo_synced'] = False db.save(doc) #attach the sent email db.put_attachment(doc, '%s' % t, attachment) doc_saved = True except couchdb.http.ResourceConflict: send_task('notify_devs.send', [ 'DOCUMENT CONFLICT on send_2_days_to_0', 'Failed to save client_docs %s with rev %s. Retrying...' % (doc_id, doc['_rev']) ]) conn.close() return 'ok'
def send_custom_message_Original_Script(doc_id, rst, email_to_list, email_cc_list, email_bcc_list, html_message, email_from): """given the doc_id, send email """ html_message += """ <pre style="font-size:12px;padding-top:128px;"> ----------------------------------------- Ref doc: %s ----------------------------------------- </pre> """ % doc_id #couchdb settings s = couchdb.Server(settings.COUCH_DSN) db = s['client_docs'] doc = db.get(doc_id) if doc == None: raise Exception('Failed to Send Invoice', '%s not found' % doc_id) msg = MIMEMultipart() part1 = MIMEText('%s' % html_message, 'html', 'UTF-8') part1.add_header('Content-Disposition', 'inline') msg.attach(part1) #attach the invoice html invoice_header_and_items = generate_html_header_and_items(doc) invoice_attachment = MIMEText( '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />%s' % invoice_header_and_items, 'html', 'UTF-8') invoice_attachment.add_header('Content-Disposition', 'attachment', filename='invoice-%s.html' % doc['order_id']) msg.attach(invoice_attachment) #attach the pdf invoice task = send_task('generate_pdf_invoice.generate', [ doc_id, ]) result = task.get() pdf_attachment = MIMEApplication(result) pdf_attachment.add_header('Content-Disposition', 'attachment', filename='invoice-%s.pdf' % doc['order_id']) msg.attach(pdf_attachment) msg['To'] = string.join(email_to_list, ',') recipients = [] for email in email_to_list: recipients.append(email) if email_cc_list != None: if len(email_cc_list) > 0: for email in email_cc_list: recipients.append(email) msg['Cc'] = string.join(email_cc_list, ',') if email_bcc_list != None: if len(email_bcc_list) > 0: for email in email_bcc_list: recipients.append(email) msg['From'] = email_from subject = get_email_subject(doc) if settings.DEBUG or settings.STAGING: subject = 'TEST %s' % subject recipients = [settings.EMAIL_ALERT] msg['Subject'] = subject s = smtplib.SMTP(host=settings.MAILGUN_CONFIG['server'], port=settings.MAILGUN_CONFIG['port']) s.login(settings.MAILGUN_CONFIG['username'], settings.MAILGUN_CONFIG['password']) s.sendmail('*****@*****.**', recipients, msg.as_string()) s.quit() logging.info("Sending email using Mailgun %s" % doc_id) #add history that the document was sent doc = db.get(doc_id) if doc.has_key('history') == False: history = [] else: history = doc['history'] now = get_ph_time() attachment = 'custom-message-%s:%s.html' % (now.strftime('%F %H:%M:%S'), now.microsecond) history.append( dict( timestamp=now.strftime('%F %H:%M:%S'), changes='Email sent To: %s Cc:%s Bcc:%s From:%s. Attached doc %s' % (email_to_list, email_cc_list, email_bcc_list, email_from, attachment), by='task celery prepaid_send_invoice.send_custom_message')) doc['history'] = history doc['sent_flag'] = 'Y' #flag to trackdown newly generated invoice, sending during 5 days before 0 doc['mongo_synced'] = False db.save(doc) #attach the sent email db.put_attachment(doc, '%s' % html_message, attachment) return 'ok'