def SvcDoRun(self): # Log a "started" message to the event log. servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) ProcETL().SendSms('ProcJob开始运行') #如果系统日期不是2007年,将时间调整为2007 (y, m, d, h, mi, s) = time.localtime()[0:6] if y != 2007: setdate = '%04d-%02d-%02d' % (2007, m, d) os.system('date %s' % setdate) win32event.SetWaitableTimer(self.timerForProcess, 0, 1000 * 60 * 1, None, None, 0) win32event.SetWaitableTimer(self.timerForPrepare, 0, 1000 * 60 * 2, None, None, 0) while 1: timeout = win32event.INFINITE waitHandles = self.hWaitStop, self.timerForProcess, self.timerForPrepare rc = win32event.WaitForMultipleObjects(waitHandles, 0, timeout) if rc == win32event.WAIT_OBJECT_0: # Stop event break elif rc == win32event.WAIT_OBJECT_0 + 1: ProcETL().ETLProcess() elif rc == win32event.WAIT_OBJECT_0 + 2: ProcETL().ETLPrepare() win32event.CancelWaitableTimer(self.timerForProcess) win32event.CancelWaitableTimer(self.timerForPrepare) servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, ''))
def stop(self): if HAS_EVENTS: win32event.CancelWaitableTimer(self.event.handle) self.stopped = True
def my_win32_watcher(): CREATED = 1 DELETED = 2 UPDATED = 3 RENAMED_FROM = 4 RENAMED_TO = 5 ACTIONS = { 1: "Created", 2: "Deleted", 3: "Updated", 4: "Renamed from something", 5: "Renamed to something" } # Thanks to Claudio Grondi for the correct set of numbers FILE_LIST_DIRECTORY = 0x0001 path_to_watch = "." hDir = win32file.CreateFile( path_to_watch, FILE_LIST_DIRECTORY, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_FLAG_BACKUP_SEMANTICS, None ) while 1: # # ReadDirectoryChangesW takes a previously-created # handle to a directory, a buffer size for results, # a flag to indicate whether to watch subtrees and # a filter of what changes to notify. # # NB Tim Juchcinski reports that he needed to up # the buffer size to be sure of picking up all # events when a large number of files were # deleted at once. # results = win32file.ReadDirectoryChangesW( hDir, 1024, True, win32con.FILE_NOTIFY_CHANGE_FILE_NAME | win32con.FILE_NOTIFY_CHANGE_DIR_NAME | win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | win32con.FILE_NOTIFY_CHANGE_SIZE | win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | win32con.FILE_NOTIFY_CHANGE_SECURITY, None, None ) do_reload = False for action, file_path in results: if file_path == ".reloadignore": logger.debug("reloading ignore config") reload_ignore_config() if file_triggers_reload(file_path): #l = file_path, file_triggers_reload(file_path) do_reload = True break if do_reload: # terminate on first file change win32event.SetEvent(terminate_event) # 50 ms rollup window for starting reloading win32event.CancelWaitableTimer(restart_event) win32event.SetWaitableTimer(restart_event, RESTART_EVENT_DT, 0, None, None, 0)