Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
chore(temporal): retire APScheduler legacy cron path (Wave 15.2)
trohitgeda3a208176d
diff --git a/server/main.py b/server/main.pyindex 567beaca..bdc4158c 100644--- a/server/main.py+++ b/server/main.py@@ -66,9 +66,6 @@ _startup_log("All imports complete")# Suppress noisy loggersimport logging-logging.getLogger("apscheduler").setLevel(logging.WARNING)-logging.getLogger("apscheduler.scheduler").setLevel(logging.WARNING)-logging.getLogger("apscheduler.executors").setLevel(logging.WARNING)logging.getLogger("uvicorn").setLevel(logging.WARNING)logging.getLogger("uvicorn.access").setLevel(logging.WARNING)logging.getLogger("uvicorn.error").setLevel(logging.WARNING)@@ -181,11 +178,6 @@ async def lifespan(app: FastAPI):event_waiter.set_cache_service(container.cache())- # Start APScheduler for cron jobs- from services.scheduler import start_scheduler, shutdown_scheduler-- start_scheduler()-# Initialize execution engine recovery sweeperfrom services.execution import (ExecutionCache,@@ -480,8 +472,6 @@ async def lifespan(app: FastAPI):await recovery_sweeper.stop()logger.info("Execution recovery sweeper stopped")- shutdown_scheduler() # Stop APScheduler-# Exit the CLI-agent MCP server's lifespancli_mcp_lifespan_ctx = getattr(app.state, "cli_mcp_lifespan_ctx", None)if cli_mcp_lifespan_ctx is not None:diff --git a/server/pyproject.toml b/server/pyproject.tomlindex e58eaa0b..22d6f581 100644--- a/server/pyproject.toml+++ b/server/pyproject.toml@@ -81,7 +81,6 @@ dependencies = [# Utils"python-dotenv>=1.0.0",- "apscheduler>=3.10.0","qrcode[pil]>=8.0","pytz>=2024.1","psutil>=6.0.0",diff --git a/server/requirements.txt b/server/requirements.txtindex 91d8ed18..72d3b200 100644--- a/server/requirements.txt+++ b/server/requirements.txt@@ -35,8 +35,6 @@ apify-client==2.5.0# via machinaos-serverapify-shared==2.2.0# via apify-client-apscheduler==3.11.2- # via machinaos-serverasync-timeout==5.0.1# via crawleeattrs==26.1.0@@ -544,10 +542,6 @@ typing-inspection==0.4.2# mcp# pydantic# pydantic-settings-tzdata==2026.1 ; sys_platform == 'win32'- # via tzlocal-tzlocal==5.3.1- # via apscheduleruritemplate==4.2.0# via google-api-python-clienturllib3==2.7.0diff --git a/server/services/deployment/manager.py b/server/services/deployment/manager.pyindex 4e1c0238..41ad2a84 100644--- a/server/services/deployment/manager.py+++ b/server/services/deployment/manager.py@@ -78,7 +78,6 @@ class DeploymentManager:self._active_runs: Dict[str, Dict[str, asyncio.Task]] = {} # workflow_id -> {run_id: task}self._run_counters: Dict[str, int] = {}self._status_callbacks: Dict[str, Callable] = {}- self._cron_iterations: Dict[str, int] = {} # node_id -> iteration countself._main_loop: Optional[asyncio.AbstractEventLoop] = Noneself._settings = {"stop_on_error": False, "max_concurrent_runs": 100, "use_parallel_executor": True}@@ -251,19 +250,13 @@ class DeploymentManager:await asyncio.gather(*workflow_runs.values(), return_exceptions=True)run_count = len(workflow_runs)- # Cleanup triggers for this workflow+ # Cleanup triggers for this workflow. Cron triggers are+ # Temporal Schedules (swept below via+ # _cancel_canary_cron_schedules); their node statuses are wiped+ # by the _clear_stuck_node_statuses(include_waiting=True) sweep.listener_count = 0- cron_count = 0- cron_node_ids = []if trigger_manager:- # Get cron node IDs before teardown (they'll be cleared)- cron_node_ids = trigger_manager.get_cron_node_ids()listener_count = await trigger_manager.teardown_all_listeners()- cron_count = trigger_manager.teardown_all_crons()-- # Reset cron trigger node statuses to idle- for node_id in cron_node_ids:- await self._broadcaster.update_node_status(node_id, "idle", {}, workflow_id=workflow_id)# Reset listener node statuses to idlefor node_id in listener_nodes:@@ -311,10 +304,6 @@ class DeploymentManager:workflow_id=workflow_id,)- # Clear cron iteration counters for this workflow's cron nodes- for node_id in cron_node_ids:- self._cron_iterations.pop(node_id, None)-# Clear state for this workflowself._deployments.pop(workflow_id, None)self._trigger_managers.pop(workflow_id, None)@@ -328,7 +317,6 @@ class DeploymentManager:"workflow_id": workflow_id,"runs_cancelled": run_count,"listeners_cancelled": listener_count,- "crons_cancelled": cron_count,"waiters_cancelled": waiter_count,"canary_listeners_cancelled": canary_count,"canary_cron_schedules_cancelled": cron_schedule_count,@@ -391,18 +379,15 @@ class DeploymentManager:# =========================================================================async def _setup_cron_trigger(self, node: Dict, workflow_id: str) -> TriggerInfo:- """Setup cron trigger for a node.-- Two paths:-- 1. **Wave 12 C3 canary**: when ``Settings.event_framework_enabled``- is on AND ``cronScheduler`` is in the canary registry, create- a Temporal :class:`Schedule` whose action is the plugin's- :class:`CronTriggerWorkflow`. Survives FastAPI process- restart via the Temporal Schedule service.- 2. **Legacy**: APScheduler ``register_cron_job`` runs an- in-process tick callback. Dies on process restart — kept as- the default while the canary stabilises.+ """Setup cron trigger for a node via a Temporal Schedule.++ Wave 12 C3: the Schedule's action is the plugin's+ :class:`CronTriggerWorkflow`, so firings survive FastAPI process+ restart via the Temporal Schedule service. Temporal is a hard+ requirement — the APScheduler in-process fallback was retired in+ Wave 15.2 (it silently died on process restart, and the dev+ server now self-heals via health-check polling + worker+ auto-restart)."""node_id = node["id"]node_type = node.get("type", "cronScheduler")@@ -413,72 +398,41 @@ class DeploymentManager:frequency = params.get("frequency", "minutes")schedule_desc = self._get_schedule_description(params)- # Path 1: Wave 12 C3 canary — Temporal Schedule.- if await self._canary_listener_enabled_for(node_type):- schedule_id = await self._start_canary_cron_schedule(- node,- workflow_id,- params,- cron_expr=cron_expr,- timezone=timezone,- frequency=frequency,- schedule_desc=schedule_desc,+ if not await self._canary_listener_enabled_for(node_type):+ raise RuntimeError(+ "cronScheduler requires the Temporal event framework "+ "(EVENT_FRAMEWORK_ENABLED) — the legacy APScheduler path "+ "was retired in Wave 15.2")- if schedule_id is not None:- await self._broadcaster.update_node_status(- node_id,- "waiting",- {- "message": f"Waiting for schedule: {cron_expr} (Temporal-durable)",- "cron_expression": cron_expr,- "timezone": timezone,- "schedule_id": schedule_id,- },- workflow_id=workflow_id,- )- return TriggerInfo(node_id, "cron", job_id=schedule_id)- # Fall through to legacy path if Temporal unavailable- # (already logged inside _start_canary_cron_schedule).-- # Initialize iteration counter for this cron node (legacy path).- self._cron_iterations[node_id] = 0-- def on_tick():- if self._main_loop and self._main_loop.is_running():- # Increment iteration counter- self._cron_iterations[node_id] = self._cron_iterations.get(node_id, 0) + 1- iteration = self._cron_iterations[node_id]-- trigger_data = {- "node_id": node_id,- "timestamp": datetime.now().isoformat(),- "trigger_type": "cron",- "event_data": {- "timestamp": datetime.now().isoformat(),- "iteration": iteration,- "frequency": frequency,- "timezone": timezone,- "schedule": schedule_desc,- "cron_expression": cron_expr,- },- }- asyncio.run_coroutine_threadsafe(self._spawn_run(node_id, trigger_data, workflow_id=workflow_id), self._main_loop)- trigger_manager = self._trigger_managers.get(workflow_id)- if not trigger_manager:- raise RuntimeError(f"No trigger manager for workflow {workflow_id}")-- job_id = trigger_manager.setup_cron(node_id, cron_expr, timezone, on_tick)+ schedule_id = await self._start_canary_cron_schedule(+ node,+ workflow_id,+ params,+ cron_expr=cron_expr,+ timezone=timezone,+ frequency=frequency,+ schedule_desc=schedule_desc,+ )+ if schedule_id is None:+ raise RuntimeError(+ "Temporal is required for cronScheduler deployments but "+ "no Temporal client is connected (see logs from "+ "_start_canary_cron_schedule)"+ )- # Broadcast waiting status for cron trigger (like event triggers do)await self._broadcaster.update_node_status(node_id,"waiting",- {"message": f"Waiting for schedule: {cron_expr}", "cron_expression": cron_expr, "timezone": timezone, "job_id": job_id},+ {+ "message": f"Waiting for schedule: {cron_expr} (Temporal-durable)",+ "cron_expression": cron_expr,+ "timezone": timezone,+ "schedule_id": schedule_id,+ },workflow_id=workflow_id,)-- return TriggerInfo(node_id, "cron", job_id=job_id)+ return TriggerInfo(node_id, "cron", job_id=schedule_id)async def _fire_start_trigger(self, node: Dict, workflow_id: str) -> TriggerInfo:"""Fire a start trigger immediately."""diff --git a/server/services/deployment/triggers.py b/server/services/deployment/triggers.pyindex b4eddcf4..de90f9de 100644--- a/server/services/deployment/triggers.py+++ b/server/services/deployment/triggers.py@@ -1,6 +1,10 @@"""Trigger Management - Setup and teardown of workflow triggers.-Handles cron scheduling, event-based triggers (webhook, whatsapp), and cleanup.+Handles event-based triggers (webhook, whatsapp) and polling triggers.+Cron triggers are Temporal Schedules owned by the deployment manager+(services/temporal/schedules.py) — the in-process APScheduler path was+retired in Wave 15.2. ``build_cron_expression`` stays here because the+Temporal Schedule path still translates node parameters through it."""import asyncio@@ -9,7 +13,6 @@ from typing import Dict, Any, Callable, Optionalfrom core.logging import get_loggerfrom constants import WORKFLOW_TRIGGER_TYPESfrom services import event_waiter-from services import scheduler as cron_schedulerlogger = get_logger(__name__)@@ -18,7 +21,6 @@ class TriggerManager:"""Manages workflow trigger lifecycle."""def __init__(self, main_loop: Optional[asyncio.AbstractEventLoop] = None):- self._active_cron_jobs: Dict[str, str] = {} # node_id -> job_idself._active_listeners: Dict[str, asyncio.Task] = {} # node_id -> taskself._main_loop = main_loopself._is_running = False@@ -29,48 +31,6 @@ class TriggerManager:def set_main_loop(self, loop: asyncio.AbstractEventLoop):self._main_loop = loop- # =========================================================================- # CRON TRIGGERS- # =========================================================================-- def setup_cron(self, node_id: str, cron_expr: str, timezone: str, on_tick: Callable[[], None]) -> str:- """Setup a cron trigger that calls on_tick on schedule."""- job_id = f"cron_{node_id}"-- def tick_callback():- if not self._is_running:- return- on_tick()-- cron_scheduler.register_cron_job(job_id=job_id, cron_expression=cron_expr, callback=tick_callback, timezone=timezone)-- self._active_cron_jobs[node_id] = job_id- logger.info("Cron trigger setup", job_id=job_id, expr=cron_expr)- return job_id-- def teardown_cron(self, node_id: str) -> bool:- """Remove a specific cron trigger."""- job_id = self._active_cron_jobs.pop(node_id, None)- if job_id:- cron_scheduler.remove_cron_job(job_id)- logger.debug("Cron trigger removed", job_id=job_id)- return True- return False-- def get_cron_node_ids(self) -> list:- """Get node IDs of active cron triggers."""- return list(self._active_cron_jobs.keys())-- def teardown_all_crons(self) -> int:- """Remove all cron triggers."""- count = 0- for node_id, job_id in list(self._active_cron_jobs.items()):- cron_scheduler.remove_cron_job(job_id)- count += 1- self._active_cron_jobs.clear()- logger.info("All cron triggers removed", count=count)- return count-# =========================================================================# EVENT TRIGGERS (Webhook, WhatsApp, etc.)# =========================================================================diff --git a/server/services/scheduler.py b/server/services/scheduler.pydeleted file mode 100644index d10c730e..00000000--- a/server/services/scheduler.py+++ /dev/null@@ -1,124 +0,0 @@-"""-Cron Scheduler Service using APScheduler.-Manages scheduled jobs for workflow automation.-"""--from apscheduler.schedulers.asyncio import AsyncIOScheduler-from apscheduler.triggers.cron import CronTrigger-from apscheduler.jobstores.base import JobLookupError-from typing import Callable, Dict, Optional-import logging--logger = logging.getLogger(__name__)--_scheduler: Optional[AsyncIOScheduler] = None---def get_scheduler() -> AsyncIOScheduler:- """Get or create the singleton scheduler instance."""- global _scheduler- if _scheduler is None:- _scheduler = AsyncIOScheduler(timezone="UTC")- return _scheduler---def start_scheduler():- """Start the scheduler if not already running."""- scheduler = get_scheduler()- if not scheduler.running:- scheduler.start()- logger.info("[Scheduler] Started")---def shutdown_scheduler():- """Shutdown the scheduler gracefully."""- scheduler = get_scheduler()- if scheduler.running:- scheduler.shutdown(wait=False)- logger.info("[Scheduler] Shutdown")---def register_cron_job(job_id: str, cron_expression: str, callback: Callable, timezone: str = "UTC", **kwargs) -> str:- """- Register a cron job with the scheduler.-- Args:- job_id: Unique identifier for the job- cron_expression: 6-field cron expression (second minute hour day month weekday)- or 5-field (minute hour day month weekday)- callback: Async function to call when job fires- timezone: Timezone for schedule (default: UTC)- **kwargs: Additional arguments passed to the callback-- Returns:- The job_id- """- scheduler = get_scheduler()-- # Parse cron expression - support both 5-field and 6-field formats- parts = cron_expression.split()-- if len(parts) >= 6:- # 6-field format: second minute hour day month weekday- trigger = CronTrigger(- second=parts[0], minute=parts[1], hour=parts[2], day=parts[3], month=parts[4], day_of_week=parts[5], timezone=timezone- )- else:- # 5-field format: minute hour day month weekday (default second=0)- if len(parts) < 5:- parts.extend(["*"] * (5 - len(parts)))- trigger = CronTrigger(- second="0", minute=parts[0], hour=parts[1], day=parts[2], month=parts[3], day_of_week=parts[4], timezone=timezone- )-- scheduler.add_job(callback, trigger=trigger, id=job_id, replace_existing=True, kwargs=kwargs)-- logger.info(f"[Scheduler] Registered cron job: {job_id} with expression: {cron_expression}")- return job_id---def remove_cron_job(job_id: str) -> bool:- """- Remove a cron job from the scheduler.-- Args:- job_id: The job identifier to remove-- Returns:- True if job was removed, False if not found- """- scheduler = get_scheduler()- try:- scheduler.remove_job(job_id)- logger.info(f"[Scheduler] Removed cron job: {job_id}")- return True- except JobLookupError:- logger.warning(f"[Scheduler] Job not found: {job_id}")- return False---def get_job_info(job_id: str) -> Optional[Dict]:- """- Get information about a scheduled job.-- Args:- job_id: The job identifier-- Returns:- Dict with job info or None if not found- """- scheduler = get_scheduler()- job = scheduler.get_job(job_id)- if job:- return {"id": job.id, "next_run_time": job.next_run_time.isoformat() if job.next_run_time else None, "trigger": str(job.trigger)}- return None---def get_all_jobs() -> list:- """Get list of all scheduled jobs."""- scheduler = get_scheduler()- jobs = scheduler.get_jobs()- return [- {"id": job.id, "next_run_time": job.next_run_time.isoformat() if job.next_run_time else None, "trigger": str(job.trigger)}- for job in jobs- ]