Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inotify #196

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/emonhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import pprint
import glob
import os
import os.path
import getpass
from collections import defaultdict
import inotify.adapters

import emonhub_setup as ehs
import emonhub_coder as ehc
Expand Down Expand Up @@ -108,13 +110,36 @@ def run(self):
# Initialise thread restart counters
restart_count = defaultdict(int)

# Watch for config file changes
inotify_setup = inotify.adapters.Inotify()
inotify_setup.add_watch(os.path.dirname(self._setup.filename))
# config file could be a symlink, so watch that for writes too
inotify_setup.add_watch(os.path.realpath(self._setup.filename))

# Until asked to stop
while not self._exit:

# Run setup and update settings if modified
self._setup.run()
if self._setup.check_settings():
self._update_settings(self._setup.settings)
events = list(inotify_setup.event_gen(yield_nones=False, timeout_s=0.2))
for (_, type_names, path, filename) in events:
self._log.debug('type_names=%s filename=%s', type_names, filename)
# If a symlink is created we have to watch that too
if 'IN_CREATE' in type_names:
try:
target = os.readlink(os.path.join(path, filename))
inotify_setup.add_watch(target)
except OSError:
pass

# We'll reload for any file changes in this directory. There is probably only one anyway.
if 'IN_CLOSE_WRITE' not in type_names and 'IN_MOVED_TO' not in type_names and 'IN_CREATE' not in type_names:
continue

self._log.info('Reloading setup because type_names=%s for filename=%s', type_names, filename)
self._setup.run()
if self._setup.check_settings():
self._update_settings(self._setup.settings)
self._log.info('Reload complete')

# Auto conf populate nodelist
if eha.auto_conf_enabled and ehc.nodelist != self._setup.settings['nodes']:
Expand Down Expand Up @@ -158,9 +183,6 @@ def run(self):
restart_count[name] += 1
self._update_settings(self._setup.settings)

# Sleep until next iteration
time.sleep(0.2)

def close(self):
"""Close hub. Do some cleanup before leaving."""

Expand Down
4 changes: 4 additions & 0 deletions src/emonhub_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def __init__(self, filename):
raise EmonHubSetupInitError(
'Configuration file error - section: ' + str(e))

@property
def filename(self):
return self._filename

def check_settings(self):
"""Check settings

Expand Down