Package layers models, config, storage, jobs, web, cli, held by tests/test_architecture.py with desk and pipeline as external trees and plugins/ limited to desk. roles.yaml is the default config, roledesk.db its own file, port 8790. create_app mounts pipeline.web.connect over Jobs(config.database); `roledesk job pull` drains a no-op pull and `roledesk db init` creates the file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
"""`roledesk serve | job pull | db init`."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import logging
|
|
import sys
|
|
|
|
from roledesk import config as config_mod
|
|
|
|
|
|
def build_parser() -> argparse.ArgumentParser:
|
|
parser = argparse.ArgumentParser(prog="roledesk",
|
|
description="A job-search engine on desk.")
|
|
parser.add_argument("-c", "--config", default=None, help="path to roles.yaml")
|
|
parser.add_argument("-v", "--verbose", action="store_true")
|
|
sub = parser.add_subparsers(dest="command", required=True)
|
|
|
|
serve = sub.add_parser("serve", help="run the web app")
|
|
serve.add_argument("--host", default="127.0.0.1")
|
|
serve.add_argument("--port", type=int, default=8790)
|
|
|
|
job = sub.add_parser("job", help="queue a job and drain the queue once")
|
|
job.add_argument("kind", choices=["pull"])
|
|
|
|
db = sub.add_parser("db", help="database housekeeping")
|
|
db.add_argument("action", choices=["init"])
|
|
return parser
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
args = build_parser().parse_args(argv)
|
|
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO,
|
|
format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
|
try:
|
|
if args.command == "serve":
|
|
from roledesk.web import serve
|
|
config_mod.load(args.config) # a config error here, not in uvicorn
|
|
return serve(args.host, args.port, args.config)
|
|
if args.command == "job":
|
|
return run_job(args)
|
|
return init_db(args)
|
|
except FileNotFoundError as exc:
|
|
print(exc, file=sys.stderr)
|
|
return 1
|
|
|
|
|
|
def run_job(args: argparse.Namespace) -> int:
|
|
"""Queue `kind`, then run everything queued until the queue is idle."""
|
|
from pipeline import JobQueue, Jobs
|
|
from roledesk import jobs
|
|
from roledesk.storage import Store
|
|
|
|
config = config_mod.load(args.config)
|
|
store = Store(config.database)
|
|
queue = JobQueue(Jobs(config.database), config, store,
|
|
workers=config.workers.model_dump(),
|
|
bootstrap=jobs.BOOTSTRAP, bootstrap_arg=config.path)
|
|
queued = queue.enqueue(args.kind)
|
|
queue.drain()
|
|
if queued is not None:
|
|
done = queue.jobs.get(queued.id)
|
|
if done is not None:
|
|
print(json.dumps({"id": done.id, "state": done.state, "result": done.result,
|
|
"error": done.error}))
|
|
return 0 if done.state == "done" else 1
|
|
return 0
|
|
|
|
|
|
def init_db(args: argparse.Namespace) -> int:
|
|
"""Create the file, or bring it to the current schema."""
|
|
from roledesk.storage import LATEST, Store
|
|
|
|
config = config_mod.load(args.config)
|
|
store = Store(config.database)
|
|
applied = store.migrations_applied
|
|
print(f"{store.path}: schema v{LATEST}"
|
|
+ (f", applied {applied}" if applied else ""))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|