Skip to content

fix(cron): import run_job in _run_cron_tracked to fix NameError - #1312

Closed
fecolinhares wants to merge 1 commit into
nesquena:masterfrom
fecolinhares:fix-1310-cron-runjob-import
Closed

fecolinhares wants to merge 1 commit into
nesquena:masterfrom
fecolinhares:fix-1310-cron-runjob-import

Conversation

@fecolinhares

Copy link
Copy Markdown
Contributor

Thinking Path

  • Hermes WebUI allows users to manually trigger cron jobs from the Tasks panel
  • Manual execution calls _run_cron_tracked() in api/routes.py
  • This function wraps cron.scheduler.run_job() to track running state
  • The function was calling run_job(job) without importing it first
  • This caused NameError: name 'run_job' is not defined when clicking "Run Now" on any cron job
  • The fix is a single-line import at the start of the function

What Changed

Added one import statement inside _run_cron_tracked():

def _run_cron_tracked(job):
    """Wrapper that tracks running state around cron.scheduler.run_job."""
    from cron.scheduler import run_job  # ← Added this line
    
    try:
        run_job(job)
    finally:
        _mark_cron_done(job.get("id", ""))

Files modified:

  • api/routes.py (+2 lines: import + blank line)

Why It Matters

  • User impact: Manual cron execution was completely broken — clicking "Run Now" crashed the background worker
  • Scope: Affects all WebUI users who schedule cron jobs and want to test/run them manually
  • Severity: High — feature advertised in UI but non-functional

Verification

  • ✅ Python compilation: python -m py_compile api/routes.py — passes
  • ✅ Existing cron tests: pytest tests/ -k cron — 45 passed, 9 skipped (require running agent)
  • ✅ No other occurrences of run_job without import in the file (searched entire routes.py)

Risks / Follow-ups

  • Risk: None — this is a straightforward missing import with no side effects
  • Follow-up: Could add a test that mocks a cron job execution to catch similar issues in CI

Model Used

  • Provider: Opencode Go
  • Model: qwen3.6-plus

AI Usage Disclosure

  • AI-assisted: Yes
  • Provider: Opencode Go
  • Model: qwen3.6-plus
  • Human review: Full code and PR text reviewed before submission

Fixes #1310

Manual cron job execution from the WebUI crashed with NameError because
run_job was called without being imported in the function scope.

This adds the missing import at the start of _run_cron_tracked(), allowing
manual cron runs to execute correctly.

Fixes nesquena#1310
@nesquena-hermes

Copy link
Copy Markdown
Collaborator

Confirmed the diff and the matching bug surface in master:

  • api/routes.py line 95-100: _run_cron_tracked() calls run_job(job) but the only from cron.scheduler import run_job in the file is inside an unrelated function at line ~3503 (the manual-run handler), so the threaded wrapper raises NameError exactly when cron worker hits it.
  • This PR's two-line addition (from cron.scheduler import run_job at function entry) is the minimal correct fix — function-local import keeps the existing module-level import structure intact and matches the lazy-import pattern already used at line ~3503.
  • The diff is +2 / -0, no logic change, no API surface change, no test change required (existing cron tests still pass per the PR description).
  • Reporter of Manual cron run crashes with NameError: run_job is not defined #1310 (ynott) already validated the diff matches their local workaround.

Looks safe to merge. Closes #1310.

🤖 Automated triage via nesquena-hermes

jasonjcwu pushed a commit to jasonjcwu/hermes-webui that referenced this pull request Apr 30, 2026
…esquena#1312)

_run_cron_tracked() runs inside a worker thread (threading.Thread), so it
cannot see the caller's local import of run_job from _handle_cron_run.
Moved the import into _run_cron_tracked and removed the now-redundant
import from the route handler.

Fixes nesquena#1312, Fixes nesquena#1310
@nesquena-hermes

Copy link
Copy Markdown
Collaborator

Thanks for jumping on #1310 so quickly, @fecolinhares! 🙏

Closing this in favor of #1317 which lands the same one-line fix plus AST-based regression tests in tests/test_cron_run_job_import.py that pin the worker-thread scope so a future refactor can't silently re-break this. The test file is what tipped the scales — both fixes are otherwise identical.

The fix from #1317 is going out as part of release v0.50.245, with credit also pointing back to #1312 and #1329 in the changelog as parallel discoveries.

Really appreciate you investigating the worker-thread scope angle in your write-up — it matched the actual root cause exactly. Looking forward to the next one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Manual cron run crashes with NameError: run_job is not defined

2 participants