-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon_utils.py
56 lines (45 loc) · 1.35 KB
/
daemon_utils.py
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
import os
import sys
import subprocess
from socket import socket as Socket
from socket import AF_INET, SOCK_STREAM
def start_daemon() -> None:
current_path = os.path.dirname(os.path.abspath(__file__))
daemon_path = os.path.join(current_path, "daemon.py")
subprocess.Popen([
sys.executable, daemon_path
],
start_new_session=True,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
def stop_daemon() -> None:
if not daemon_is_running():
return
client = Socket(AF_INET, SOCK_STREAM)
client.connect(("127.0.0.1", 8669))
client.send(b"stop")
ack = client.recv(3)
client.close()
def daemon_is_running() -> bool:
lock_file = os.path.expanduser("~/.daemon.lock")
if not os.path.isfile(lock_file):
return False
with open(lock_file, "r") as file:
try:
pid = int(file.read())
os.kill(pid, 0)
return True
except (ValueError, ProcessLookupError):
return False
def get_daemon_status() -> int | None:
if not daemon_is_running():
return
client = Socket(AF_INET, SOCK_STREAM)
client.connect(("127.0.0.1", 8669))
client.send(b"status")
status = client.recv(4)
client.send(b"ACK")
client.close()
return int.from_bytes(status, "big")