-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpycsvc.py
50 lines (47 loc) · 1.9 KB
/
rpycsvc.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
# set up logging #####################################
import sys,logging,logging.handlers,os.path
#in this particular case, argv[0] is likely pythonservice.exe deep in python's lib\
# so it makes no sense to write log there
log_file=os.path.splitext(__file__)[0]+".log"
l = logging.getLogger()
l.setLevel(logging.DEBUG)
f = logging.Formatter('%(asctime)s %(process)d:%(thread)d %(name)s %(levelname)-8s %(message)s')
h=logging.StreamHandler(sys.stdout)
h.setLevel(logging.NOTSET)
h.setFormatter(f)
l.addHandler(h)
h=logging.handlers.RotatingFileHandler(log_file,maxBytes=1024**2,backupCount=1)
h.setLevel(logging.NOTSET)
h.setFormatter(f)
l.addHandler(h)
del h,f
#hook to log unhandled exceptions
def excepthook(type,value,traceback):
logging.error("Unhandled exception occured",exc_info=(type,value,traceback))
#Don't need another copy of traceback on stderr
if old_excepthook!=sys.__excepthook__:
old_excepthook(type,value,traceback)
old_excepthook = sys.excepthook
sys.excepthook = excepthook
del excepthook,log_file
# ####################################################
import win32serviceutil, argparse
import rpyc.utils.server, rpyc.core, rpyc.utils.classic
class RPyCService(win32serviceutil.ServiceFramework):
_svc_name_="RPyCService"
_svc_display_name_="RPyCService"
_svc_description_="""RPyC's rpyc_classis service wrapper"""
_svc_deps_=("tcpip",)
def SvcDoRun(self):
l.info("Starting service")
#launch method taken from rpyc_classic.serve_threaded()
#the constructor binds a named logger automatically
self.t = rpyc.utils.server.ThreadedServer(
rpyc.core.SlaveService,
port=rpyc.utils.classic.DEFAULT_SERVER_PORT)
self.t.start()
def SvcStop(self):
l.info("Stopping service")
self.t.close()
if __name__=='__main__':
win32serviceutil.HandleCommandLine(RPyCService)