Skip to content

Commit e85529a

Browse files
committed
refactor: auto-import command modules with 'perform' check
- Automatically import all modules in the commands directory and validate that each has a 'perform' method. - Enhance logging for better traceability. Generated-by: aiautocommit
1 parent 3fcab8f commit e85529a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

app/commands/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
11
"""
22
Commands are distinct from jobs: jobs are designed to be run by celery, commands can be run my jobs or by a user.
3+
4+
Auto-imports all modules in the commands directory and subdirectories and validates that each module has a 'perform' method.
35
"""
6+
7+
import importlib
8+
from pathlib import Path
9+
10+
from app import log
11+
12+
commands_path = Path(__file__).parent
13+
14+
for py_file in commands_path.rglob("*.py"):
15+
if py_file.name in ["__init__.py", "__main__.py"]:
16+
continue
17+
18+
rel_path = py_file.relative_to(commands_path)
19+
module_parts = rel_path.with_suffix("").parts
20+
module_name = f"{__package__}." + ".".join(module_parts)
21+
22+
log.debug("auto importing", command=module_name)
23+
24+
try:
25+
module = importlib.import_module(module_name)
26+
27+
# Validate that the module has a 'perform' method
28+
if not hasattr(module, "perform") or not callable(getattr(module, "perform")):
29+
log.error(f"Module {module_name} doesn't have a 'perform' method")
30+
31+
except Exception as e:
32+
log.error(f"Error importing module {module_name}: {str(e)}")

0 commit comments

Comments
 (0)