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 re
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from concurrent import futures
|
from concurrent import futures
|
||||||
|
|
@ -186,8 +187,11 @@ class Datasette:
|
||||||
if db.name in self.databases:
|
if db.name in self.databases:
|
||||||
raise Exception("Multiple files with same stem: {}".format(db.name))
|
raise Exception("Multiple files with same stem: {}".format(db.name))
|
||||||
self.add_database(db.name, db)
|
self.add_database(db.name, db)
|
||||||
self.scan_dirs_executor = futures.ThreadPoolExecutor(max_workers=1)
|
if dirs:
|
||||||
self.scan_dirs_executor.submit(self.scan_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.cache_headers = cache_headers
|
||||||
self.cors = cors
|
self.cors = cors
|
||||||
self._metadata = metadata or {}
|
self._metadata = metadata or {}
|
||||||
|
|
@ -225,18 +229,24 @@ class Datasette:
|
||||||
|
|
||||||
def scan_dirs(self):
|
def scan_dirs(self):
|
||||||
# Recurse through self.dirs looking for new SQLite DBs
|
# Recurse through self.dirs looking for new SQLite DBs
|
||||||
for dir in self.dirs:
|
while True:
|
||||||
print(dir)
|
current_filepaths = {
|
||||||
for filepath in Path(dir).glob("**/*.db"):
|
d.path for d in list(self.databases.values()) if d.path is not None
|
||||||
print(filepath)
|
}
|
||||||
if is_valid_sqlite(filepath):
|
for dir in self.dirs:
|
||||||
self.add_database(
|
for filepath in Path(dir).glob("**/*.db"):
|
||||||
str(filepath)
|
if str(filepath) in current_filepaths:
|
||||||
.replace("../", "")
|
continue
|
||||||
.replace("/", "_")
|
print(filepath)
|
||||||
.replace(".db", ""),
|
if is_valid_sqlite(filepath):
|
||||||
Database(self, filepath, is_mutable=True),
|
self.add_database(
|
||||||
)
|
str(filepath)
|
||||||
|
.replace("../", "")
|
||||||
|
.replace("/", "_")
|
||||||
|
.replace(".db", ""),
|
||||||
|
Database(self, str(filepath), is_mutable=True),
|
||||||
|
)
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
def config(self, key):
|
def config(self, key):
|
||||||
return self._config.get(key, None)
|
return self._config.get(key, None)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue