Spaces:
Running
Running
File size: 1,749 Bytes
a31f556 | 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 | """
modules/app_scheduler.py — Phase 4: Scheduled app launching (Windows Task Scheduler).
Creates a one-time task using schtasks for the current user.
"""
from __future__ import annotations
import datetime as _dt
import subprocess
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class SchedResult:
ok: bool
message: str
def schedule_run(task_name: str, exe: str, when: _dt.datetime) -> SchedResult:
tn = (task_name or "").strip() or "FRIDAY_RunApp"
cmd = (exe or "").strip()
if not cmd:
return SchedResult(False, "Missing executable.")
# schtasks expects HH:MM (24h) and date MM/DD/YYYY
st = when.strftime("%H:%M")
sd = when.strftime("%m/%d/%Y")
p = subprocess.run(
["schtasks", "/Create", "/SC", "ONCE", "/TN", tn, "/TR", cmd, "/ST", st, "/SD", sd, "/F"],
capture_output=True,
text=True,
shell=False,
)
out = (p.stdout or p.stderr or "").strip()
if p.returncode == 0:
return SchedResult(True, f"Scheduled {cmd} at {when}.")
if "access is denied" in out.lower():
return SchedResult(False, "Scheduling needs admin permission on this system.")
return SchedResult(False, f"Schedule failed: {out[:200]}")
def cancel(task_name: str) -> SchedResult:
tn = (task_name or "").strip()
if not tn:
return SchedResult(False, "Missing task name.")
p = subprocess.run(["schtasks", "/Delete", "/TN", tn, "/F"], capture_output=True, text=True, shell=False)
out = (p.stdout or p.stderr or "").strip()
if p.returncode == 0:
return SchedResult(True, f"Cancelled task {tn}.")
return SchedResult(False, f"Cancel failed: {out[:200]}")
|