| 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 |
"""Define the Cylc task queue management API.""" |
| 18 |
|
| 19 |
from typing import List, Dict, Counter, Any, TYPE_CHECKING |
| 20 |
from abc import ABCMeta, abstractmethod |
| 21 |
|
| 22 |
if TYPE_CHECKING: |
| 23 |
from cylc.flow.task_proxy import TaskProxy |
| 24 |
|
| 25 |
|
| 26 |
class TaskQueueManagerBase(metaclass=ABCMeta): |
| 27 |
"""Base class for queueing implementations.""" |
| 28 |
|
| 29 |
@abstractmethod |
| 30 |
def __init__(self, |
| 31 |
qconfig: dict, |
| 32 |
all_task_names: List[str], |
| 33 |
descendants: dict |
| 34 |
) -> None: |
| 35 |
"""Initialize task queue manager from workflow config. |
| 36 |
|
| 37 |
Arguments: |
| 38 |
* qconfig: flow.cylc queues config |
| 39 |
* all_task_names: list of all task names |
| 40 |
* descendants: runtime family dict |
| 41 |
|
| 42 |
""" |
| 43 |
raise NotImplementedError |
| 44 |
|
| 45 |
@abstractmethod |
| 46 |
def push_task(self, itask: 'TaskProxy') -> None: |
| 47 |
"""Queue the given task.""" |
| 48 |
raise NotImplementedError |
| 49 |
|
| 50 |
@abstractmethod |
| 51 |
def push_task_if_limited( |
| 52 |
self, itask: 'TaskProxy', active: Counter[str] |
| 53 |
) -> bool: |
| 54 |
"""Queue the task only if the queue limit is reached. |
| 55 |
|
| 56 |
Requires current active task counts. |
| 57 |
Return True if queued, else False. |
| 58 |
""" |
| 59 |
raise NotImplementedError |
| 60 |
|
| 61 |
@abstractmethod |
| 62 |
def release_tasks(self, active: Counter[str]) -> 'List[TaskProxy]': |
| 63 |
"""Release tasks, given current active task counts.""" |
| 64 |
raise NotImplementedError |
| 65 |
|
| 66 |
@abstractmethod |
| 67 |
def remove_task(self, itask: 'TaskProxy') -> bool: |
| 68 |
"""Try to remove a task from the queues. Return True if done.""" |
| 69 |
raise NotImplementedError |
| 70 |
|
| 71 |
@abstractmethod |
| 72 |
def adopt_tasks(self, orphans: List[str]) -> None: |
| 73 |
"""Adopt tasks with defs removed by scheduler reload or restart.""" |
| 74 |
raise NotImplementedError |
| 75 |
|
| 76 |
def _expand_families(self, |
| 77 |
qconfig: dict, |
| 78 |
all_task_names: List[str], |
| 79 |
descendants: dict |
| 80 |
) -> dict: |
| 81 |
"""Expand family names in queue membership lists. |
| 82 |
|
| 83 |
(All queueing implementations will presumably need this). |
| 84 |
Returns dict of queues config with only task names present. |
| 85 |
|
| 86 |
""" |
| 87 |
queues: Dict[str, Any] = {} |
| 88 |
for qname, queue in qconfig.items(): |
| 89 |
qmembers = set() |
| 90 |
for mem in queue["members"]: |
| 91 |
if mem in descendants: |
| 92 |
# Family name. |
| 93 |
for fmem in descendants[mem]: |
| 94 |
if (fmem not in descendants |
| 95 |
and fmem in all_task_names): |
| 96 |
# Task name. |
| 97 |
qmembers.add(fmem) |
| 98 |
elif mem in all_task_names: |
| 99 |
# Task name. |
| 100 |
qmembers.add(mem) |
| 101 |
queues[qname] = {} |
| 102 |
queues[qname]["members"] = qmembers |
| 103 |
queues[qname]["limit"] = queue["limit"] |
| 104 |
return queues |