began to implement background service with ThreadedNotifier class

This commit is contained in:
Nathaniel Kenschaft 2020-06-14 14:29:27 -04:00
parent 78d6e9eab1
commit fa2e3f6275
3 changed files with 30 additions and 12 deletions

View File

@ -7,9 +7,8 @@ import event_processor
class SkoolOSDaemon:
"""Constructor"""
def __init__(self, work_dir, daemonize):
def __init__(self, work_dir='/tmp'):
self.work_dir = work_dir
self.daemonize = daemonize
self.start_time = None
self.end_time = None
self.log_file = None
@ -32,7 +31,7 @@ class SkoolOSDaemon:
self.log_file = open('/tmp/skooloslogs/' + str(self.start_time), 'w')
self.log_file.write("Start time: \n" + self.readable_time(self.start_time) + "\n\n")
sys.stdout = self.log_file
event_processor.watch_dir(self.work_dir, self.daemonize)
event_processor.watch_dir(self.work_dir)
def stop(self):
event_processor.stop_watching()
self.end_time = time.time()
@ -49,7 +48,7 @@ def Main():
def signal_handler(signum, frame):
logger.stop()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# signal.signal(signal.SIGTERM, signal_handler)
global logger
logger = SkoolOSDaemon("/tmp")
logger.start()

View File

@ -18,7 +18,7 @@ class EventProcessor(pyinotify.ProcessEvent):
def __method_format(method):
return {
"IN_OPEN":"Accessed a file",
"IN_OPEN":"Opened a file",
"IN_CREATE":"Created a file",
"IN_CLOSE_WRITE":"Wrote to a file",
"IN_DELETE":"Deleted a file",
@ -38,11 +38,11 @@ def __process_generator(cls, method):
readable_time(time.time())
)
if "IN_DELETE" in description:
description += "Warning: Unexpected file deletion\n"
description += "WARNING: Unexpected file deletion\n"
if "IN_MOVED_TO" in description:
description += "Warning: Unexpected file add to work\n"
description += "WARNING: Unexpected file add to work\n"
if "IN_MOVED_FROM" in description:
description += "Warning: Unexpected file moved out of directory\n"
description += "WARNING: Unexpected file moved out of directory\n"
print(description)
_method_name.__name__ = "process_{}".format(method)
setattr(cls, _method_name.__name__, _method_name)
@ -51,14 +51,14 @@ def __process_generator(cls, method):
EVENT_NOTIFIER = None
def watch_dir(dir_to_watch, daemonize=False):
def watch_dir(dir_to_watch):
global EVENT_NOTIFIER
for method in EventProcessor._methods:
__process_generator(EventProcessor, method)
watch_manager = pyinotify.WatchManager()
EVENT_NOTIFIER = pyinotify.Notifier(watch_manager, EventProcessor())
watch_manager.add_watch(dir_to_watch, pyinotify.ALL_EVENTS)
EVENT_NOTIFIER.loop(daemonize=daemonize)
EVENT_NOTIFIER = pyinotify.ThreadedNotifier(watch_manager, EventProcessor())
watch_manager.add_watch(dir_to_watch, pyinotify.ALL_EVENTS, rec=True)
EVENT_NOTIFIER.loop()
def stop_watching():

19
BackgroundService/test.py Normal file
View File

@ -0,0 +1,19 @@
from bgservice import SkoolOSDaemon as sod
import threading
logger = sod()
if __name__ == "__main__":
line=1
print(line)
line+=1
logger.start()
print(line)
line+=1
input("Enter any key when you are done modifyng the /tmp/ directory")
print(line)
line+=1
logger.stop()
print(line)
line+=1