Skip to content

Commit

Permalink
api nb.build: reload on demand, fixed timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Jul 28, 2021
1 parent f182adc commit f70e50d
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions mpcontribs-api/mpcontribs/api/notebooks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from copy import deepcopy
from bson import ObjectId
from nbformat import v4 as nbf
from urllib.parse import urlparse
from flask import Blueprint, request, current_app
from flask_mongorest import operators as ops
from flask_mongorest.methods import Fetch, BulkFetch
Expand Down Expand Up @@ -80,24 +79,19 @@ def build():
cids = request.args.get("cids", "").split(",")
projects = request.args.get("projects", "").split(",")
force = bool(request.args.get("force", 0))
port = urlparse(request.url).port
remaining_time = 295 if port else 25

if not projects[0]:
projects = [p["name"] for p in Projects.objects.only("name")]

contribs_objects = Contribs.objects(project__in=projects).only(
"id", "last_modified", "tables", "structures", "attachments", "notebook"
).order_by("-last_modified")
remaining_time = 25
query = {"project__in": projects} if projects[0] else {}
mask = ["id", "last_modified", "notebook"]
contribs_objects = Contribs.objects(**query).only(*mask).order_by("-last_modified")
documents = contribs_objects(id__in=cids) if cids[0] else contribs_objects
count = 0

for document in documents:
for idx, document in enumerate(documents):
stop = time.perf_counter()
remaining_time -= stop - start

if remaining_time < 0:
return f"{count} notebooks built"
return f"TIMEOUT (idx={idx}): {count} notebooks built"

start = time.perf_counter()

Expand All @@ -114,6 +108,10 @@ def build():
except DoesNotExist:
pass

cid = str(document.id)
print(f"prep notebook for {cid} ...")
document.reload("tables", "structures", "attachments")

cells = [
# define client only once in kernel
# avoids API calls for regex expansion for query parameters
Expand Down Expand Up @@ -166,7 +164,6 @@ def build():
]))
)

cid = str(document.id)
try:
outputs = execute_cells(cid, cells)
except Exception as e:
Expand All @@ -185,7 +182,7 @@ def build():
document.save(signal_kwargs={"skip": True})
count += 1

return f"{count} notebooks built"
return f"ALL DONE: {count} notebooks built"

# remove dangling and unset missing notebooks
# print("Count mismatch but all notebook DBRefs set -> CLEANUP")
Expand Down

0 comments on commit f70e50d

Please sign in to comment.