mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Run scan_dirs in a thread every 10 seconds
This commit is contained in:
parent
55e633e09f
commit
f2fd7d20bf
1 changed files with 24 additions and 14 deletions
|
|
@ -7,6 +7,7 @@ import os
|
|||
import re
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import traceback
|
||||
import urllib.parse
|
||||
from concurrent import futures
|
||||
|
|
@ -186,8 +187,11 @@ class Datasette:
|
|||
if db.name in self.databases:
|
||||
raise Exception("Multiple files with same stem: {}".format(db.name))
|
||||
self.add_database(db.name, db)
|
||||
self.scan_dirs_executor = futures.ThreadPoolExecutor(max_workers=1)
|
||||
self.scan_dirs_executor.submit(self.scan_dirs)
|
||||
if dirs:
|
||||
self.scan_dirs_thread = threading.Thread(
|
||||
target=self.scan_dirs, name="scan-dirs", daemon=True
|
||||
)
|
||||
self.scan_dirs_thread.start()
|
||||
self.cache_headers = cache_headers
|
||||
self.cors = cors
|
||||
self._metadata = metadata or {}
|
||||
|
|
@ -225,18 +229,24 @@ class Datasette:
|
|||
|
||||
def scan_dirs(self):
|
||||
# Recurse through self.dirs looking for new SQLite DBs
|
||||
for dir in self.dirs:
|
||||
print(dir)
|
||||
for filepath in Path(dir).glob("**/*.db"):
|
||||
print(filepath)
|
||||
if is_valid_sqlite(filepath):
|
||||
self.add_database(
|
||||
str(filepath)
|
||||
.replace("../", "")
|
||||
.replace("/", "_")
|
||||
.replace(".db", ""),
|
||||
Database(self, filepath, is_mutable=True),
|
||||
)
|
||||
while True:
|
||||
current_filepaths = {
|
||||
d.path for d in list(self.databases.values()) if d.path is not None
|
||||
}
|
||||
for dir in self.dirs:
|
||||
for filepath in Path(dir).glob("**/*.db"):
|
||||
if str(filepath) in current_filepaths:
|
||||
continue
|
||||
print(filepath)
|
||||
if is_valid_sqlite(filepath):
|
||||
self.add_database(
|
||||
str(filepath)
|
||||
.replace("../", "")
|
||||
.replace("/", "_")
|
||||
.replace(".db", ""),
|
||||
Database(self, str(filepath), is_mutable=True),
|
||||
)
|
||||
time.sleep(10)
|
||||
|
||||
def config(self, key):
|
||||
return self._config.get(key, None)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue