Added and partly debugged multithreaded RedirectorWatch

parent 6e897a1e
......@@ -5,7 +5,7 @@ EMAIL = "enk@etersoft.ru"
PYTHON_VERSION = "3.6"
VERSION_STATUS = "alpha"
REDIRECTOR_DIR = "/home/enk/var/lib/redirector" # /home/enk
REDIRECTOR_DIR = "/root/redirector" # /home/enk
CONFIG_YAML = '/etc/redirector/config.yaml'
MAPS_DIR = REDIRECTOR_DIR + "/maps"
CONFIG_DIR = REDIRECTOR_DIR + "/location-includes"
......@@ -3,11 +3,12 @@ import os
import pyinotify
import asyncore
from core import generators
from core import logger
from core import parser
from core import const
from mymodule import generators
from mymodule import logger
from mymodule import parser
from mymodule import const
redirector_watch = None
class Redirector:
def __init__(self, logger=None):
......@@ -55,11 +56,23 @@ class RedirectorWatch:
self.logger = logger.Logger("redirector-watch.log")
self.redirector = Redirector()
self.parser = parser.ConfigReader(logger=self.logger)
self.mask = pyinotify.IN_MODIFY | pyinotify.IN_DELETE
self.watch_manager = pyinotify.WatchManager()
self.notifier = None
self.mask = pyinotify.IN_MODIFY
self.wm_pool = []
self.n_pool = []
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):
def __init__(self, *args, **kwargs):
self.redirector = kwargs.pop("redirector")
......@@ -67,13 +80,27 @@ class RedirectorWatch:
def process_IN_MODIFY(self, event):
self.redirector.generate(yaml_file, event.pathname)
self.notifier = pyinotify.AsyncNotifier(self.watch_manager, Watch(redirector=self.redirector))
file_list = self.parser.parse_yaml(yaml_file)
return Watch
# 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:
cwd = os.getcwd()
wdd = self.watch_manager.add_watch(cwd + "/" + map_path, self.mask, rec=True)
asyncore.loop()
wdd = wm.add_watch(cwd + "/" + map_path, self.mask, rec=True)
class RedirectorWatchException(Exception):
def __init__(self, message, errors):
super().__init__(message)
self.errors = errors
def main(args=None):
......@@ -87,8 +114,9 @@ def main(args=None):
def watch(args=None):
global redirector_watch
if not redirector_watch:
redirector_watch = RedirectorWatch()
if not args:
args = sys.argv[1:]
print(args)
watch_ = RedirectorWatch()
watch_.watch(args)
redirector_watch.watch(args)
import os
import shutil
from distutils.core import setup
from distutils.command.install import install
# from distutils import log
from setuptools import setup, find_packages
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 = [
"Development Status :: 4 - Beta",
"Environment :: Plugins",
......@@ -30,40 +17,17 @@ classifiers_list = [
}[const.VERSION_STATUS]
]
"""
add readme
add requirements.txt
config
"""
# Private classes #
class RedirectorInstall(install):
def run(self):
install.run(self)
# Main #
setup(
name=const.NAME,
version=const.VERSION,
author=const.AUTHOR,
author_email=const.EMAIL,
# maintainer = const.MAINTAINER,
# maintainer_email =const.MAINTAINER_EMAIL,
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},
version="0.1",
packages=find_packages(),
entry_points={
'console_scripts': [
'redirector = core.redirector:main',
'redirector-watch = core.redirector:watch'
]
},
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