Skip to content

Commit 2cfe735

Browse files
committed
Update visit end time information in rsyncer and database when triggering a flush, but only if the end time is greater than what is currently stored in the database
1 parent cf34251 commit 2cfe735

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/murfey/server/api/instrument.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import asyncio
44
import datetime
55
import logging
6-
import urllib
76
from pathlib import Path
87
from typing import Annotated, List, Optional
8+
from urllib.parse import quote
99

1010
import aiohttp
1111
from fastapi import APIRouter, Depends
@@ -418,7 +418,7 @@ async def update_visit_end_time(
418418
if machine_config.instrument_server_url:
419419
async with aiohttp.ClientSession() as clientsession:
420420
async with clientsession.post(
421-
f"{machine_config.instrument_server_url}{url_path_for('api.router', 'update_multigrid_controller_visit_end_time', session_id=session_id)}?end_time={urllib.parse.quote(end_time.isoformat())}",
421+
f"{machine_config.instrument_server_url}{url_path_for('api.router', 'update_multigrid_controller_visit_end_time', session_id=session_id)}?end_time={quote(end_time.isoformat())}",
422422
headers={
423423
"Authorization": f"Bearer {instrument_server_tokens[session_id]['access_token']}"
424424
},
@@ -506,16 +506,41 @@ async def restart_rsyncer(
506506
async def flush_skipped_rsyncer(
507507
session_id: MurfeySessionID, rsyncer_source: RsyncerSource, db=murfey_db
508508
):
509-
data = {}
510-
instrument_name = (
511-
db.exec(select(Session).where(Session.id == session_id)).one().instrument_name
512-
)
509+
# Load data for session
510+
session_entry = db.exec(select(Session).where(Session.id == session_id)).one()
511+
instrument_name = session_entry.instrument_name
512+
513+
# Define a new visit end time that's slightly ahead of current time
514+
new_end_time = datetime.datetime.now().replace(
515+
second=0, microsecond=0
516+
) + datetime.timedelta(minutes=5)
517+
# Update the stored visit end time if the new one exceeds it
518+
if session_entry.visit_end_time:
519+
if new_end_time > session_entry.visit_end_time:
520+
session_entry.visit_end_time = new_end_time
521+
db.add(session_entry)
522+
db.commit()
523+
524+
# Send request to flush rsyncer
525+
data: dict = {}
526+
update_result: dict = {}
513527
machine_config = get_machine_config(instrument_name=instrument_name)[
514528
instrument_name
515529
]
516530
if isinstance(session_id, int):
517531
if machine_config.instrument_server_url:
518532
async with aiohttp.ClientSession() as clientsession:
533+
# Send request to instrument server to update multigrid controller
534+
async with clientsession.post(
535+
f"{machine_config.instrument_server_url}{url_path_for('api.router', 'update_multigrid_controller_visit_end_time', session_id=session_id)}?end_time={quote(session_entry.visit_end_time.isoformat())}",
536+
headers={
537+
"Authorization": f"Bearer {instrument_server_tokens[session_id]['access_token']}"
538+
},
539+
) as resp:
540+
update_result = await resp.json()
541+
if not update_result.get("success", False):
542+
return {"success": False}
543+
# Send request to flush the rsyncer
519544
async with clientsession.post(
520545
f"{machine_config.instrument_server_url}{url_path_for('api.router', 'flush_skipped_rsyncer', session_id=session_id)}",
521546
json={

0 commit comments

Comments
 (0)