-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* /admin/restart => /admin/operations * Add a link to download the database * Add a SQL editor to query and modify the database in a pinch
- Loading branch information
Showing
5 changed files
with
114 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import asyncio | ||
import os | ||
import signal | ||
|
||
from hrepr import H | ||
from starbear import Queue | ||
|
||
from ...config import papconf | ||
from ..common import mila_template | ||
|
||
|
||
@mila_template(help="/help#operations") | ||
async def app(page, box): | ||
"""Admin operations.""" | ||
q = Queue() | ||
|
||
box.print(H.p(H.button("Restart server", onclick=q.tag("restart")))) | ||
box.print(H.p(H.a("Download database", href=papconf.paths.database))) | ||
|
||
async for event in q: | ||
match event.tag: | ||
case "restart": | ||
box.set("Restarting. Try to refresh in a few seconds.") | ||
await asyncio.sleep( | ||
0 | ||
) # Make sure we send the feedback before the kill() | ||
try: | ||
os.kill(os.getpid(), signal.SIGTERM) | ||
except Exception as exc: | ||
box.print(H.div["error"]("An error occurred")) | ||
box.print(H.div["error"](exc)) | ||
|
||
|
||
ROUTES = app |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from hrepr import H | ||
import sqlalchemy | ||
from starbear import Queue | ||
|
||
from ...config import papconf | ||
from ..common import mila_template | ||
|
||
|
||
@mila_template(help="/help#sql") | ||
async def app(page, box): | ||
"""Query and manipulate the database.""" | ||
q = Queue() | ||
|
||
form = H.form["sql-edit"]( | ||
H.div(H.textarea(name="sql")), | ||
H.div(H.button("Run")), | ||
onsubmit=q.wrap(form=True), | ||
) | ||
|
||
box.print(H.div(form)) | ||
box.print(result_area := H.div().autoid()) | ||
|
||
with papconf.database as db: | ||
async for event in q: | ||
try: | ||
result = db.session.execute(event["sql"]) | ||
except Exception as exc: | ||
page[result_area].set(exc) | ||
continue | ||
|
||
try: | ||
t = H.table["sql-results"]() | ||
t = t(H.tr(H.th(row_name) for row_name in result.keys())) | ||
t = t( | ||
H.tr( | ||
H.td( | ||
H.code(value.hex()) | ||
if isinstance(value, bytes) | ||
else str(value) | ||
) | ||
for value in row | ||
) | ||
for row in result | ||
) | ||
page[result_area].set(t) | ||
|
||
except sqlalchemy.exc.ResourceClosedError: | ||
# Happens when we try to iterate over DELETE results | ||
page[result_area].set("done") | ||
|
||
|
||
ROUTES = app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters