-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
62 lines (51 loc) · 1.61 KB
/
Copy pathcli.py
File metadata and controls
62 lines (51 loc) · 1.61 KB
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
import argparse
from scheduler.engine import SchedulerEngine
engine = SchedulerEngine()
parser = argparse.ArgumentParser(description="Scheduler & Alarms CLI")
sub = parser.add_subparsers(dest="command")
# add job
add = sub.add_parser("add")
add.add_argument("--name", required=True)
add.add_argument("--time")
add.add_argument("--freq", choices=["once", "daily", "weekly", "hourly", "interval"], default="once")
add.add_argument("--seconds", type=int)
# start
sub.add_parser("start")
# list jobs
sub.add_parser("list")
args = parser.parse_args()
if args.command == "add":
rule = {"frequency": args.freq}
if args.freq == "interval":
if not args.seconds:
print("Error: --seconds is required for interval jobs")
exit(1)
rule["seconds"] = args.seconds
else:
if not args.time:
print(f"Error: --time is required for {args.freq} jobs (format: HH:MM)")
exit(1)
rule["time"] = args.time
try:
engine.add_job(args.name, "echo task", rule)
print("Job added successfully.")
except Exception as e:
print(f"Error adding job: {e}")
exit(1)
elif args.command == "list":
jobs = engine.list_jobs()
if not jobs:
print("No jobs scheduled.")
else:
for job in jobs:
print(f"- {job['name']} | next run: {job['next_run']}")
elif args.command == "start":
print("Starting scheduler... (Ctrl+C to stop)")
try:
engine.start()
import time
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nShutting down...")
engine.stop()