-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonService.py
82 lines (70 loc) · 2.98 KB
/
PythonService.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
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
# ZPF
# encoding=utf-8
import win32timezone
from logging.handlers import TimedRotatingFileHandler
import win32serviceutil
import win32service
import win32event
import os
import logging
import inspect
import time
import shutil
class PythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "PythonService" #服務名
_svc_display_name_ = "PythonService" #job在windows services上顯示的名字
_svc_description_ = "PythonService system files" #job的描述
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.logger = self._getLogger()
self.path = 'D:\\WebSite'
self.T = time.time()
self.run = True
def _getLogger(self):
'''日誌記錄'''
logger = logging.getLogger('[PythonService]')
this_file = inspect.getfile(inspect.currentframe())
dirpath = os.path.abspath(os.path.dirname(this_file))
if os.path.isdir('%s\\log'%dirpath): #創建log文件夾
pass
else:
os.mkdir('%s\\log'%dirpath)
dir = '%s\\log' % dirpath
handler = TimedRotatingFileHandler(os.path.join(dir, "Clearjob.log"),when="midnight",interval=1,backupCount=20)
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
def SvcDoRun(self):
self.logger.info("service is run....")
try:
while self.run:
self.logger.info('---Begin---')
for path, name, file in os.walk('D:\\Website'):
if path == 'D:\\Website':
for IISname in name:
floder = []
for i in os.listdir(os.path.join(path, IISname)):
if i.isdigit():
floder.append(int(i))
if len(floder) == 0:
pass
elif len(floder) >= 2: # 設置保留備份
floder.sort()
for x in floder[:(len(floder) - 2)]:
self.logger.info("delete dir: %s" % os.path.join(os.path.join(path, IISname), str(x)))
shutil.rmtree(os.path.join(os.path.join(path, IISname), str(x)))
self.logger.info('---End---')
time.sleep(10)
except Exception as e:
self.logger.info(e)
time.sleep(60)
def SvcStop(self):
self.logger.info("service is stop....")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.run = False
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PythonService)