Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a019f1352
|
||
|
|
56b19120f4
|
||
|
|
546b8c4a2d
|
||
|
|
8d8e08d94f
|
||
|
|
0ea2dadea4
|
||
|
|
006d631682
|
||
|
|
669129d919
|
||
|
|
961c64e422
|
||
|
|
c22bd73759
|
||
|
|
0ac52b23d4
|
||
|
|
88ce35930c
|
||
|
|
01ccd1817d
|
||
|
|
b80d71e95e
|
||
|
|
d07316990e
|
||
|
|
327bd919b9
|
||
|
|
a32725504d
|
||
|
|
f0b578bdb8
|
||
|
|
641d467069
|
||
|
|
f71af57288
|
||
|
|
085f8f1134
|
||
|
|
50d0dbef79
|
||
|
|
914ec8cfb9
|
||
|
|
769687bca6
|
||
|
|
f9ee0c92cb
|
||
|
|
1a9d695374
|
@@ -117,6 +117,3 @@ dmypy.json
|
||||
|
||||
# Temporary Vim files
|
||||
.*.swp
|
||||
|
||||
# config file
|
||||
/config.py
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# pyinotifyd
|
||||
A daemon to monitor filesystems events with inotify on Linux and run tasks like filesystem operations (copy, move or delete), a shell command or custom async python methods.
|
||||
A daemon for monitoring filesystem events with inotify on Linux and run tasks like filesystem operations (copy, move or delete), a shell command or custom async python methods.
|
||||
|
||||
It is possible to schedule tasks with a delay, which can then be canceled again in case a canceling event occurs. A useful example for this is to run tasks only if a file has not changed within a certain amount of time.
|
||||
|
||||
@@ -49,7 +49,7 @@ The basic idea is to instantiate one or multiple schedulers and map specific ino
|
||||
pyinotifyd has different schedulers to schedule tasks with an optional delay. The advantages of using a scheduler are consistent logging and the possibility to cancel delayed tasks. Furthermore, schedulers have the ability to differentiate between files and directories.
|
||||
|
||||
### TaskScheduler
|
||||
Schedule a custom python method *job* with an optional *delay* in seconds. Skip scheduling of tasks for files and/or directories according to *files* and *dirs* arguments. If there already is a scheduled task, re-schedule it with *delay*. Use *logname* in log messages.
|
||||
Schedule a custom python method *job* with an optional *delay* in seconds. Skip scheduling of tasks for files and/or directories according to *files* and *dirs* arguments. If there already is a scheduled task, re-schedule it with *delay*. Use *logname* in log messages. All additional modules, functions and variables that are defined in the config file and are needed within the *job*, need to be passed as dictionary to the TaskManager through *global_vars*. If you want to limit the scheduler to run only one job at a time, set *singlejob* to True.
|
||||
All arguments except for *job* are optional.
|
||||
```python
|
||||
# Please note that pyinotifyd uses pythons asyncio for asynchronous task execution.
|
||||
@@ -58,6 +58,9 @@ All arguments except for *job* are optional.
|
||||
# Bad: time.sleep(10)
|
||||
# Good: await asyncio.sleep(10)
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
async def custom_job(event, task_id):
|
||||
await asyncio.sleep(10)
|
||||
logging.info(f"{task_id}: execute example task: {event}")
|
||||
@@ -67,7 +70,9 @@ task_sched = TaskScheduler(
|
||||
files=True,
|
||||
dirs=False,
|
||||
delay=0,
|
||||
logname="sched")
|
||||
logname="sched",
|
||||
global_vars=globals(),
|
||||
singlejob=False)
|
||||
```
|
||||
|
||||
### ShellScheduler
|
||||
@@ -160,21 +165,23 @@ pyinotifyd = Pyinotifyd(
|
||||
```
|
||||
|
||||
### Watches
|
||||
A watch connects the *path* to an *event_map*. Automatically add a watch on each sub-directories in *path* if *rec* is set to True. If *auto_add* is True, a watch will be added automatically on newly created sub-directories in *path*.
|
||||
A watch connects the *path* to an *event_map*. Automatically add a watch on each sub-directories in *path* if *rec* is set to True. If *auto_add* is True, a watch will be added automatically on newly created sub-directories in *path*. All events for paths matching one of the regular expressions in *exclude_filter* are ignored. If the value of *exclude_filter* is a string, it is assumed to be a path to a file from which the list of regular expressions will be read.
|
||||
```python
|
||||
# Add a watch directly to Pyinotifyd.
|
||||
pyinotifyd.add_watch(
|
||||
path="/src_path",
|
||||
event_map=event_map,
|
||||
rec=False,
|
||||
auto_add=False)
|
||||
auto_add=False,
|
||||
exclude_filter=["^/src_path/subpath$"])
|
||||
|
||||
# Or instantiate and add it
|
||||
w = Watch(
|
||||
path="/src_path",
|
||||
event_map=event_map,
|
||||
rec=False,
|
||||
auto_add=False)
|
||||
auto_add=False,
|
||||
exclude_filter=["^/src_path/subpath$"])
|
||||
|
||||
pyinotifyd.add_watch(watch=w)
|
||||
```
|
||||
@@ -210,6 +217,8 @@ enableSyslog(lglevel=INFO, name="daemon")
|
||||
|
||||
## Schedule python method for all events on files and directories
|
||||
```python
|
||||
import logging
|
||||
|
||||
async def custom_job(event, task_id):
|
||||
logging.info(f"{task_id}: execute example task: {event}")
|
||||
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
# Copyright 2020 Gentoo Authors
|
||||
# Distributed under the terms of the GNU General Public License v2
|
||||
|
||||
EAPI=7
|
||||
PYTHON_COMPAT=( python3_{7,8,9} )
|
||||
DISTUTILS_USE_SETUPTOOLS=rdepend
|
||||
|
||||
SCM=""
|
||||
if [ "${PV#9999}" != "${PV}" ] ; then
|
||||
SCM="git-r3"
|
||||
EGIT_REPO_URI="https://github.com/spacefreak86/${PN}"
|
||||
fi
|
||||
|
||||
inherit ${SCM} distutils-r1 systemd
|
||||
|
||||
DESCRIPTION="Monitore filesystems events and execute Python methods or Shell commands."
|
||||
HOMEPAGE="https://github.com/spacefreak86/pymodmilter"
|
||||
if [ "${PV#9999}" != "${PV}" ] ; then
|
||||
SRC_URI=""
|
||||
KEYWORDS=""
|
||||
# Needed for tests
|
||||
S="${WORKDIR}/${PN}"
|
||||
EGIT_CHECKOUT_DIR="${S}"
|
||||
else
|
||||
SRC_URI="https://github.com/spacefreak86/${PN}/archive/${PV}.tar.gz -> ${P}.tar.gz"
|
||||
KEYWORDS="amd64 x86"
|
||||
fi
|
||||
|
||||
LICENSE="GPL-3"
|
||||
SLOT="0"
|
||||
|
||||
IUSE="systemd"
|
||||
|
||||
RDEPEND="dev-python/pyinotify[${PYTHON_USEDEP}]"
|
||||
|
||||
python_install_all() {
|
||||
distutils-r1_python_install_all
|
||||
|
||||
dodir /etc/${PN}
|
||||
insinto /etc/${PN}
|
||||
newins ${PN}/misc/config.py.default config.py
|
||||
|
||||
use systemd && systemd_dounit ${PN}/misc/${PN}.service
|
||||
|
||||
newinitd ${PN}/misc/openrc/${PN}.initd ${PN}
|
||||
newconfd ${PN}/misc/openrc/${PN}.confd ${PN}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
# Distributed under the terms of the GNU General Public License v2
|
||||
|
||||
EAPI=7
|
||||
PYTHON_COMPAT=( python3_{7,8,9} )
|
||||
PYTHON_COMPAT=( python3_{8..11} )
|
||||
DISTUTILS_USE_SETUPTOOLS=rdepend
|
||||
|
||||
SCM=""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
set -x
|
||||
PYTHON=$(which python)
|
||||
|
||||
script_dir=$(dirname "$(readlink -f -- "$BASH_SOURCE")")
|
||||
|
||||
@@ -7,6 +7,7 @@ script_dir=$(dirname "$(readlink -f -- "$BASH_SOURCE")")
|
||||
pkg_dir=$(realpath "${script_dir}/../..")
|
||||
|
||||
cd "${pkg_dir}/dist"
|
||||
ls -la
|
||||
msg="Select version to distribute (cancel with CTRL+C):"
|
||||
echo "${msg}"
|
||||
select version in $(find . -maxdepth 1 -type f -name "pyinotifyd-*.*.*.tar.gz" -printf "%f\n" | sed "s#\.tar\.gz##g"); do
|
||||
|
||||
Executable → Regular
+56
-35
@@ -31,12 +31,12 @@ import pyinotify
|
||||
import signal
|
||||
import sys
|
||||
|
||||
from pyinotify import ProcessEvent
|
||||
from pyinotify import ProcessEvent, ExcludeFilter
|
||||
|
||||
from pyinotifyd._install import install, uninstall
|
||||
from pyinotifyd.scheduler import TaskScheduler, Cancel
|
||||
|
||||
__version__ = "0.0.5"
|
||||
__version__ = "0.0.9"
|
||||
|
||||
|
||||
def setLoglevel(loglevel, logname=None):
|
||||
@@ -56,16 +56,15 @@ def enableSyslog(loglevel=None, address="/dev/log", logname=None):
|
||||
|
||||
|
||||
class _SchedulerList:
|
||||
def __init__(self, schedulers=[], loop=None):
|
||||
def __init__(self, schedulers=[]):
|
||||
if not isinstance(schedulers, list):
|
||||
schedulers = [schedulers]
|
||||
|
||||
self._schedulers = schedulers
|
||||
self._loop = (loop or asyncio.get_event_loop())
|
||||
|
||||
def process_event(self, event):
|
||||
for scheduler in self._schedulers:
|
||||
self._loop.create_task(scheduler.process_event(event))
|
||||
asyncio.create_task(scheduler.process_event(event))
|
||||
|
||||
def schedulers(self):
|
||||
return self._schedulers
|
||||
@@ -76,10 +75,10 @@ class EventMap(ProcessEvent):
|
||||
**pyinotify.EventsCodes.OP_FLAGS,
|
||||
**pyinotify.EventsCodes.EVENT_FLAGS}
|
||||
|
||||
def my_init(self, event_map=None, default_sched=None, loop=None,
|
||||
def my_init(self, event_map=None, default_sched=None, exclude_filter=None,
|
||||
logname="eventmap"):
|
||||
self._map = {}
|
||||
self._loop = (loop or asyncio.get_event_loop())
|
||||
self._exclude_filter = None
|
||||
|
||||
if default_sched is not None:
|
||||
for flag in EventMap.flags:
|
||||
@@ -91,6 +90,7 @@ class EventMap(ProcessEvent):
|
||||
for flag, schedulers in event_map.items():
|
||||
self.set_scheduler(flag, schedulers)
|
||||
|
||||
self.set_exclude_filter(exclude_filter)
|
||||
self._log = logging.getLogger((logname or __name__))
|
||||
|
||||
def set_scheduler(self, flag, schedulers):
|
||||
@@ -106,27 +106,43 @@ class EventMap(ProcessEvent):
|
||||
isinstance(scheduler, Cancel):
|
||||
instances.append(scheduler)
|
||||
else:
|
||||
instances.append(
|
||||
TaskScheduler(scheduler, loop=self._loop))
|
||||
instances.append(TaskScheduler(scheduler))
|
||||
|
||||
self._map[flag] = _SchedulerList(instances, loop=self._loop)
|
||||
self._map[flag] = _SchedulerList(instances)
|
||||
|
||||
elif flag in self._map:
|
||||
del self._map[flag]
|
||||
|
||||
def set_exclude_filter(self, exclude_filter):
|
||||
if exclude_filter is None:
|
||||
self._exclude_filter = None
|
||||
return
|
||||
|
||||
if not isinstance(exclude_filter, ExcludeFilter):
|
||||
self._exclude_filter = ExcludeFilter(exclude_filter)
|
||||
else:
|
||||
self._exclude_filter = exclude_filter
|
||||
|
||||
def process_default(self, event):
|
||||
msg = "received event"
|
||||
attrs = ""
|
||||
for attr in [
|
||||
"dir", "mask", "maskname", "pathname", "src_pathname", "wd"]:
|
||||
value = getattr(event, attr, None)
|
||||
if attr == "mask":
|
||||
value = hex(value)
|
||||
if value:
|
||||
msg += f", {attr}={value}"
|
||||
attrs += f", {attr}={value}"
|
||||
|
||||
self._log.debug(msg)
|
||||
self._log.debug(f"received event{attrs}")
|
||||
maskname = event.maskname.split("|")[0]
|
||||
if maskname in self._map:
|
||||
|
||||
if maskname not in self._map:
|
||||
return
|
||||
|
||||
if self._exclude_filter and self._exclude_filter(event.pathname):
|
||||
self._log.debug(f"pathname {event.pathname} is excluded")
|
||||
return
|
||||
|
||||
self._map[maskname].process_event(event)
|
||||
|
||||
def schedulers(self):
|
||||
@@ -139,23 +155,32 @@ class EventMap(ProcessEvent):
|
||||
|
||||
|
||||
class Watch:
|
||||
def __init__(self, path, event_map=None, default_sched=None, rec=False,
|
||||
auto_add=False, logname="watch", loop=None):
|
||||
assert isinstance(path, str), \
|
||||
f"path: expected {type('')}, got {type(path)}"
|
||||
def __init__(self, path, event_map=None, default_sched=None,
|
||||
rec=False, auto_add=False, exclude_filter=None,
|
||||
logname="watch"):
|
||||
assert (isinstance(path, str) or isinstance(path, list)), \
|
||||
f"path: expected {type('')} or {type([])}, got {type(path)}"
|
||||
|
||||
if isinstance(event_map, EventMap):
|
||||
self._event_map = event_map
|
||||
else:
|
||||
self._event_map = EventMap(
|
||||
event_map=event_map, default_sched=default_sched)
|
||||
event_map=event_map, default_sched=default_sched,
|
||||
exclude_filter=exclude_filter)
|
||||
|
||||
assert isinstance(rec, bool), \
|
||||
f"rec: expected {type(bool)}, got {type(rec)}"
|
||||
assert isinstance(auto_add, bool), \
|
||||
f"auto_add: expected {type(bool)}, got {type(auto_add)}"
|
||||
|
||||
self._exclude_filter = None
|
||||
if exclude_filter:
|
||||
if not isinstance(exclude_filter, ExcludeFilter):
|
||||
self._exclude_filter = ExcludeFilter(exclude_filter)
|
||||
else:
|
||||
self._exclude_filter = exclude_filter
|
||||
|
||||
logname = (logname or __name__)
|
||||
self._loop = loop
|
||||
|
||||
self._path = path
|
||||
self._rec = rec
|
||||
@@ -171,14 +196,14 @@ class Watch:
|
||||
def event_map(self):
|
||||
return self._event_map
|
||||
|
||||
def start(self, loop=None):
|
||||
loop = (loop or self._loop)
|
||||
def start(self):
|
||||
self._watch_manager.add_watch(self._path, pyinotify.ALL_EVENTS,
|
||||
rec=self._rec, auto_add=self._auto_add,
|
||||
exclude_filter=self._exclude_filter,
|
||||
do_glob=True)
|
||||
|
||||
self._notifier = pyinotify.AsyncioNotifier(
|
||||
self._watch_manager, loop, default_proc_fun=self._event_map)
|
||||
self._watch_manager, asyncio.get_event_loop(), default_proc_fun=self._event_map)
|
||||
|
||||
def stop(self):
|
||||
self._notifier.stop()
|
||||
@@ -189,12 +214,10 @@ class Watch:
|
||||
class Pyinotifyd:
|
||||
name = "pyinotifyd"
|
||||
|
||||
def __init__(self, watches=[], shutdown_timeout=30, logname="daemon",
|
||||
loop=None):
|
||||
def __init__(self, watches=[], shutdown_timeout=30, logname="daemon"):
|
||||
self.set_watches(watches)
|
||||
self.set_shutdown_timeout(shutdown_timeout)
|
||||
logname = (logname or __name__)
|
||||
self._loop = (loop or asyncio.get_event_loop())
|
||||
|
||||
self._log = logging.getLogger(logname)
|
||||
|
||||
@@ -203,12 +226,12 @@ class Pyinotifyd:
|
||||
config = {}
|
||||
name = Pyinotifyd.name
|
||||
exec("from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL",
|
||||
{}, config)
|
||||
exec(f"from {name} import Pyinotifyd, Watch", {}, config)
|
||||
exec(f"from {name} import setLoglevel, enableSyslog", {}, config)
|
||||
exec(f"from {name}.scheduler import *", {}, config)
|
||||
config)
|
||||
exec(f"from {name} import Pyinotifyd, Watch", config)
|
||||
exec(f"from {name} import setLoglevel, enableSyslog", config)
|
||||
exec(f"from {name}.scheduler import *", config)
|
||||
with open(config_file, "r") as fh:
|
||||
exec(fh.read(), {}, config)
|
||||
exec(fh.read(), config)
|
||||
instance = config[f"{name}"]
|
||||
assert isinstance(instance, Pyinotifyd), \
|
||||
f"{name}: expected {type(Pyinotifyd)}, " \
|
||||
@@ -246,9 +269,7 @@ class Pyinotifyd:
|
||||
schedulers.extend(w.event_map().schedulers())
|
||||
return list(set(schedulers))
|
||||
|
||||
def start(self, loop=None):
|
||||
loop = (loop or self._loop)
|
||||
|
||||
def start(self):
|
||||
if len(self._watches) == 0:
|
||||
self._log.warning(
|
||||
"no watches configured, the daemon will not do anything")
|
||||
@@ -256,7 +277,7 @@ class Pyinotifyd:
|
||||
for watch in self._watches:
|
||||
self._log.info(
|
||||
f"start listening for inotify events on '{watch.path()}'")
|
||||
watch.start(loop)
|
||||
watch.start()
|
||||
|
||||
def pause(self):
|
||||
for scheduler in self.schedulers():
|
||||
|
||||
+12
-3
@@ -21,14 +21,19 @@ import shutil
|
||||
import sys
|
||||
|
||||
|
||||
SYSTEMD_PATH = "/lib/systemd/system"
|
||||
SYSTEMD_PATHS = ["/lib/systemd/system", "/usr/lib/systemd/system"]
|
||||
|
||||
OPENRC = "/sbin/openrc"
|
||||
|
||||
|
||||
def _systemd_files(pkg_dir, name):
|
||||
for path in SYSTEMD_PATHS:
|
||||
if os.path.isdir(path):
|
||||
break
|
||||
|
||||
return [
|
||||
(f"{pkg_dir}/misc/systemd/{name}.service",
|
||||
f"{SYSTEMD_PATH}/{name}.service", True)]
|
||||
f"{path}/{name}.service", True)]
|
||||
|
||||
|
||||
def _openrc_files(pkg_dir, name):
|
||||
@@ -113,7 +118,11 @@ def _check_root():
|
||||
|
||||
|
||||
def _check_systemd():
|
||||
systemd = os.path.isdir(SYSTEMD_PATH)
|
||||
for path in SYSTEMD_PATHS:
|
||||
systemd = os.path.isdir(path)
|
||||
if systemd:
|
||||
break
|
||||
|
||||
if systemd:
|
||||
logging.info("systemd detected")
|
||||
|
||||
|
||||
@@ -2,14 +2,20 @@
|
||||
# TaskScheduler config #
|
||||
##########################
|
||||
|
||||
#import asyncio
|
||||
#import logging
|
||||
#
|
||||
#async def custom_job(event, task_id):
|
||||
# await asyncio.sleep(1)
|
||||
# logging.info(f"{task_id}: execute example task: {event}")
|
||||
#
|
||||
#task_sched = TaskScheduler(
|
||||
# job=custom_job,
|
||||
# files=True,
|
||||
# dirs=False,
|
||||
# delay=10)
|
||||
# delay=10,
|
||||
# global_vars=globals(),
|
||||
# singlejob=False)
|
||||
|
||||
|
||||
###########################
|
||||
@@ -20,7 +26,8 @@
|
||||
# cmd="/usr/local/bin/task.sh {maskname} {pathname} {src_pathname}",
|
||||
# files=True,
|
||||
# dirs=False,
|
||||
# delay=10)
|
||||
# delay=10,
|
||||
# singlejob=False)
|
||||
|
||||
|
||||
#################################
|
||||
@@ -81,7 +88,8 @@
|
||||
# path="/watched/directory",
|
||||
# event_map = event_map,
|
||||
# rec=True,
|
||||
# auto_add=True)
|
||||
# auto_add=True,
|
||||
# exclude_filter=["^/watched/directory/subpath$"])
|
||||
|
||||
|
||||
################
|
||||
|
||||
@@ -13,11 +13,6 @@ retry="SIGTERM/${shutdown_timeout}"
|
||||
|
||||
extra_commands="configtest reload"
|
||||
|
||||
depend() {
|
||||
need net
|
||||
before mta
|
||||
}
|
||||
|
||||
checkconfig() {
|
||||
output=$(${command} ${command_args} -t 2>&1)
|
||||
ret=$?
|
||||
|
||||
Executable → Regular
+38
-23
@@ -46,13 +46,13 @@ class SchedulerLogger(logging.LoggerAdapter):
|
||||
class TaskScheduler:
|
||||
|
||||
class TaskState:
|
||||
def __init__(self, id=None, task=None, cancelable=True):
|
||||
self.id = id or str(uuid4())
|
||||
def __init__(self, task_id=None, task=None, cancelable=True):
|
||||
self.id = task_id or str(uuid4())
|
||||
self.task = task
|
||||
self.cancelable = cancelable
|
||||
|
||||
def __init__(self, job, files=True, dirs=False, delay=0, logname="sched",
|
||||
loop=None):
|
||||
global_vars={}, singlejob=False):
|
||||
assert iscoroutinefunction(job), \
|
||||
f"job: expected coroutine, got {type(job)}"
|
||||
assert isinstance(files, bool), \
|
||||
@@ -61,14 +61,16 @@ class TaskScheduler:
|
||||
f"dirs: expected {type(bool)}, got {type(dirs)}"
|
||||
assert isinstance(delay, int), \
|
||||
f"delay: expected {type(int)}, got {type(delay)}"
|
||||
assert isinstance(global_vars, dict), \
|
||||
f"global_vars: expected {type(dict)}, got {type(global_vars)}"
|
||||
|
||||
self._job = job
|
||||
self._files = files
|
||||
self._dirs = dirs
|
||||
self._delay = delay
|
||||
self._log = logging.getLogger((logname or __name__))
|
||||
self._loop = (loop or asyncio.get_event_loop())
|
||||
|
||||
self._globals = global_vars
|
||||
self._singlejob = singlejob
|
||||
self._tasks = {}
|
||||
self._pause = False
|
||||
|
||||
@@ -88,8 +90,7 @@ class TaskScheduler:
|
||||
self._log.info(
|
||||
f"wait {timeout} seconds for {len(pending)} "
|
||||
f"remaining task(s) to complete")
|
||||
done, pending = await asyncio.wait([*pending], timeout=timeout,
|
||||
loop=self._loop)
|
||||
done, pending = await asyncio.wait([*pending], timeout=timeout)
|
||||
if pending:
|
||||
self._log.warning(
|
||||
f"shutdown timeout exceeded, "
|
||||
@@ -97,20 +98,23 @@ class TaskScheduler:
|
||||
for task in pending:
|
||||
task.cancel()
|
||||
try:
|
||||
await asyncio.gather(*pending, loop=self._loop)
|
||||
await asyncio.gather(*pending)
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
else:
|
||||
self._log.info("all remainig tasks completed")
|
||||
|
||||
def taskindex(self, event):
|
||||
return "singlejob" if self._singlejob else event.pathname
|
||||
|
||||
async def _run_job(self, event, task_state, restart=False):
|
||||
logger = SchedulerLogger(self._log, {
|
||||
"event": event,
|
||||
"id": task_state.id})
|
||||
|
||||
if self._delay > 0:
|
||||
task_state.task = self._loop.create_task(
|
||||
asyncio.sleep(self._delay, loop=self._loop))
|
||||
task_state.task = asyncio.create_task(
|
||||
asyncio.sleep(self._delay))
|
||||
try:
|
||||
if restart:
|
||||
prefix = "re-"
|
||||
@@ -124,8 +128,15 @@ class TaskScheduler:
|
||||
return
|
||||
|
||||
logger.info("start task")
|
||||
if self._globals:
|
||||
local_vars = {"self": self,
|
||||
"event": event,
|
||||
"task_id": task_state.id}
|
||||
task_state.task = asyncio.create_task(
|
||||
eval("self._job(event, task_id)", self._globals, local_vars))
|
||||
|
||||
task_state.task = self._loop.create_task(
|
||||
else:
|
||||
task_state.task = asyncio.create_task(
|
||||
self._job(event, task_state.id))
|
||||
|
||||
try:
|
||||
@@ -134,9 +145,10 @@ class TaskScheduler:
|
||||
except asyncio.CancelledError:
|
||||
logger.warning("ongoing task cancelled")
|
||||
else:
|
||||
self._log.info("task finished")
|
||||
logger.info("task finished")
|
||||
finally:
|
||||
del self._tasks[event.pathname]
|
||||
task_index = self.taskindex(event)
|
||||
del self._tasks[task_index]
|
||||
|
||||
async def process_event(self, event):
|
||||
if not ((not event.dir and self._files) or
|
||||
@@ -144,9 +156,13 @@ class TaskScheduler:
|
||||
return
|
||||
|
||||
restart = False
|
||||
task_index = self.taskindex(event)
|
||||
try:
|
||||
task_state = self._tasks[event.pathname]
|
||||
|
||||
task_state = self._tasks[task_index]
|
||||
except KeyError:
|
||||
task_state = TaskScheduler.TaskState()
|
||||
self._tasks[task_index] = task_state
|
||||
else:
|
||||
logger = SchedulerLogger(self._log, {
|
||||
"event": event,
|
||||
"id": task_state.id})
|
||||
@@ -162,16 +178,13 @@ class TaskScheduler:
|
||||
logger.warning("skip event due to ongoing task")
|
||||
return
|
||||
|
||||
except KeyError:
|
||||
task_state = TaskScheduler.TaskState()
|
||||
self._tasks[event.pathname] = task_state
|
||||
|
||||
if not self._pause:
|
||||
await self._run_job(event, task_state, restart)
|
||||
|
||||
async def process_cancel_event(self, event):
|
||||
try:
|
||||
task_state = self._tasks[event.pathname]
|
||||
task_index = self.taskindex(event)
|
||||
task_state = self._tasks[task_index]
|
||||
except KeyError:
|
||||
return
|
||||
|
||||
@@ -183,7 +196,8 @@ class TaskScheduler:
|
||||
task_state.task.cancel()
|
||||
logger.info("scheduled task cancelled")
|
||||
task_state.task = None
|
||||
del self._tasks[event.pathname]
|
||||
logger.info(f"{task_index}")
|
||||
del self._tasks[task_index]
|
||||
else:
|
||||
logger.warning("skip event due to ongoing task")
|
||||
|
||||
@@ -228,7 +242,7 @@ class ShellScheduler(TaskScheduler):
|
||||
|
||||
logger.info(f"execute shell command, cmd={cmd}")
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_shell(cmd, loop=self._loop)
|
||||
proc = await asyncio.create_subprocess_shell(cmd)
|
||||
await proc.communicate()
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
@@ -276,7 +290,8 @@ class FileManagerRule:
|
||||
|
||||
class FileManagerScheduler(TaskScheduler):
|
||||
def __init__(self, rules, job=None, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs, job=self._manager_job)
|
||||
super().__init__(
|
||||
*args, **kwargs, job=self._manager_job, singlejob=False)
|
||||
|
||||
if not isinstance(rules, list):
|
||||
rules = [rules]
|
||||
|
||||
@@ -18,7 +18,7 @@ setup(name = "pyinotifyd",
|
||||
# 3 - Alpha
|
||||
# 4 - Beta
|
||||
# 5 - Production/Stable
|
||||
"Development Status :: 4 - Beta",
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python",
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
import pyinotifyd
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(
|
||||
pyinotifyd.main()
|
||||
)
|
||||
Reference in New Issue
Block a user