Pi 1 год назад
Родитель
Сommit
3811611ab8
3 измененных файлов с 20 добавлено и 2 удалено
  1. 14 2
      app/rest/Cache.py
  2. 5 0
      app/rest/cherrypy.py
  3. 1 0
      app/rest/pyapi.py

+ 14 - 2
app/rest/Cache.py

@@ -3,6 +3,7 @@
 # All rights reserved
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
+import cherrypy
 import os
 from time import time
 from typing import Dict
@@ -24,6 +25,7 @@ def delete_page(name: str, root: str = 'app/rest/static/files'):
 
 def save_page(name: str, content: bytes, tool: str, root='app/rest/static/files') -> str:
     directory = f'{root}/{name}'
+    cherrypy.log(f"saving file {directory}/{name}")
     try:
         os.mkdir(directory, mode=0o700, dir_fd=None)
     except FileExistsError:
@@ -36,11 +38,17 @@ def save_page(name: str, content: bytes, tool: str, root='app/rest/static/files'
 def get_page(name: str, root: str = 'app/rest/static/files') -> str:
     directory = f'{root}/{name}'
 
+    cherrypy.log(f"getting {name} from file")
     try:
         mtime = os.stat(f'{directory}/{name}.file').st_mtime
     except:
         mtime = None
     
+    if mtime is None:
+        cherrypy.log(f"no file")
+        return None
+    cherrypy.log(f"mtime: {mtime}")
+    cherrypy.log(f"mtime stale: {mtime and time() - mtime > STALE}")
     if mtime and time() - mtime > STALE:
         delete_page(name)
         return None
@@ -97,10 +105,14 @@ class Cache:
     def get(self, key: str) -> CachedLoadingPage:
         key = key_to_hash(key)
 
+        cherrypy.log(f"getting {key} from cache")
         if key not in self._cache:
+            cherrypy.log(f"getting {key} from file")
             try:
                 existing = get_page(hash_to_base32(key))
             except:
+                existing = None
+            if existing is None:
                 return None
             
             return self.add(key, CachedLoadingPage(existing, lambda q: q.put(None), incremental=True))
@@ -113,13 +125,13 @@ class Cache:
 
         if not page.loaded:
             page.update()
-            if not page.loaded:
-                return page
 
         if page.loaded:
             try:
                 existing = get_page(hash_to_base32(key))
             except:
+                existing = None
+            if existing is None:
                 content = ''.join(page.value) if isinstance(page.value, list) else page.value
                 save_page(hash_to_base32(key), content.encode('utf-8'), tool='grocery')
 

+ 5 - 0
app/rest/cherrypy.py

@@ -4,9 +4,14 @@ from .pyapi import *
 
 application = bottle.default_app()
 
+cherrypy.config.update({'environment' : 'staging'})
 cherrypy.config.update({
     'server.socket_host': "0.0.0.0",
     'server.socket_port': 6772,
+    'engine.autoreload.on': True,
+    'request.show_tracebacks': True,
+    'request.show_mismatched_params': True,
+    'log.screen': True,
 })
 
 cherrypy.tree.graft(application, "/")

+ 1 - 0
app/rest/pyapi.py

@@ -4,6 +4,7 @@
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
 import os
+import cherrypy
 from threading import Thread
 from typing import Union
 from bottle import (