Skip to content

Commit

Permalink
refactor: use util method for checking if job is queued
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush committed Sep 12, 2022
1 parent 6b94b53 commit 12d99b3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ def start_import(self):
if "Bank Account" not in json.dumps(preview["columns"]):
frappe.throw(_("Please add the Bank Account column"))

from frappe.core.page.background_jobs.background_jobs import get_info
from frappe.utils.background_jobs import is_job_queued
from frappe.utils.scheduler import is_scheduler_inactive

if is_scheduler_inactive() and not frappe.flags.in_test:
frappe.throw(_("Scheduler is inactive. Cannot import data."), title=_("Scheduler Inactive"))

enqueued_jobs = [d.get("job_name") for d in get_info()]

if self.name not in enqueued_jobs:
if not is_job_queued(self.name):
enqueue(
start_import,
queue="default",
Expand Down
6 changes: 2 additions & 4 deletions erpnext/accounts/doctype/ledger_merge/ledger_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils.background_jobs import is_job_queued

from erpnext.accounts.doctype.account.account import merge_account


class LedgerMerge(Document):
def start_merge(self):
from frappe.core.page.background_jobs.background_jobs import get_info
from frappe.utils.background_jobs import enqueue
from frappe.utils.scheduler import is_scheduler_inactive

if is_scheduler_inactive() and not frappe.flags.in_test:
frappe.throw(_("Scheduler is inactive. Cannot merge accounts."), title=_("Scheduler Inactive"))

enqueued_jobs = [d.get("job_name") for d in get_info()]

if self.name not in enqueued_jobs:
if not is_job_queued(self.name):
enqueue(
start_merge,
queue="default",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from frappe import _, scrub
from frappe.model.document import Document
from frappe.utils import flt, nowdate
from frappe.utils.background_jobs import enqueue
from frappe.utils.background_jobs import enqueue, is_job_queued

from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
get_accounting_dimensions,
Expand Down Expand Up @@ -207,14 +207,12 @@ def make_invoices(self):
if len(invoices) < 50:
return start_import(invoices)
else:
from frappe.core.page.background_jobs.background_jobs import get_info
from frappe.utils.scheduler import is_scheduler_inactive

if is_scheduler_inactive() and not frappe.flags.in_test:
frappe.throw(_("Scheduler is inactive. Cannot import data."), title=_("Scheduler Inactive"))

enqueued_jobs = [d.get("job_name") for d in get_info()]
if self.name not in enqueued_jobs:
if not is_job_queued(self.name):
enqueue(
start_import,
queue="default",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

import frappe
from frappe import _
from frappe.core.page.background_jobs.background_jobs import get_info
from frappe.model.document import Document
from frappe.model.mapper import map_child_doc, map_doc
from frappe.utils import cint, flt, get_time, getdate, nowdate, nowtime
from frappe.utils.background_jobs import enqueue
from frappe.utils.background_jobs import enqueue, is_job_queued
from frappe.utils.scheduler import is_scheduler_inactive


Expand Down Expand Up @@ -467,7 +466,7 @@ def enqueue_job(job, **kwargs):
closing_entry = kwargs.get("closing_entry") or {}

job_name = closing_entry.get("name")
if not job_already_enqueued(job_name):
if not is_job_queued(job_name):
enqueue(
job,
**kwargs,
Expand All @@ -491,12 +490,6 @@ def check_scheduler_status():
frappe.throw(_("Scheduler is inactive. Cannot enqueue job."), title=_("Scheduler Inactive"))


def job_already_enqueued(job_name):
enqueued_jobs = [d.get("job_name") for d in get_info()]
if job_name in enqueued_jobs:
return True


def safe_load_json(message):
try:
json_message = json.loads(message).get("message")
Expand Down

0 comments on commit 12d99b3

Please sign in to comment.