mirror of
https://github.com/Rushilwiz/SkoolOS.git
synced 2025-04-16 02:10:19 -04:00
began to implement background service with ThreadedNotifier class
This commit is contained in:
parent
78d6e9eab1
commit
fa2e3f6275
|
@ -7,9 +7,8 @@ import event_processor
|
||||||
|
|
||||||
class SkoolOSDaemon:
|
class SkoolOSDaemon:
|
||||||
"""Constructor"""
|
"""Constructor"""
|
||||||
def __init__(self, work_dir, daemonize):
|
def __init__(self, work_dir='/tmp'):
|
||||||
self.work_dir = work_dir
|
self.work_dir = work_dir
|
||||||
self.daemonize = daemonize
|
|
||||||
self.start_time = None
|
self.start_time = None
|
||||||
self.end_time = None
|
self.end_time = None
|
||||||
self.log_file = 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 = open('/tmp/skooloslogs/' + str(self.start_time), 'w')
|
||||||
self.log_file.write("Start time: \n" + self.readable_time(self.start_time) + "\n\n")
|
self.log_file.write("Start time: \n" + self.readable_time(self.start_time) + "\n\n")
|
||||||
sys.stdout = self.log_file
|
sys.stdout = self.log_file
|
||||||
event_processor.watch_dir(self.work_dir, self.daemonize)
|
event_processor.watch_dir(self.work_dir)
|
||||||
def stop(self):
|
def stop(self):
|
||||||
event_processor.stop_watching()
|
event_processor.stop_watching()
|
||||||
self.end_time = time.time()
|
self.end_time = time.time()
|
||||||
|
@ -49,7 +48,7 @@ def Main():
|
||||||
def signal_handler(signum, frame):
|
def signal_handler(signum, frame):
|
||||||
logger.stop()
|
logger.stop()
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
signal.signal(signal.SIGTERM, signal_handler)
|
# signal.signal(signal.SIGTERM, signal_handler)
|
||||||
global logger
|
global logger
|
||||||
logger = SkoolOSDaemon("/tmp")
|
logger = SkoolOSDaemon("/tmp")
|
||||||
logger.start()
|
logger.start()
|
||||||
|
|
|
@ -18,7 +18,7 @@ class EventProcessor(pyinotify.ProcessEvent):
|
||||||
|
|
||||||
def __method_format(method):
|
def __method_format(method):
|
||||||
return {
|
return {
|
||||||
"IN_OPEN":"Accessed a file",
|
"IN_OPEN":"Opened a file",
|
||||||
"IN_CREATE":"Created a file",
|
"IN_CREATE":"Created a file",
|
||||||
"IN_CLOSE_WRITE":"Wrote to a file",
|
"IN_CLOSE_WRITE":"Wrote to a file",
|
||||||
"IN_DELETE":"Deleted a file",
|
"IN_DELETE":"Deleted a file",
|
||||||
|
@ -38,11 +38,11 @@ def __process_generator(cls, method):
|
||||||
readable_time(time.time())
|
readable_time(time.time())
|
||||||
)
|
)
|
||||||
if "IN_DELETE" in description:
|
if "IN_DELETE" in description:
|
||||||
description += "Warning: Unexpected file deletion\n"
|
description += "WARNING: Unexpected file deletion\n"
|
||||||
if "IN_MOVED_TO" in description:
|
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:
|
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)
|
print(description)
|
||||||
_method_name.__name__ = "process_{}".format(method)
|
_method_name.__name__ = "process_{}".format(method)
|
||||||
setattr(cls, _method_name.__name__, _method_name)
|
setattr(cls, _method_name.__name__, _method_name)
|
||||||
|
@ -51,14 +51,14 @@ def __process_generator(cls, method):
|
||||||
EVENT_NOTIFIER = None
|
EVENT_NOTIFIER = None
|
||||||
|
|
||||||
|
|
||||||
def watch_dir(dir_to_watch, daemonize=False):
|
def watch_dir(dir_to_watch):
|
||||||
global EVENT_NOTIFIER
|
global EVENT_NOTIFIER
|
||||||
for method in EventProcessor._methods:
|
for method in EventProcessor._methods:
|
||||||
__process_generator(EventProcessor, method)
|
__process_generator(EventProcessor, method)
|
||||||
watch_manager = pyinotify.WatchManager()
|
watch_manager = pyinotify.WatchManager()
|
||||||
EVENT_NOTIFIER = pyinotify.Notifier(watch_manager, EventProcessor())
|
EVENT_NOTIFIER = pyinotify.ThreadedNotifier(watch_manager, EventProcessor())
|
||||||
watch_manager.add_watch(dir_to_watch, pyinotify.ALL_EVENTS)
|
watch_manager.add_watch(dir_to_watch, pyinotify.ALL_EVENTS, rec=True)
|
||||||
EVENT_NOTIFIER.loop(daemonize=daemonize)
|
EVENT_NOTIFIER.loop()
|
||||||
|
|
||||||
|
|
||||||
def stop_watching():
|
def stop_watching():
|
||||||
|
|
19
BackgroundService/test.py
Normal file
19
BackgroundService/test.py
Normal 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
|
Loading…
Reference in New Issue
Block a user