| 1 |
.. currentmodule:: globus_sdk |
| 2 |
|
| 3 |
.. _userguide_scheduled_transfers: |
| 4 |
|
| 5 |
Scheduling a Repeating Transfer |
| 6 |
=============================== |
| 7 |
|
| 8 |
The Globus Timers service allows users to schedule tasks to run at a future |
| 9 |
date or on a recurring schedule. |
| 10 |
In particular, Timers allows scheduled ``Transfer Tasks``, which can be |
| 11 |
submitted very similarly to submissions to the Globus Transfer service. |
| 12 |
|
| 13 |
Creating a Transfer Timer |
| 14 |
------------------------- |
| 15 |
|
| 16 |
To setup recurring transfers, you will need to create a timer. |
| 17 |
The timer *contains* a Transfer Task submission. |
| 18 |
It will submit that Transfer Task each time it runs. |
| 19 |
|
| 20 |
.. literalinclude:: create_timer.py |
| 21 |
:caption: ``create_timer.py`` [:download:`download <create_timer.py>`] |
| 22 |
:language: python |
| 23 |
|
| 24 |
|
| 25 |
Discover Data Access Scopes and Create a Timer |
| 26 |
---------------------------------------------- |
| 27 |
|
| 28 |
Unlike direct ``Transfer Task`` submission, creating a Timer with unknown inputs |
| 29 |
won't give you an error immediately if you need ``data_access`` scopes because |
| 30 |
that information isn't available until the Timer runs. |
| 31 |
|
| 32 |
Therefore, if the input collections are variable, we need to enhance the previous |
| 33 |
example to automatically determine whether or not the ``data_access`` scope is |
| 34 |
needed. |
| 35 |
We'll do this with a new ``uses_data_access`` helper and a :class:`TransferClient`: |
| 36 |
|
| 37 |
.. code-block:: python |
| 38 |
|
| 39 |
with globus_sdk.TransferClient(app=app) as transfer_client: |
| 40 |
... # a code block which can use the helper |
| 41 |
|
| 42 |
|
| 43 |
def uses_data_access(client: globus_sdk.TransferClient, collection_id: str) -> bool: |
| 44 |
""" |
| 45 |
Lookup the given collection ID. |
| 46 |
Having looked up the record, return `True` if it uses a `data_access` scope |
| 47 |
and `False` otherwise. |
| 48 |
""" |
| 49 |
doc = client.get_endpoint(collection_id) |
| 50 |
if doc["entity_type"] != "GCSv5_mapped_collection": |
| 51 |
return False |
| 52 |
if doc["high_assurance"]: |
| 53 |
return False |
| 54 |
return True |
| 55 |
|
| 56 |
This will allow us to guard our use of the ``data_access`` scope thusly: |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
|
| 60 |
if uses_data_access(transfer_client, SRC_COLLECTION): |
| 61 |
timers_client.add_app_transfer_data_access_scope(SRC_COLLECTION) |
| 62 |
if uses_data_access(transfer_client, DST_COLLECTION): |
| 63 |
timers_client.add_app_transfer_data_access_scope(DST_COLLECTION) |
| 64 |
|
| 65 |
.. note:: |
| 66 |
|
| 67 |
Because the ``data_access`` requirement can't be detected until after you have |
| 68 |
logged in to the app, it is possible for this to result in a "double login" |
| 69 |
scenario. First, you login providing consent for Timers and Transfer, but |
| 70 |
then a ``data_access`` scope is found to be needed. You then have to login |
| 71 |
again to satisfy that requirement. |
| 72 |
|
| 73 |
The ``UserApp`` will track the addition until you use ``app.logout``, |
| 74 |
however, so this only happens the first time the script runs. |
| 75 |
|
| 76 |
With these modifications in place, the resulting script looks like so: |
| 77 |
|
| 78 |
.. literalinclude:: create_timer_detect_data_access.py |
| 79 |
:caption: ``create_timer_detect_data_access.py`` [:download:`download <create_timer_detect_data_access.py>`] |
| 80 |
:language: python |