From 4738a1422da05e714f201368a90f936a0d743ee2 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 10 Feb 2023 14:39:16 +0530 Subject: [PATCH] fix: Add format, verbose options to scheduler --- frappe/commands/scheduler.py | 17 ++++++++++++++--- frappe/utils/scheduler.py | 10 ++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/frappe/commands/scheduler.py b/frappe/commands/scheduler.py index 7ced1e4d11..6365ecdd26 100755 --- a/frappe/commands/scheduler.py +++ b/frappe/commands/scheduler.py @@ -75,8 +75,12 @@ def disable_scheduler(context): @click.command("scheduler") @click.option("--site", help="site name") @click.argument("state", type=click.Choice(["pause", "resume", "disable", "enable", "status"])) +@click.option( + "--format", "-f", default="text", type=click.Choice(["json", "text"]), help="Output format" +) +@click.option("--verbose", "-v", is_flag=True, help="Verbose output") @pass_context -def scheduler(context, state: str, site: str | None = None): +def scheduler(context, state: str, format: str, verbose: bool = False, site: str | None = None): """Control scheduler state.""" import frappe import frappe.utils.scheduler @@ -84,11 +88,18 @@ def scheduler(context, state: str, site: str | None = None): site = site or get_site(context) + output = { + "text": "Scheduler is {status} for site {site}", + "json": '{{"status": "{status}", "site": "{site}"}}', + } + with frappe.init_site(site=site): match state: case "status": frappe.connect() - status = "disabled" if frappe.utils.scheduler.is_scheduler_inactive(verbose=verbose) else "enabled" + status = ( + "disabled" if frappe.utils.scheduler.is_scheduler_inactive(verbose=verbose) else "enabled" + ) return print(output[format].format(status=status, site=site)) case "pause" | "resume": update_site_config("pause_scheduler", state == "pause") @@ -97,7 +108,7 @@ def scheduler(context, state: str, site: str | None = None): frappe.utils.scheduler.toggle_scheduler(state == "enable") frappe.db.commit() - print(f"Scheduler {state}d for site {site}") + print(output[format].format(status=f"{state}d", site=site)) @click.command("set-maintenance-mode") diff --git a/frappe/utils/scheduler.py b/frappe/utils/scheduler.py index 146a7fa244..8cda71ee9a 100755 --- a/frappe/utils/scheduler.py +++ b/frappe/utils/scheduler.py @@ -92,16 +92,18 @@ def enqueue_events(site: str) -> list[str] | None: return enqueued_jobs -def is_scheduler_inactive() -> bool: +def is_scheduler_inactive(verbose=True) -> bool: if frappe.local.conf.maintenance_mode: - cprint(f"{frappe.local.site}: Maintenance mode is ON") + if verbose: + cprint(f"{frappe.local.site}: Maintenance mode is ON") return True if frappe.local.conf.pause_scheduler: - cprint(f"{frappe.local.site}: frappe.conf.pause_scheduler is SET") + if verbose: + cprint(f"{frappe.local.site}: frappe.conf.pause_scheduler is SET") return True - if is_scheduler_disabled(): + if is_scheduler_disabled(verbose=verbose): return True return False