Manually run a task from the scheduler by ID
(self, input: Input, request: Request)
| 9 | _printer: PrintStyle = PrintStyle(italic=True, font_color="green", padding=False) |
| 10 | |
| 11 | async def process(self, input: Input, request: Request) -> Output: |
| 12 | """ |
| 13 | Manually run a task from the scheduler by ID |
| 14 | """ |
| 15 | # Get timezone from input (do not set if not provided, we then rely on poll() to set it) |
| 16 | if timezone := input.get("timezone", None): |
| 17 | Localization.get().set_timezone(timezone) |
| 18 | |
| 19 | # Get task ID from input |
| 20 | task_id: str = input.get("task_id", "") |
| 21 | |
| 22 | if not task_id: |
| 23 | return {"error": "Missing required field: task_id"} |
| 24 | |
| 25 | self._printer.print(f"SchedulerTaskRun: On-Demand running task {task_id}") |
| 26 | |
| 27 | scheduler = TaskScheduler.get() |
| 28 | await scheduler.reload() |
| 29 | |
| 30 | # Check if the task exists first |
| 31 | task = scheduler.get_task_by_uuid(task_id) |
| 32 | if not task: |
| 33 | self._printer.error(f"SchedulerTaskRun: Task with ID '{task_id}' not found") |
| 34 | return {"error": f"Task with ID '{task_id}' not found"} |
| 35 | |
| 36 | # Check if task is already running |
| 37 | if task.state == TaskState.RUNNING: |
| 38 | # Return task details along with error for better frontend handling |
| 39 | serialized_task = scheduler.serialize_task(task_id) |
| 40 | self._printer.error(f"SchedulerTaskRun: Task '{task_id}' is in state '{task.state}' and cannot be run") |
| 41 | return { |
| 42 | "error": f"Task '{task_id}' is in state '{task.state}' and cannot be run", |
| 43 | "task": serialized_task |
| 44 | } |
| 45 | |
| 46 | # Run the task, which now includes atomic state checks and updates |
| 47 | try: |
| 48 | await scheduler.run_task_by_uuid(task_id) |
| 49 | self._printer.print(f"SchedulerTaskRun: Task '{task_id}' started successfully") |
| 50 | # Get updated task after run starts |
| 51 | serialized_task = scheduler.serialize_task(task_id) |
| 52 | if serialized_task: |
| 53 | return { |
| 54 | "success": True, |
| 55 | "message": f"Task '{task_id}' started successfully", |
| 56 | "task": serialized_task |
| 57 | } |
| 58 | else: |
| 59 | return {"success": True, "message": f"Task '{task_id}' started successfully"} |
| 60 | except ValueError as e: |
| 61 | self._printer.error(f"SchedulerTaskRun: Task '{task_id}' failed to start: {str(e)}") |
| 62 | return {"error": str(e)} |
| 63 | except Exception as e: |
| 64 | self._printer.error(f"SchedulerTaskRun: Task '{task_id}' failed to start: {str(e)}") |
| 65 | return {"error": f"Failed to run task '{task_id}': {str(e)}"} |
nothing calls this directly
no test coverage detected