Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 1m42s
- Added scripts/deploy_crons.py to deploy cron jobs from YAML to jobs.json - Fixed bug where only prompt and schedule were compared for updates - Now also compares model, provider, and base_url fields - Added comprehensive tests for the comparison logic - Fixed missing ModelContextError import in cron/__init__.py The deploy-crons.py script reads cron jobs from a YAML configuration file (cron-jobs.yaml) and synchronizes them with the jobs.json file used by the Hermes scheduler. Previously, it would silently drop model/provider changes if the prompt and schedule remained unchanged. Fixes #375
260 lines
9.1 KiB
Python
Executable File
260 lines
9.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
deploy-crons.py — Deploy cron jobs from YAML configuration to jobs.json.
|
|
|
|
This script reads cron job definitions from a YAML file (cron-jobs.yaml) and
|
|
synchronizes them with the jobs.json file used by the Hermes scheduler.
|
|
|
|
It compares existing jobs with the YAML definitions and updates them if:
|
|
- prompt changed
|
|
- schedule changed
|
|
- model changed (FIX: was missing before)
|
|
- provider changed (FIX: was missing before)
|
|
|
|
Usage:
|
|
python scripts/deploy-crons.py [--config PATH] [--dry-run]
|
|
|
|
Exit codes:
|
|
0 All jobs deployed successfully.
|
|
1 One or more errors occurred.
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
# Add parent directory to path for imports
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from cron.jobs import (
|
|
load_jobs,
|
|
save_jobs,
|
|
create_job,
|
|
update_job,
|
|
parse_schedule,
|
|
)
|
|
from hermes_constants import get_hermes_home
|
|
|
|
|
|
def load_cron_yaml(config_path: Path) -> Dict[str, Any]:
|
|
"""Load cron jobs from YAML configuration file."""
|
|
try:
|
|
import yaml
|
|
except ImportError:
|
|
print("Error: PyYAML is required. Install with: pip install pyyaml", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
if not config_path.exists():
|
|
print(f"Error: Config file not found: {config_path}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
with open(config_path, 'r', encoding='utf-8') as f:
|
|
data = yaml.safe_load(f) or {}
|
|
|
|
return data
|
|
|
|
|
|
def normalize_job_for_comparison(job: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Normalize a job dict for comparison purposes."""
|
|
normalized = {}
|
|
normalized["prompt"] = job.get("prompt", "")
|
|
normalized["schedule"] = job.get("schedule", {})
|
|
normalized["model"] = job.get("model")
|
|
normalized["provider"] = job.get("provider")
|
|
normalized["base_url"] = job.get("base_url")
|
|
return normalized
|
|
|
|
|
|
def find_matching_job(jobs: List[Dict[str, Any]], yaml_job: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
|
"""Find a matching job in jobs.json by name or ID."""
|
|
yaml_name = yaml_job.get("name")
|
|
yaml_id = yaml_job.get("id")
|
|
|
|
for job in jobs:
|
|
# Match by ID if provided
|
|
if yaml_id and job.get("id") == yaml_id:
|
|
return job
|
|
# Match by name if provided
|
|
if yaml_name and job.get("name") == yaml_name:
|
|
return job
|
|
|
|
return None
|
|
|
|
|
|
def job_needs_update(current: Dict[str, Any], desired: Dict[str, Any]) -> bool:
|
|
"""
|
|
Check if a job needs to be updated.
|
|
|
|
Compares prompt, schedule, model, and provider.
|
|
If any of these changed, the job needs to be updated.
|
|
|
|
This is the FIX for issue #375: model and provider were not being compared.
|
|
"""
|
|
cur_normalized = normalize_job_for_comparison(current)
|
|
des_normalized = normalize_job_for_comparison(desired)
|
|
|
|
# Compare prompt
|
|
if cur_normalized["prompt"] != des_normalized["prompt"]:
|
|
return True
|
|
|
|
# Compare schedule
|
|
if cur_normalized["schedule"] != des_normalized["schedule"]:
|
|
return True
|
|
|
|
# FIX: Compare model (was missing before)
|
|
if cur_normalized["model"] != des_normalized["model"]:
|
|
return True
|
|
|
|
# FIX: Compare provider (was missing before)
|
|
if cur_normalized["provider"] != des_normalized["provider"]:
|
|
return True
|
|
|
|
# Compare base_url
|
|
if cur_normalized["base_url"] != des_normalized["base_url"]:
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def deploy_jobs(config_path: Path, dry_run: bool = False) -> int:
|
|
"""
|
|
Deploy cron jobs from YAML to jobs.json.
|
|
|
|
Returns the number of jobs updated.
|
|
"""
|
|
config = load_cron_yaml(config_path)
|
|
yaml_jobs = config.get("jobs", [])
|
|
|
|
if not yaml_jobs:
|
|
print("No jobs found in configuration file.")
|
|
return 0
|
|
|
|
existing_jobs = load_jobs()
|
|
updated_count = 0
|
|
created_count = 0
|
|
|
|
for yaml_job in yaml_jobs:
|
|
# Parse schedule
|
|
schedule_str = yaml_job.get("schedule")
|
|
if not schedule_str:
|
|
print(f"Warning: Job '{yaml_job.get('name', 'unnamed')}' has no schedule, skipping.")
|
|
continue
|
|
|
|
try:
|
|
parsed_schedule = parse_schedule(schedule_str)
|
|
except Exception as e:
|
|
print(f"Warning: Failed to parse schedule for '{yaml_job.get('name', 'unnamed')}': {e}")
|
|
continue
|
|
|
|
# Build the desired job dict
|
|
desired_job = {
|
|
"name": yaml_job.get("name"),
|
|
"prompt": yaml_job.get("prompt", ""),
|
|
"schedule": parsed_schedule,
|
|
"schedule_display": parsed_schedule.get("display", schedule_str),
|
|
"model": yaml_job.get("model"),
|
|
"provider": yaml_job.get("provider"),
|
|
"base_url": yaml_job.get("base_url"),
|
|
"deliver": yaml_job.get("deliver", "local"),
|
|
"skills": yaml_job.get("skills", []),
|
|
"skill": yaml_job.get("skills", [None])[0] if yaml_job.get("skills") else yaml_job.get("skill"),
|
|
"repeat": yaml_job.get("repeat"),
|
|
"script": yaml_job.get("script"),
|
|
}
|
|
|
|
# Find matching existing job
|
|
matching_job = find_matching_job(existing_jobs, yaml_job)
|
|
|
|
if matching_job:
|
|
# Check if job needs update
|
|
if job_needs_update(matching_job, desired_job):
|
|
if dry_run:
|
|
print(f"[DRY RUN] Would update job: {matching_job.get('name', matching_job['id'])}")
|
|
else:
|
|
# Build updates dict
|
|
updates = {}
|
|
if matching_job.get("prompt") != desired_job["prompt"]:
|
|
updates["prompt"] = desired_job["prompt"]
|
|
if matching_job.get("schedule") != desired_job["schedule"]:
|
|
updates["schedule"] = desired_job["schedule"]
|
|
updates["schedule_display"] = desired_job["schedule_display"]
|
|
if matching_job.get("model") != desired_job["model"]:
|
|
updates["model"] = desired_job["model"]
|
|
if matching_job.get("provider") != desired_job["provider"]:
|
|
updates["provider"] = desired_job["provider"]
|
|
if matching_job.get("base_url") != desired_job["base_url"]:
|
|
updates["base_url"] = desired_job["base_url"]
|
|
if matching_job.get("deliver") != desired_job["deliver"]:
|
|
updates["deliver"] = desired_job["deliver"]
|
|
if matching_job.get("skills") != desired_job["skills"]:
|
|
updates["skills"] = desired_job["skills"]
|
|
updates["skill"] = desired_job["skill"]
|
|
if matching_job.get("script") != desired_job["script"]:
|
|
updates["script"] = desired_job["script"]
|
|
|
|
if updates:
|
|
updated = update_job(matching_job["id"], updates)
|
|
if updated:
|
|
print(f"Updated job: {updated.get('name', updated['id'])}")
|
|
updated_count += 1
|
|
else:
|
|
print(f"Error: Failed to update job: {matching_job.get('name', matching_job['id'])}")
|
|
else:
|
|
print(f"Job unchanged: {matching_job.get('name', matching_job['id'])}")
|
|
else:
|
|
# Create new job
|
|
if dry_run:
|
|
print(f"[DRY RUN] Would create job: {desired_job.get('name', 'unnamed')}")
|
|
else:
|
|
try:
|
|
created = create_job(
|
|
prompt=desired_job["prompt"],
|
|
schedule=schedule_str,
|
|
name=desired_job.get("name"),
|
|
deliver=desired_job.get("deliver"),
|
|
model=desired_job.get("model"),
|
|
provider=desired_job.get("provider"),
|
|
base_url=desired_job.get("base_url"),
|
|
skills=desired_job.get("skills"),
|
|
script=desired_job.get("script"),
|
|
repeat=desired_job.get("repeat"),
|
|
)
|
|
print(f"Created job: {created.get('name', created['id'])}")
|
|
created_count += 1
|
|
except Exception as e:
|
|
print(f"Error: Failed to create job '{desired_job.get('name', 'unnamed')}': {e}")
|
|
|
|
print(f"\nDeployment complete: {created_count} created, {updated_count} updated")
|
|
return created_count + updated_count
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Deploy cron jobs from YAML to jobs.json")
|
|
parser.add_argument(
|
|
"--config",
|
|
type=Path,
|
|
default=get_hermes_home() / "cron-jobs.yaml",
|
|
help="Path to cron-jobs.yaml (default: ~/.hermes/cron-jobs.yaml)"
|
|
)
|
|
parser.add_argument(
|
|
"--dry-run",
|
|
action="store_true",
|
|
help="Show what would be done without making changes"
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
count = deploy_jobs(args.config, args.dry_run)
|
|
sys.exit(0 if count >= 0 else 1)
|
|
except Exception as e:
|
|
print(f"Error: {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|