From 780df0be60bbf13bedf17ba35ae793e1e24dddc7 Mon Sep 17 00:00:00 2001 From: Makarand Bauskar Date: Thu, 28 Sep 2017 12:14:00 +0530 Subject: [PATCH] [hotfix] pull emails from all incoming email accounts instead of enqueue single email account (#4199) --- .../doctype/email_account/email_account.py | 39 ++++++++++++------- frappe/email/smtp.py | 8 +++- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index 307e2e46ff..a36d461c60 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -685,27 +685,36 @@ def pull(now=False): frappe.cache().set_value("workers:no-internet", False) else: return + queued_jobs = get_jobs(site=frappe.local.site, key='job_name')[frappe.local.site] - for email_account in frappe.get_list("Email Account", - filters={"enable_incoming": 1, "awaiting_password": 0}): - if now: - pull_from_email_account(email_account.name) + email_accounts = frappe.db.sql_list("""select name from `tabEmail Account` where + enable_incoming=1 and awaiting_password=0""") - else: - # job_name is used to prevent duplicates in queue - job_name = 'pull_from_email_account|{0}'.format(email_account.name) + # No incoming email account available + if not email_accounts: + return - if job_name not in queued_jobs: - enqueue(pull_from_email_account, 'short', event='all', job_name=job_name, - email_account=email_account.name) + if now: + pull_from_email_accounts(email_accounts) + else: + # job_name is used to prevent duplicates in queue + job_name = 'pull_from_email_accounts|{0}'.format(",".join(email_accounts)) -def pull_from_email_account(email_account): + if job_name not in queued_jobs: + enqueue(pull_from_email_accounts, 'short', event='all', job_name=job_name, + email_accounts=email_accounts) + +def pull_from_email_accounts(email_accounts): '''Runs within a worker process''' - email_account = frappe.get_doc("Email Account", email_account) - email_account.receive() + if not email_accounts: + return - # mark Email Flag Queue mail as read - email_account.mark_emails_as_read_unread() + for email_account in email_accounts: + email_account = frappe.get_doc("Email Account", email_account) + email_account.receive() + + # mark Email Flag Queue mail as read + email_account.mark_emails_as_read_unread() def get_max_email_uid(email_account): # get maximum uid of emails diff --git a/frappe/email/smtp.py b/frappe/email/smtp.py index 1c6b2d89df..62d0dce95b 100644 --- a/frappe/email/smtp.py +++ b/frappe/email/smtp.py @@ -47,7 +47,9 @@ def get_outgoing_email_account(raise_exception_not_set=True, append_to=None, sen if not getattr(frappe.local, "outgoing_email_account", None): frappe.local.outgoing_email_account = {} - if not frappe.local.outgoing_email_account.get(append_to or sender_email_id or "default"): + if not frappe.local.outgoing_email_account.get(append_to) \ + or frappe.local.outgoing_email_account.get(sender_email_id) \ + or frappe.local.outgoing_email_account.get("default"): email_account = None if append_to: @@ -77,7 +79,9 @@ def get_outgoing_email_account(raise_exception_not_set=True, append_to=None, sen frappe.local.outgoing_email_account[append_to or sender_email_id or "default"] = email_account - return frappe.local.outgoing_email_account[append_to or sender_email_id or "default"] + return frappe.local.outgoing_email_account.get(append_to) \ + or frappe.local.outgoing_email_account.get(sender_email_id) \ + or frappe.local.outgoing_email_account.get("default") def get_default_outgoing_email_account(raise_exception_not_set=True): '''conf should be like: