Added and partly debugged multithreaded RedirectorWatch

parent 6e897a1e
...@@ -5,7 +5,7 @@ EMAIL = "enk@etersoft.ru" ...@@ -5,7 +5,7 @@ EMAIL = "enk@etersoft.ru"
PYTHON_VERSION = "3.6" PYTHON_VERSION = "3.6"
VERSION_STATUS = "alpha" VERSION_STATUS = "alpha"
REDIRECTOR_DIR = "/home/enk/var/lib/redirector" # /home/enk REDIRECTOR_DIR = "/root/redirector" # /home/enk
CONFIG_YAML = '/etc/redirector/config.yaml' CONFIG_YAML = '/etc/redirector/config.yaml'
MAPS_DIR = REDIRECTOR_DIR + "/maps" MAPS_DIR = REDIRECTOR_DIR + "/maps"
CONFIG_DIR = REDIRECTOR_DIR + "/location-includes" CONFIG_DIR = REDIRECTOR_DIR + "/location-includes"
...@@ -3,12 +3,13 @@ import os ...@@ -3,12 +3,13 @@ import os
import pyinotify import pyinotify
import asyncore import asyncore
from core import generators from mymodule import generators
from core import logger from mymodule import logger
from core import parser from mymodule import parser
from core import const from mymodule import const
redirector_watch = None
class Redirector: class Redirector:
def __init__(self, logger=None): def __init__(self, logger=None):
self.parser = parser.ConfigReader(logger=logger) self.parser = parser.ConfigReader(logger=logger)
...@@ -55,11 +56,23 @@ class RedirectorWatch: ...@@ -55,11 +56,23 @@ class RedirectorWatch:
self.logger = logger.Logger("redirector-watch.log") self.logger = logger.Logger("redirector-watch.log")
self.redirector = Redirector() self.redirector = Redirector()
self.parser = parser.ConfigReader(logger=self.logger) self.parser = parser.ConfigReader(logger=self.logger)
self.mask = pyinotify.IN_MODIFY | pyinotify.IN_DELETE self.mask = pyinotify.IN_MODIFY
self.watch_manager = pyinotify.WatchManager() self.wm_pool = []
self.notifier = None self.n_pool = []
def watch(self, yaml_file): def watch(self, yaml_file):
if not yaml_file:
raise self.RedirectorWatchException("yaml_file list is empty", Exception)
yaml_file = yaml_file if isinstance(yaml_file, list) else [yaml_file]
for yfile in yaml_file:
Watch = self.create_watch_class(yfile)
try:
self.set_inotify(yfile, Watch)
except pyinotify.PyinotifyError as e:
raise self.RedirectorWatchException("Can\'t set inotify for yamll file %s" % yfile, e)
# creating class Watch with custom static 'yaml_file' argument
def create_watch_class(self, yaml_file):
class Watch(pyinotify.ProcessEvent): class Watch(pyinotify.ProcessEvent):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.redirector = kwargs.pop("redirector") self.redirector = kwargs.pop("redirector")
...@@ -67,13 +80,27 @@ class RedirectorWatch: ...@@ -67,13 +80,27 @@ class RedirectorWatch:
def process_IN_MODIFY(self, event): def process_IN_MODIFY(self, event):
self.redirector.generate(yaml_file, event.pathname) self.redirector.generate(yaml_file, event.pathname)
return Watch
self.notifier = pyinotify.AsyncNotifier(self.watch_manager, Watch(redirector=self.redirector))
file_list = self.parser.parse_yaml(yaml_file) # we suggest that self.watch_manager already exists
def set_inotify(self, yaml_file, Watch_class):
wm = pyinotify.WatchManager()
self.add_files(yaml_file, wm)
self.wm_pool.append(wm)
notifier = pyinotify.ThreadedNotifier(wm, Watch_class(redirector=self.redirector))
notifier.start()
self.n_pool.append(notifier)
def add_files(self, file_, wm):
file_list = self.parser.parse_yaml(file_)
for map_path, _ in file_list: for map_path, _ in file_list:
cwd = os.getcwd() cwd = os.getcwd()
wdd = self.watch_manager.add_watch(cwd + "/" + map_path, self.mask, rec=True) wdd = wm.add_watch(cwd + "/" + map_path, self.mask, rec=True)
asyncore.loop()
class RedirectorWatchException(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
def main(args=None): def main(args=None):
...@@ -87,8 +114,9 @@ def main(args=None): ...@@ -87,8 +114,9 @@ def main(args=None):
def watch(args=None): def watch(args=None):
global redirector_watch
if not redirector_watch:
redirector_watch = RedirectorWatch()
if not args: if not args:
args = sys.argv[1:] args = sys.argv[1:]
print(args) redirector_watch.watch(args)
watch_ = RedirectorWatch()
watch_.watch(args)
import os import os
import shutil
from distutils.core import setup from setuptools import setup, find_packages
from distutils.command.install import install
# from distutils import log
from core import const from core import const
#####
data_files_list = [
['/etc/redirector']
]
for maps_list_item in (("etc/redirector", "core"),):
data_files_list.append((maps_list_item[0], [os.path.join(maps_list_item[1], item) for item
in os.listdir(maps_list_item[1]) if
os.path.isfile(maps_list_item[1] + '/' + item) and item not in (
".gitignore", "__pycache__")]))
classifiers_list = [ classifiers_list = [
"Development Status :: 4 - Beta", "Development Status :: 4 - Beta",
"Environment :: Plugins", "Environment :: Plugins",
...@@ -30,40 +17,17 @@ classifiers_list = [ ...@@ -30,40 +17,17 @@ classifiers_list = [
}[const.VERSION_STATUS] }[const.VERSION_STATUS]
] ]
"""
add readme
add requirements.txt
config
"""
# Private classes #
class RedirectorInstall(install):
def run(self):
install.run(self)
# Main #
setup( setup(
name=const.NAME, name=const.NAME,
version=const.VERSION, version="0.1",
packages=find_packages(),
author=const.AUTHOR, entry_points={
author_email=const.EMAIL, 'console_scripts': [
# maintainer = const.MAINTAINER, 'redirector = core.redirector:main',
# maintainer_email =const.MAINTAINER_EMAIL, 'redirector-watch = core.redirector:watch'
]
description="Utility for generating nginx .config and .map files from custom config files", },
packages=None,
data_files=data_files_list,
requires=[
"pyyaml",
"pyinotify"
],
cmdclass={"install": RedirectorInstall},
classifiers=classifiers_list classifiers=classifiers_list
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment