/
queue_adapter.py
74 lines (62 loc) · 2.89 KB
/
queue_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.
"""
This is a central control point for all the queue adapters, i.e. how we talk to remote computing power.
Before this class was written, we only ever used the first queue adapter we found defined in the resources.
That behaviour is maintained, but now with `QueueAdapters` we can trivially extend to using multiple resources and
multiple adapters.
"""
from pyiron_base.interfaces.singleton import Singleton
from pysqa import QueueAdapter as PySQAAdpter
import os
from pyiron_base.state.settings import settings
__author__ = "Liam Huber"
__copyright__ = (
"Copyright 2021, Max-Planck-Institut für Eisenforschung GmbH - "
"Computational Materials Design (CM) Department"
)
__version__ = "1.0"
__maintainer__ = "Liam Huber"
__email__ = "huber@mpie.de"
__status__ = "production"
__date__ = "Oct 22, 2021"
class QueueAdapters(metaclass=Singleton):
"""
Populates a list of :class:`pysqa.QueueAdapter` objects based on info stored in the resource paths defined in the
settings.
For each resource path, if either of `queues/queue.yaml` and `queues/clusters.yaml` exist, then the `queues/`
folder in that path is used to initialize a new object, with preference given to `queue.yaml`.
The :attribute:`adapter` property then lets you access the *first of these that was found*, or None if there were no
queue configuration files.
"""
def __init__(self):
self._adapters = None
self.construct_adapters()
def construct_adapters(self) -> None:
"""Read through the resources and construct queue adapters for all the queue configuration files found."""
self._adapters = []
for resource_path in settings.configuration["resource_paths"]:
if (
os.path.exists(resource_path)
and "queues" in os.listdir(resource_path)
and (
"queue.yaml" in os.listdir(os.path.join(resource_path, "queues"))
or "clusters.yaml"
in os.listdir(os.path.join(resource_path, "queues"))
)
):
self._adapters.append(
PySQAAdpter(directory=os.path.join(resource_path, "queues"))
)
@property
def adapter(self) -> PySQAAdpter:
"""
A :class:`pysqa.QueueAdapter` constructed from the first appropriate configuration files found among the
`queues/` subdirectory among the resource paths defined in the settings.
"""
return None if len(self._adapters) == 0 else self._adapters[0]
def update(self) -> None:
"""Constructs new queue adapters based on the current settings configuration."""
self.construct_adapters()
queue_adapters = QueueAdapters()