Debian Codegraph @

docs/user_guide/usage_patterns/data_transfer/transfer_relative_deadline/index.rst

raw
1 .. currentmodule:: globus_sdk
2
3 .. _userguide_transfer_relative_deadline:
4
5 Setting a Relative Deadline for a Transfer
6 ==========================================
7
8 When transferring or deleting data via Globus Transfer, users are able to set a
9 ``deadline`` for their tasks.
10 This allows you to declare a time by which the task must be completed -- if the
11 deadline is reached and the task is still in progress, it will be cancelled.
12
13 The ``deadline`` field in a Transfer task takes a date and time as a string,
14 with support for common ISO 8601 format.
15 Because of the use of standard formats, it is easy to use the Python
16 ``datetime`` module to compute a relative deadline at some point in the future.
17 You can use this to easily submit tasks with deadlines limited to the next minute,
18 hour, or day.
19 You can use this, for example, to enforce that a Transfer Task which takes too
20 long results in errors (even if it is making slow progress).
21
22 .. note::
23
24 Not all ISO 8601 syntaxes are supported.
25 We recommend sticking to the restrictive subset defined by RFC 3339, in
26 which date-time data is typically formatted as "YYYY-MM-DD'T'HH:mm:SSZ".
27
28 :py:meth:`datetime.isoformat() <datetime.datetime.isoformat()>` follows
29 this format and is therefore a great choice for Python programmers!
30
31 For readers who prefer to start with complete working examples, jump ahead to the
32 :ref:`example script <userguide_transfer_relative_deadline_example>`.
33
34 Computing and Formatting a Deadline
35 -----------------------------------
36
37 We need to compute a relative deadline -- some point into the future -- and
38 format it as a string.
39 We'll express that idea in a function which takes a :class:`datetime.timedelta`
40 as an ``offset``, an amount of time into the future.
41 This gives us a generic phrasing of getting a future date:
42
43 .. code-block:: python
44
45 import datetime
46
47
48 def make_relative_deadline(offset: datetime.timedelta) -> str:
49 now = datetime.datetime.now(tz=datetime.timezone.utc)
50 deadline = now + offset
51 return deadline.isoformat()
52
53 We can then see that this works by testing it out:
54
55 .. code-block:: pycon
56
57 >>> make_relative_deadline(datetime.timedelta(minutes=10))
58 '2003-09-21T18:58:09.279314+00:00'
59
60 Creating a Task with the Deadline
61 ---------------------------------
62
63 ``deadline`` is an initialization parameter to :class:`TransferData` and
64 :class:`DeleteData`.
65 Along with all of our other parameters to create the Transfer Task, here's
66 a sample task document with a deadline set for "an hour from now":
67
68 .. code-block:: python
69
70 # Globus Tutorial Collection 1
71 # https://app.globus.org/file-manager/collections/6c54cade-bde5-45c1-bdea-f4bd71dba2cc
72 SRC_COLLECTION = "6c54cade-bde5-45c1-bdea-f4bd71dba2cc"
73 SRC_PATH = "/share/godata/file1.txt"
74
75 # Globus Tutorial Collection 2
76 # https://app.globus.org/file-manager/collections/31ce9ba0-176d-45a5-add3-f37d233ba47d
77 DST_COLLECTION = "31ce9ba0-176d-45a5-add3-f37d233ba47d"
78 DST_PATH = "/~/example-transfer-script-destination.txt"
79
80 # create a Transfer Task request document, including a relative deadline
81 transfer_request = globus_sdk.TransferData(
82 SRC_COLLECTION,
83 DST_COLLECTION,
84 deadline=make_relative_deadline(datetime.timedelta(hours=1)),
85 )
86 transfer_request.add_item(SRC_PATH, DST_PATH)
87
88 This is then valid to submit with a :class:`TransferClient`:
89
90 .. code-block:: python
91
92 tc = globus_sdk.TransferClient(...)
93
94 tc.submit_transfer(transfer_request)
95
96 .. _userguide_transfer_relative_deadline_example:
97
98 Summary: Complete Example
99 -------------------------
100
101 For a complete example script, we will need to also include a
102 :class:`GlobusApp` for login, so that we can setup the :class:`TransferClient`
103 correctly.
104 We'll also take a small step to make the script work with mapped collections
105 which require the ``data_access`` scope, like the tutorial collections.
106 With those small additions, the above examples can be turned into a working
107 script!
108
109 *This example is complete. It should run without errors "as is".*
110
111 .. literalinclude:: submit_transfer_relative_deadline.py
112 :caption: ``submit_transfer_relative_deadline.py`` [:download:`download <submit_transfer_relative_deadline.py>`]
113 :language: python