Skip to content

Commit fc0d2be

Browse files
committed
Add ability to mock a long transfer
1 parent b680b13 commit fc0d2be

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

zstash/globus.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import, print_function
22

33
import sys
4+
import time
45
from typing import List, Optional
56

67
from globus_sdk import TransferAPIError, TransferClient, TransferData
@@ -26,6 +27,30 @@
2627
task_id = None
2728
archive_directory_listing: IterableTransferResponse = None
2829

30+
DEBUG_LONG_TRANSFER: bool = False # Set to true if testing token expiration handling
31+
32+
33+
def _debug_sleep_to_expire_token(context: str, retry_count: int = 0):
34+
"""
35+
FOR DEBUGGING ONLY: Sleep to simulate token expiration during long operations.
36+
37+
Args:
38+
context: Description of where this is being called (e.g., "blocking", "non-blocking")
39+
retry_count: Current retry count (only sleep on first iteration)
40+
"""
41+
if DEBUG_LONG_TRANSFER and retry_count == 0:
42+
transfer_duration_mock_hours = 49
43+
logger.info(
44+
f"{ts_utc()}: TESTING ({context}): Sleeping for {transfer_duration_mock_hours} hours to let access token expire"
45+
)
46+
time.sleep(transfer_duration_mock_hours * 3600)
47+
logger.info(
48+
f"{ts_utc()}: TESTING ({context}): Woke up after {transfer_duration_mock_hours} hours. "
49+
"Access token expired, RefreshTokenAuthorizer should automatically refresh on next API call."
50+
)
51+
return True # Indicates sleep happened
52+
return False # No sleep
53+
2954

3055
def globus_activate(hpss: str):
3156
"""
@@ -88,6 +113,9 @@ def globus_transfer( # noqa: C901
88113
# Make a simple API call to trigger refresh if needed
89114
transfer_client.endpoint_autoactivate(local_endpoint, if_expires_in=86400)
90115
transfer_client.endpoint_autoactivate(remote_endpoint, if_expires_in=86400)
116+
117+
# FOR DEBUGGING: Test non-blocking mode token refresh
118+
_debug_sleep_to_expire_token("non-blocking")
91119
except Exception as e:
92120
logger.warning(f"Token refresh check: {e}")
93121

@@ -214,6 +242,7 @@ def globus_block_wait(
214242
# Refresh token before each wait attempt
215243
transfer_client.endpoint_autoactivate(local_endpoint, if_expires_in=86400)
216244
transfer_client.endpoint_autoactivate(remote_endpoint, if_expires_in=86400)
245+
217246
logger.info(
218247
f"{ts_utc()}: on task_wait try {retry_count + 1} out of {max_retries}"
219248
)
@@ -226,6 +255,11 @@ def globus_block_wait(
226255
else:
227256
curr_task = transfer_client.get_task(task_id)
228257
task_status = curr_task["status"]
258+
259+
# FOR DEBUGGING: Test blocking mode token refresh
260+
if _debug_sleep_to_expire_token("blocking", retry_count):
261+
continue # Force another iteration to test refresh
262+
229263
if task_status == "SUCCEEDED":
230264
break
231265
finally:

zstash/globus_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import socket
99
import sys
10+
import time
1011
from typing import Dict, List, Optional
1112

1213
from globus_sdk import (
@@ -192,8 +193,6 @@ def load_tokens():
192193
expires_at = transfer_token.get("expires_at")
193194

194195
if expires_at:
195-
import time
196-
197196
# Refresh if expiring within 1 hour
198197
if time.time() > (expires_at - 3600):
199198
logger.info(

0 commit comments

Comments
 (0)