| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""xtrigger function to trigger off of a wall clock time.""" |
| 18 |
|
| 19 |
from time import time |
| 20 |
from typing import Any, Dict |
| 21 |
from cylc.flow.cycling.iso8601 import interval_parse |
| 22 |
from cylc.flow.exceptions import WorkflowConfigError |
| 23 |
|
| 24 |
|
| 25 |
def wall_clock(offset: str = 'PT0S', sequential: bool = True): |
| 26 |
"""Trigger at a specific real "wall clock" time relative to the cycle point |
| 27 |
in the graph. |
| 28 |
|
| 29 |
Clock triggers, unlike other trigger functions, are executed synchronously |
| 30 |
in the main process. |
| 31 |
|
| 32 |
Args: |
| 33 |
offset: |
| 34 |
ISO 8601 interval to wait after the cycle point is reached in real |
| 35 |
time before triggering. May be negative, in which case it will |
| 36 |
trigger before the real time reaches the cycle point. |
| 37 |
sequential: |
| 38 |
Wall-clock xtriggers are run sequentially by default. |
| 39 |
See :ref:`Sequential Xtriggers` for more details. |
| 40 |
|
| 41 |
.. versionchanged:: 8.3.0 |
| 42 |
|
| 43 |
The ``sequential`` argument was added. |
| 44 |
""" |
| 45 |
# NOTE: This is just a placeholder for the actual implementation. |
| 46 |
# This is only used for validating the signature and for autodocs. |
| 47 |
... |
| 48 |
|
| 49 |
|
| 50 |
def _wall_clock(trigger_time: int) -> bool: |
| 51 |
"""Actual implementation of wall_clock. |
| 52 |
|
| 53 |
Return True after the desired wall clock time, or False before. |
| 54 |
|
| 55 |
Args: |
| 56 |
trigger_time: |
| 57 |
Trigger time as seconds since Unix epoch. |
| 58 |
sequential (bool): |
| 59 |
Used by the workflow to flag corresponding xtriggers as sequential. |
| 60 |
""" |
| 61 |
return time() > trigger_time |
| 62 |
|
| 63 |
|
| 64 |
def validate(args: Dict[str, Any]): |
| 65 |
"""Validate and manipulate args parsed from the workflow config. |
| 66 |
|
| 67 |
NOTE: the xtrigger signature is different to the function signature above |
| 68 |
|
| 69 |
wall_clock() # infer zero interval |
| 70 |
wall_clock(PT1H) |
| 71 |
wall_clock(offset=PT1H) |
| 72 |
|
| 73 |
The offset must be a valid ISO 8601 interval. |
| 74 |
""" |
| 75 |
try: |
| 76 |
interval_parse(args["offset"]) |
| 77 |
except (ValueError, AttributeError): |
| 78 |
raise WorkflowConfigError( |
| 79 |
f"Invalid offset: {args['offset']}" |
| 80 |
) from None |