Files
agent-zero/plugins/_scheduler/api/scheduler_task_run.py
linkliti 6f079d8817 feat(plugins): move the tasks interface and scheduler task endpoints into the scheduler plugin
Move the scheduler CRUD endpoints, modals, styles, sidebar tasks list, and task row into the _scheduler plugin and drop the core tasks components. The sidebar hosts plugin sections through a neutral sidebar-extensions point inside the extensions-section container instead of a tasks-specific one, so other plugins can contribute sections there too. The scheduler and datepicker styles load through the plugin page-head extension instead of core index.html preloads, and the shared flatpickr preload stays in core. Repoint the header scheduler entry at the plugin modal path and update the api and plugins DOX indexes. Pin indicators follow the relocated task row and the pin test reads it from the plugin. The chats store and core chat behavior adopt the plugin task store idiom, and the ws client surface test expects the plugin store accessor. The modals and css DOX drop their core scheduler ownership and the scheduler plugin DOX indexes its sidebar and page-head extensions. The plugin manifest becomes always enabled with a broader description so the Tasks UI stays core behavior, and the scheduler editor test follows the moved endpoint and store paths.

Validation: pytest plugins/_pin_to_top/tests/test_pins.py tests/test_sidebar_row_actions.py tests/test_webui_extension_surfaces.py tests/test_ws_client_api_surface.py tests/test_webui_startup_assets.py::test_only_the_icon_guard_stylesheet_blocks_application_paint tests/test_chat_working_animation.py tests/test_scheduler_editor.py
2026-10-21 23:37:31 +03:00

66 lines
2.8 KiB
Python

from helpers.api import ApiHandler, Input, Output, Request
from helpers.task_scheduler import TaskScheduler, TaskState
from helpers.print_style import PrintStyle
from helpers.localization import Localization
class SchedulerTaskRun(ApiHandler):
_printer: PrintStyle = PrintStyle(italic=True, font_color="green", padding=False)
async def process(self, input: Input, request: Request) -> Output:
"""
Manually run a task from the scheduler by ID
"""
# Get timezone from input (do not set if not provided, we then rely on poll() to set it)
if timezone := input.get("timezone", None):
Localization.get().set_timezone(timezone)
# Get task ID from input
task_id: str = input.get("task_id", "")
if not task_id:
return {"error": "Missing required field: task_id"}
self._printer.print(f"SchedulerTaskRun: On-Demand running task {task_id}")
scheduler = TaskScheduler.get()
await scheduler.reload()
# Check if the task exists first
task = scheduler.get_task_by_uuid(task_id)
if not task:
self._printer.error(f"SchedulerTaskRun: Task with ID '{task_id}' not found")
return {"error": f"Task with ID '{task_id}' not found"}
# Check if task is already running
if task.state == TaskState.RUNNING:
# Return task details along with error for better frontend handling
serialized_task = scheduler.serialize_task(task_id)
self._printer.error(f"SchedulerTaskRun: Task '{task_id}' is in state '{task.state}' and cannot be run")
return {
"error": f"Task '{task_id}' is in state '{task.state}' and cannot be run",
"task": serialized_task
}
# Run the task, which now includes atomic state checks and updates
try:
await scheduler.run_task_by_uuid(task_id)
self._printer.print(f"SchedulerTaskRun: Task '{task_id}' started successfully")
# Get updated task after run starts
serialized_task = scheduler.serialize_task(task_id)
if serialized_task:
return {
"success": True,
"message": f"Task '{task_id}' started successfully",
"task": serialized_task
}
else:
return {"success": True, "message": f"Task '{task_id}' started successfully"}
except ValueError as e:
self._printer.error(f"SchedulerTaskRun: Task '{task_id}' failed to start: {str(e)}")
return {"error": str(e)}
except Exception as e:
self._printer.error(f"SchedulerTaskRun: Task '{task_id}' failed to start: {str(e)}")
return {"error": f"Failed to run task '{task_id}': {str(e)}"}