-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_service.py
More file actions
115 lines (99 loc) · 4.16 KB
/
Copy pathinstall_service.py
File metadata and controls
115 lines (99 loc) · 4.16 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
"""
Windows Service Installer for CSV Search Engine
This creates a Windows service that runs the Streamlit app 24/7
"""
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
import sys
import os
import subprocess
from pathlib import Path
class CSVSearchEngineService(win32serviceutil.ServiceFramework):
"""Windows Service for CSV Search Engine"""
_svc_name_ = "CSVSearchEngine"
_svc_display_name_ = "CSV Search Engine Service"
_svc_description_ = "24/7 CSV Search Engine with Streamlit Web Interface"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
self.process = None
def SvcStop(self):
"""Stop the service"""
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
if self.process:
self.process.terminate()
def SvcDoRun(self):
"""Run the service"""
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
# Change to the project directory
project_dir = Path(__file__).parent
os.chdir(project_dir)
# Start the Streamlit app
self.start_streamlit()
# Wait for stop signal
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
def start_streamlit(self):
"""Start the Streamlit application"""
try:
# Activate virtual environment and start Streamlit
src_dir = project_dir / "src"
cmd = [
str(project_dir / "venv" / "Scripts" / "python.exe"),
"-m", "streamlit", "run", "app.py",
"--server.port", "8501",
"--server.address", "0.0.0.0",
"--server.headless", "true"
]
self.process = subprocess.Popen(cmd, cwd=src_dir)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
("Streamlit started on port 8501", ''))
except Exception as e:
servicemanager.LogMsg(servicemanager.EVENTLOG_ERROR_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(f"Error starting Streamlit: {e}", ''))
def install_service():
"""Install the Windows service"""
try:
win32serviceutil.InstallService(
CSVSearchEngineService._svc_reg_class_,
CSVSearchEngineService._svc_name_,
CSVSearchEngineService._svc_display_name_,
description=CSVSearchEngineService._svc_description_
)
print("✅ Service installed successfully!")
print("🔧 To start the service, run as Administrator:")
print(" net start CSVSearchEngine")
print("🛑 To stop the service:")
print(" net stop CSVSearchEngine")
print("🗑️ To remove the service:")
print(" python install_service.py remove")
except Exception as e:
print(f"❌ Error installing service: {e}")
def remove_service():
"""Remove the Windows service"""
try:
win32serviceutil.RemoveService(CSVSearchEngineService._svc_name_)
print("✅ Service removed successfully!")
except Exception as e:
print(f"❌ Error removing service: {e}")
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(CSVSearchEngineService)
servicemanager.StartServiceCtrlDispatcher()
elif sys.argv[1] == 'install':
install_service()
elif sys.argv[1] == 'remove':
remove_service()
else:
win32serviceutil.HandleCommandLine(CSVSearchEngineService)