def run_async_task(self, site=None, user=None, cmd=None, form_dict=None, hijack_std=False): ret = {} frappe.init(site) frappe.connect() frappe.local.task_id = self.request.id if hijack_std: original_stdout, original_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = get_std_streams(self.request.id) frappe.local.stdout, frappe.local.stderr = sys.stdout, sys.stderr try: set_task_status(self.request.id, "Running") frappe.db.commit() frappe.set_user(user) # sleep(60) frappe.local.form_dict = frappe._dict(form_dict) execute_cmd(cmd, from_async=True) ret = frappe.local.response except Exception, e: frappe.db.rollback() ret = frappe.local.response http_status_code = getattr(e, "http_status_code", 500) ret['status_code'] = http_status_code frappe.errprint(frappe.get_traceback()) frappe.utils.response.make_logs() set_task_status(self.request.id, "Error", response=ret) task_logger.error('Exception in running {}: {}'.format(cmd, ret['exc']))
def send_newsletter(site, newsletter, event): # hack! pass event="bulk_long" to queue in longjob queue try: frappe.connect(site=site) doc = frappe.get_doc("Newsletter", newsletter) doc.send_bulk() except: frappe.db.rollback() task_logger.error(site) task_logger.error(frappe.get_traceback()) # wasn't able to send emails :( doc.db_set("email_sent", 0) frappe.db.commit() log("send_newsletter") raise else: frappe.db.commit() finally: frappe.destroy()
def run_async_task(self, site, user, cmd, form_dict): ret = {} frappe.init(site) frappe.connect() sys.stdout, sys.stderr = get_std_streams(self.request.id) frappe.local.stdout, frappe.local.stderr = sys.stdout, sys.stderr frappe.local.task_id = self.request.id frappe.cache() try: set_task_status(self.request.id, "Running") frappe.db.commit() frappe.set_user(user) # sleep(60) frappe.local.form_dict = frappe._dict(form_dict) execute_cmd(cmd, async=True) ret = frappe.local.response except Exception, e: frappe.db.rollback() if not frappe.flags.in_test: frappe.db.commit() ret = frappe.local.response http_status_code = getattr(e, "http_status_code", 500) ret['status_code'] = http_status_code frappe.errprint(frappe.get_traceback()) frappe.utils.response.make_logs() set_task_status(self.request.id, "Failed", response=ret) task_logger.error('Exception in running {}: {}'.format( cmd, ret['exc']))
def run_async_task(self, site, user, cmd, form_dict): ret = {} frappe.init(site) frappe.connect() sys.stdout, sys.stderr = get_std_streams(self.request.id) frappe.local.stdout, frappe.local.stderr = sys.stdout, sys.stderr frappe.local.task_id = self.request.id frappe.cache() try: set_task_status(self.request.id, "Running") frappe.db.commit() frappe.set_user(user) # sleep(60) frappe.local.form_dict = frappe._dict(form_dict) execute_cmd(cmd, from_async=True) ret = frappe.local.response except Exception, e: frappe.db.rollback() if not frappe.flags.in_test: frappe.db.commit() ret = frappe.local.response http_status_code = getattr(e, "http_status_code", 500) ret['status_code'] = http_status_code frappe.errprint(frappe.get_traceback()) frappe.utils.response.make_logs() set_task_status(self.request.id, "Failed", response=ret) task_logger.error('Exception in running {}: {}'.format(cmd, ret['exc']))
def enqueue_events_for_site(site): try: frappe.init(site=site) if frappe.local.conf.maintenance_mode: return frappe.connect(site=site) enqueue_events(site) except: task_logger.error('Exception in Enqueue Events for Site {0}'.format(site)) raise finally: frappe.destroy()
def sendmail(site, communication_name, print_html=None, print_format=None, attachments=None, recipients=None, cc=None, lang=None, session=None,read_receipt=None): try: frappe.connect(site=site) if lang: frappe.local.lang = lang if session: # hack to enable access to private files in PDF session['data'] = frappe._dict(session['data']) frappe.local.session.update(session) # upto 3 retries for i in xrange(3): try: communication = frappe.get_doc("Communication", communication_name) if communication.sent_or_received == "Received": communication.message_id = None communication._notify(print_html=print_html, print_format=print_format, attachments=attachments, recipients=recipients, cc=cc) except MySQLdb.OperationalError, e: # deadlock, try again if e.args[0]==1213: frappe.db.rollback() time.sleep(1) continue else: raise else: break except: traceback = log("frappe.tasks.sendmail", frappe.as_json({ "site": site, "communication_name": communication_name, "print_html": print_html, "print_format": print_format, "attachments": attachments, "recipients": recipients, "cc": cc, "lang": lang })) task_logger.error(traceback) raise else: frappe.db.commit() finally: frappe.destroy()
def sendmail(site, communication_name, print_html=None, print_format=None, attachments=None, recipients=None, cc=None, lang=None, session=None): try: frappe.connect(site=site) if lang: frappe.local.lang = lang if session: # hack to enable access to private files in PDF session['data'] = frappe._dict(session['data']) frappe.local.session.update(session) # upto 3 retries for i in xrange(3): try: communication = frappe.get_doc("Communication", communication_name) communication._notify(print_html=print_html, print_format=print_format, attachments=attachments, recipients=recipients, cc=cc) except MySQLdb.OperationalError, e: # deadlock, try again if e.args[0]==1213: frappe.db.rollback() time.sleep(1) continue else: raise else: break except: traceback = log("frappe.tasks.sendmail", frappe.as_json({ "site": site, "communication_name": communication_name, "print_html": print_html, "print_format": print_format, "attachments": attachments, "recipients": recipients, "cc": cc, "lang": lang })) task_logger.error(traceback) raise else: frappe.db.commit() finally: frappe.destroy()
def enqueue_events_for_site(site): print dir(enqueue_events_for_site) try: site = "omnitech.gulfcloudservices.com" if site in get_sites(): frappe.init(site=site) if frappe.local.conf.maintenance_mode: return frappe.connect(site=site) enqueue_events(site) except: task_logger.error('Exception in Enqueue Events for Site {0}'.format(site)) raise finally: frappe.destroy()
def enqueue_events_for_site(site): print dir(enqueue_events_for_site) try: site = "omnitech.gulfcloudservices.com" if site in get_sites(): frappe.init(site=site) if frappe.local.conf.maintenance_mode: return frappe.connect(site=site) enqueue_events(site) except: task_logger.error( 'Exception in Enqueue Events for Site {0}'.format(site)) raise finally: frappe.destroy()