Spaces:
Paused
Paused
File size: 2,883 Bytes
8d1819a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
from python.helpers.api import ApiHandler, Input, Output, Request
from python.helpers.task_scheduler import TaskScheduler, TaskState
from python.helpers.print_style import PrintStyle
from python.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)}"}
|