mirror of
https://github.com/bbilly1/tilefy.git
synced 2024-08-02 16:03:34 +00:00
watch for changes in tiles.yml
This commit is contained in:
parent
9a16fde0d3
commit
86947a1f9e
@ -6,6 +6,7 @@ from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from apscheduler.triggers.cron import CronTrigger
|
||||
from src.template import create_single_tile
|
||||
from src.tilefy_redis import TilefyRedis
|
||||
from src.watcher import watch_yml
|
||||
|
||||
|
||||
class TilefyScheduler:
|
||||
@ -29,6 +30,7 @@ class TilefyScheduler:
|
||||
self.add_job_store()
|
||||
jobs = self.build_jobs()
|
||||
self.add_jobs(jobs)
|
||||
self.add_watcher()
|
||||
|
||||
self.scheduler.start()
|
||||
|
||||
@ -73,3 +75,13 @@ class TilefyScheduler:
|
||||
replace_existing=True,
|
||||
)
|
||||
print(f"{job_name}: Add job {cron_tab}")
|
||||
|
||||
def add_watcher(self):
|
||||
"""add watcher to jobs"""
|
||||
self.scheduler.add_job(
|
||||
watch_yml,
|
||||
"interval",
|
||||
seconds=5,
|
||||
id="watcher",
|
||||
replace_existing=True,
|
||||
)
|
||||
|
@ -41,6 +41,10 @@ class TilefyRedis(RedisBase):
|
||||
|
||||
return False
|
||||
|
||||
def del_message(self, key):
|
||||
"""delete message from redis"""
|
||||
self.conn.execute_command("JSON.DEL", self.NAME_SPACE + key)
|
||||
|
||||
|
||||
def load_yml():
|
||||
"""read yml file"""
|
||||
|
76
tilefy/src/watcher.py
Normal file
76
tilefy/src/watcher.py
Normal file
@ -0,0 +1,76 @@
|
||||
"""watch for changes in tiles.yml"""
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
|
||||
from src.template import create_all_tiles
|
||||
from src.tilefy_redis import TilefyRedis
|
||||
|
||||
|
||||
class Watcher:
|
||||
"""watch for changes"""
|
||||
|
||||
FILE_PATH = "/data/tiles.yml"
|
||||
|
||||
def __init__(self):
|
||||
self.modified = False
|
||||
self.hash = False
|
||||
|
||||
def watch(self):
|
||||
"""watch for changes, call from schedule"""
|
||||
modified = self.is_changed()
|
||||
if modified:
|
||||
print(f"{self.FILE_PATH}: modified")
|
||||
create_all_tiles()
|
||||
self._store_last()
|
||||
|
||||
def is_changed(self):
|
||||
"""check if file has changed"""
|
||||
self._get_modified()
|
||||
last = self._get_last()
|
||||
if not last or not self.modified:
|
||||
print("create first modified entry")
|
||||
self._get_hash()
|
||||
self._store_last()
|
||||
return False
|
||||
|
||||
if self.modified == last["modified"]:
|
||||
return False
|
||||
|
||||
self._get_hash()
|
||||
if self.hash != last["hash"]:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def _get_modified(self):
|
||||
"""get last modified timestamp"""
|
||||
self.modified = int(os.stat(self.FILE_PATH).st_mtime)
|
||||
|
||||
def _get_hash(self):
|
||||
"""get hash of file content"""
|
||||
with open(self.FILE_PATH, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
self.hash = hashlib.sha1(content.encode()).hexdigest()
|
||||
|
||||
def _store_last(self):
|
||||
"""store last access details in redis"""
|
||||
message = {
|
||||
"modified": self.modified,
|
||||
"hash": self.hash,
|
||||
}
|
||||
TilefyRedis().set_message("modified", message)
|
||||
|
||||
def _get_last(self):
|
||||
"""get last stored"""
|
||||
return TilefyRedis().get_message("modified")
|
||||
|
||||
def _del_last(self):
|
||||
"""remove last item from redis"""
|
||||
TilefyRedis().del_message("modified")
|
||||
|
||||
|
||||
def watch_yml():
|
||||
"""watch tiles.yml for changes"""
|
||||
Watcher().watch()
|
Loading…
Reference in New Issue
Block a user