72 lines
2.2 KiB
Python
Executable file
72 lines
2.2 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
####################
|
|
# Inspiration / Further Reading
|
|
####################
|
|
# https://github.com/chrisjbillington/inotify_simple
|
|
# https://stackoverflow.com/questions/16148735/how-to-implement-a-watchdog-timer-in-python
|
|
|
|
####################
|
|
# Dependencies
|
|
####################
|
|
# apt install python3 python3-pip python3-dbus
|
|
# pip3 install inotify_simple
|
|
|
|
from inotify_simple import INotify, flags
|
|
import os
|
|
from threading import Timer
|
|
|
|
####################
|
|
# Simple watchdog timer class
|
|
####################
|
|
class Watchdog:
|
|
def __init__(self, timeout, userHandler=None): # timeout in seconds
|
|
self.timeout = timeout
|
|
self.handler = userHandler if userHandler is not None else self.defaultHandler
|
|
self.timer = Timer(self.timeout, self.handler)
|
|
# Do NOT start timer by default ; we want this triggered by reset events that come through
|
|
# via the inotify filesystem watcher
|
|
#self.timer.start()
|
|
|
|
def reset(self):
|
|
self.timer.cancel()
|
|
self.timer = Timer(self.timeout, self.handler)
|
|
self.timer.start()
|
|
|
|
def stop(self):
|
|
self.timer.cancel()
|
|
|
|
def defaultHandler(self):
|
|
raise self
|
|
|
|
####################
|
|
# Setup inotify on folder(s)
|
|
####################
|
|
inotify = INotify()
|
|
watch_flags = flags.CREATE | flags.DELETE
|
|
wd = inotify.add_watch('/tank/pictures', watch_flags)
|
|
|
|
####################
|
|
# Restart fim slideshow once inotify storm is over (3s timeout)
|
|
####################
|
|
def inotify_handler():
|
|
print('restarting fim slideshow')
|
|
import dbus
|
|
sysbus = dbus.SystemBus()
|
|
systemd1 = sysbus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
|
|
manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager')
|
|
job = manager.RestartUnit('fim.service', 'fail')
|
|
|
|
####################
|
|
# Watch for inotify events in an infinate loop
|
|
# This will BLOCK waiting for events within inotify.read(None)
|
|
# This makes it pretty safe for an infinate loop scenario
|
|
####################
|
|
watchdog = Watchdog(3.0, inotify_handler)
|
|
while True:
|
|
for event in inotify.read(None):
|
|
print(event)
|
|
watchdog.reset()
|
|
for flag in flags.from_mask(event.mask):
|
|
print(' ' + str(flag))
|