feat: add startWecker and stopWecker mutations to start/stop alarm via API
- Add startWecker mutation: starts wecker.py via subprocess if not already ringing - Add stopWecker mutation: kills the running wecker process via SIGTERM if ringing - Both mutations handle the 'already ringing' / 'not ringing' edge cases gracefully - Update README with API documentation for the new mutations - Add comprehensive tests (unit + GraphQL endpoint)
This commit is contained in:
@@ -2,6 +2,8 @@ import strawberry
|
||||
from typing import List, Optional
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import signal
|
||||
from pathlib import Path
|
||||
from api.crontab_manager import CrontabManager
|
||||
from common import PID_FILE
|
||||
@@ -95,5 +97,36 @@ class Mutation:
|
||||
return True
|
||||
return False
|
||||
|
||||
@strawberry.field
|
||||
def start_wecker(self) -> bool:
|
||||
"""Start the wecker alarm if it is not already ringing.
|
||||
Returns True if started, False if already ringing."""
|
||||
if is_wecker_ringing():
|
||||
return False
|
||||
project_root = Path(__file__).parent.parent.absolute()
|
||||
python_exec = sys.executable
|
||||
cmd = f"cd {project_root} && {python_exec} wecker.py >> wecker.log 2>&1"
|
||||
subprocess.Popen(cmd, shell=True)
|
||||
return True
|
||||
|
||||
@strawberry.field
|
||||
def stop_wecker(self) -> bool:
|
||||
"""Stop the wecker alarm if it is currently ringing.
|
||||
Returns True if stopped, False if not ringing."""
|
||||
if not is_wecker_ringing():
|
||||
return False
|
||||
try:
|
||||
with open(PID_FILE) as f:
|
||||
pid = int(f.read().strip())
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
if os.path.exists(PID_FILE):
|
||||
os.remove(PID_FILE)
|
||||
return True
|
||||
except (ValueError, ProcessLookupError, PermissionError, OSError):
|
||||
# PID file is stale or process already dead
|
||||
if os.path.exists(PID_FILE):
|
||||
os.remove(PID_FILE)
|
||||
return False
|
||||
|
||||
|
||||
schema = strawberry.Schema(query=Query, mutation=Mutation)
|
||||
|
||||
Reference in New Issue
Block a user