Benjamin Bossan commited on
Commit
b38dea1
1 Parent(s): 29eecb6

Add backup functionality

Browse files
Dockerfile CHANGED
@@ -10,7 +10,7 @@ COPY src ./src
10
  RUN python3 -m pip install .
11
 
12
  COPY "demo.py" .
13
- EXPOSE 7860
14
  COPY start.sh .
15
  RUN chmod +x start.sh
16
 
 
10
  RUN python3 -m pip install .
11
 
12
  COPY "demo.py" .
13
+ EXPOSE 7860 8080
14
  COPY start.sh .
15
  RUN chmod +x start.sh
16
 
README.md CHANGED
@@ -70,6 +70,10 @@ docker run -p 7860:7860 -e GRADIO_SERVER_NAME=0.0.0.0 -v $HOME/.cache/huggingfac
70
 
71
  Note that the Hugging Face cache folder is mounted as a docker volume to make use of potentially available local model cache instead of downloading the transformers models each time the container is started. To prevent that, remove the `-v ...` parameter. The database used for storing the results is ephemeral and will be deleted when the docker container is stopped.
72
 
 
 
 
 
73
  ## Checks
74
 
75
  ### Running tests
 
70
 
71
  Note that the Hugging Face cache folder is mounted as a docker volume to make use of potentially available local model cache instead of downloading the transformers models each time the container is started. To prevent that, remove the `-v ...` parameter. The database used for storing the results is ephemeral and will be deleted when the docker container is stopped.
72
 
73
+ ### Backup
74
+
75
+ To download a backup of the backend DB, visit `localhost:8080/backup`. If you wish to start the app based on a backup, set the `DB_FILE_NAME` environment variable to the name of the backup.
76
+
77
  ## Checks
78
 
79
  ### Running tests
src/gistillery/db.py CHANGED
@@ -86,7 +86,7 @@ def namedtuple_factory(cursor, row): # type: ignore
86
  return cls._make(row)
87
 
88
 
89
- def _get_db_connection() -> sqlite3.Connection:
90
  global TABLES_CREATED
91
 
92
  # sqlite cannot deal with concurrent access, so we set a big timeout
@@ -116,7 +116,7 @@ def _get_db_connection() -> sqlite3.Connection:
116
 
117
  @contextmanager
118
  def get_db_cursor() -> Generator[sqlite3.Cursor, None, None]:
119
- conn = _get_db_connection()
120
  cursor = conn.cursor()
121
  try:
122
  yield cursor
 
86
  return cls._make(row)
87
 
88
 
89
+ def get_db_connection() -> sqlite3.Connection:
90
  global TABLES_CREATED
91
 
92
  # sqlite cannot deal with concurrent access, so we set a big timeout
 
116
 
117
  @contextmanager
118
  def get_db_cursor() -> Generator[sqlite3.Cursor, None, None]:
119
+ conn = get_db_connection()
120
  cursor = conn.cursor()
121
  try:
122
  yield cursor
src/gistillery/webservice.py CHANGED
@@ -1,11 +1,14 @@
 
1
  import logging
 
 
2
  import uuid
3
 
4
  from fastapi import FastAPI
 
5
 
6
  from gistillery.base import EntriesResult, JobStatus, JobStatusResult, RequestInput
7
- from gistillery.db import TABLES, get_db_cursor
8
-
9
 
10
  logger = logging.getLogger(__name__)
11
  logger.setLevel(logging.DEBUG)
@@ -140,3 +143,26 @@ def clear() -> str:
140
  for table_name in TABLES:
141
  cursor.execute(f"DELETE FROM {table_name}")
142
  return "OK"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime as dt
2
  import logging
3
+ import shutil
4
+ import sqlite3
5
  import uuid
6
 
7
  from fastapi import FastAPI
8
+ from fastapi.responses import FileResponse
9
 
10
  from gistillery.base import EntriesResult, JobStatus, JobStatusResult, RequestInput
11
+ from gistillery.db import TABLES, get_db_connection, get_db_cursor
 
12
 
13
  logger = logging.getLogger(__name__)
14
  logger.setLevel(logging.DEBUG)
 
143
  for table_name in TABLES:
144
  cursor.execute(f"DELETE FROM {table_name}")
145
  return "OK"
146
+
147
+
148
+ @app.get("/backup/")
149
+ def backup() -> FileResponse:
150
+ # create a backup and return it
151
+ def progress(status, remaining, total):
152
+ logger.debug(f"DB: Copied {total-remaining} of {total} pages...")
153
+
154
+ now = dt.datetime.now(dt.timezone.utc)
155
+ fname = f"sqlite-data_backup_{now.strftime('%Y-%m-%d_%H-%M-%S')}.db"
156
+
157
+ try:
158
+ conn = get_db_connection()
159
+ backup_db = sqlite3.connect(fname)
160
+ with backup_db:
161
+ conn.backup(backup_db, pages=1, progress=progress)
162
+ except Exception as e:
163
+ logger.error(f"Error creating backup: {e}")
164
+ conn.close()
165
+ backup_db.close()
166
+ raise e
167
+
168
+ return FileResponse(fname, media_type="application/octet-stream", filename=fname)
tests/test_app.py CHANGED
@@ -1,5 +1,6 @@
1
  import datetime as dt
2
  import os
 
3
  from types import SimpleNamespace
4
 
5
  import pytest
@@ -311,3 +312,22 @@ class TestWebservice:
311
  ]
312
  )
313
  assert rows[0].input == expected
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import datetime as dt
2
  import os
3
+ import sqlite3
4
  from types import SimpleNamespace
5
 
6
  import pytest
 
312
  ]
313
  )
314
  assert rows[0].input == expected
315
+
316
+ def test_backup(self, client, tmp_path):
317
+ # submit an entry, create a backup, check that the backup contains the entry
318
+ from gistillery.db import namedtuple_factory
319
+
320
+ client.post("/submit", json={"author": "Pie Test", "content": "this is a pie"})
321
+ resp = client.get("/backup")
322
+ assert resp.status_code == 200
323
+
324
+ with open(tmp_path / "backup.db", "wb") as f:
325
+ f.write(resp.content)
326
+ conn = sqlite3.connect(tmp_path / "backup.db")
327
+
328
+ conn.row_factory = namedtuple_factory
329
+ cursor = conn.cursor()
330
+ res = cursor.execute("select * from entries").fetchall()
331
+ assert len(res) == 1
332
+ assert is_roughly_now(res[0].created_at)
333
+ assert res[0].author == "Pie Test"