Skip to content

Commit

Permalink
move func to cron handlers file
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetSaroha committed Jan 26, 2025
1 parent fedd805 commit 6425f8d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 86 deletions.
87 changes: 1 addition & 86 deletions src/makim/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
from makim import __version__
from makim.cli.auto_generator import (
create_dynamic_command,
create_dynamic_command_cron,
suggest_command,
)
from makim.cli.config import CLI_ROOT_FLAGS_VALUES_COUNT, extract_root_config
from makim.cli.cron_handlers import (
_handle_cron_list,
_handle_cron_start,
_handle_cron_stop,
_handle_cron_commands,
)
from makim.core import Makim

Expand Down Expand Up @@ -101,71 +98,6 @@ def _get_command_from_cli() -> str:
return command


def _handle_cron_commands(makim_instance: Makim) -> typer.Typer:
"""Create and handle cron-related commands.
Returns
-------
typer.Typer: The cron command group with all subcommands.
"""
typer_cron = typer.Typer(
help='Tasks Scheduler',
invoke_without_command=True,
)

if 'scheduler' in makim_instance.global_data:
for schedule_name, schedule_params in makim_instance.global_data.get(
'scheduler', {}
).items():
create_dynamic_command_cron(
makim_instance,
typer_cron,
schedule_name,
schedule_params or {},
)

@typer_cron.command(help='List all scheduled tasks')
def list() -> None:
"""List tasks defined in .makim.yaml and their current status."""
_handle_cron_list(makim_instance)

@typer_cron.command(help='Start a scheduler by its name')
def start(
name: str = typer.Argument(
None,
help="""Name of the scheduler to start.
Use '--all' for all schedulers""",
),
all: bool = typer.Option(
False,
'--all',
help='Start all available schedulers',
is_flag=True,
),
) -> None:
"""Start (enable) a scheduled task."""
_handle_cron_start(makim_instance, name, all)

@typer_cron.command(help='Stop a scheduler by its name')
def stop(
name: str = typer.Argument(
None,
help="""Name of the scheduler to stop.
Use '--all' for all schedulers""",
),
all: bool = typer.Option(
False,
'--all',
help='Stop all running schedulers',
is_flag=True,
),
) -> None:
"""Stop (disable) scheduled task(s)."""
_handle_cron_stop(makim_instance, name, all)

return typer_cron


def run_app() -> None:
"""Run the Typer app."""
root_config = extract_root_config()
Expand All @@ -192,23 +124,6 @@ def run_app() -> None:
for task_name, task_data in group_data.get('tasks', {}).items():
tasks[f'{group_name}.{task_name}'] = task_data

# Add dynamically cron commands to Typer app
if 'scheduler' in makim.global_data:
typer_cron = typer.Typer(
help='Tasks Scheduler',
invoke_without_command=True,
)

for schedule_name, schedule_params in makim.global_data.get(
'scheduler', {}
).items():
create_dynamic_command_cron(
makim, typer_cron, schedule_name, schedule_params or {}
)

# Add cron command
app.add_typer(typer_cron, name='cron', rich_help_panel='Extensions')

# Add dynamically commands to Typer app
# Add cron commands if scheduler is configured
typer_cron = _handle_cron_commands(makim)
Expand Down
66 changes: 66 additions & 0 deletions src/makim/cli/cron_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rich.console import Console
from rich.table import Table

from makim.cli.auto_generator import create_dynamic_command_cron
from makim.core import Makim


Expand All @@ -21,6 +22,71 @@ def _create_cron_table() -> Table:
return table


def _handle_cron_commands(makim_instance: Makim) -> typer.Typer:
"""Create and handle cron-related commands.
Returns
-------
typer.Typer: The cron command group with all subcommands.
"""
typer_cron = typer.Typer(
help='Tasks Scheduler',
invoke_without_command=True,
)

if 'scheduler' in makim_instance.global_data:
for schedule_name, schedule_params in makim_instance.global_data.get(
'scheduler', {}
).items():
create_dynamic_command_cron(
makim_instance,
typer_cron,
schedule_name,
schedule_params or {},
)

@typer_cron.command(help='List all scheduled tasks')
def list() -> None:
"""List tasks defined in .makim.yaml and their current status."""
_handle_cron_list(makim_instance)

@typer_cron.command(help='Start a scheduler by its name')
def start(
name: str = typer.Argument(
None,
help="""Name of the scheduler to start.
Use '--all' for all schedulers""",
),
all: bool = typer.Option(
False,
'--all',
help='Start all available schedulers',
is_flag=True,
),
) -> None:
"""Start (enable) a scheduled task."""
_handle_cron_start(makim_instance, name, all)

@typer_cron.command(help='Stop a scheduler by its name')
def stop(
name: str = typer.Argument(
None,
help="""Name of the scheduler to stop.
Use '--all' for all schedulers""",
),
all: bool = typer.Option(
False,
'--all',
help='Stop all running schedulers',
is_flag=True,
),
) -> None:
"""Stop (disable) scheduled task(s)."""
_handle_cron_stop(makim_instance, name, all)

return typer_cron


def _handle_cron_list(makim_instance: Makim) -> None:
"""Handle the cron list command."""
scheduled_tasks = makim_instance.global_data.get('scheduler', {})
Expand Down

0 comments on commit 6425f8d

Please sign in to comment.