|
| 1 | +######################################################################### |
| 2 | +# error.pyx - pyslurm db specific errors |
| 3 | +######################################################################### |
| 4 | +# Copyright (C) 2025 Toni Harzendorf <[email protected]> |
| 5 | +# |
| 6 | +# This file is part of PySlurm |
| 7 | +# |
| 8 | +# PySlurm is free software; you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation; either version 2 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | + |
| 13 | +# PySlurm is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License along |
| 19 | +# with PySlurm; if not, write to the Free Software Foundation, Inc., |
| 20 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | +# |
| 22 | +# cython: c_string_type=unicode, c_string_encoding=default |
| 23 | +# cython: language_level=3 |
| 24 | + |
| 25 | +from pyslurm.core.error import RPCError, slurm_errno, verify_rpc |
| 26 | +from pyslurm.db.util cimport SlurmList, SlurmListItem |
| 27 | + |
| 28 | + |
| 29 | +class AssociationChangeInfo: |
| 30 | + |
| 31 | + def __init__(self, user, cluster, account, partition=None): |
| 32 | + self.user = user |
| 33 | + self.cluster = cluster |
| 34 | + self.account = account |
| 35 | + self.partition = partition |
| 36 | + self.running_jobs = [] |
| 37 | + |
| 38 | + |
| 39 | +class JobsRunningError(RPCError): |
| 40 | + |
| 41 | + def __init__(self, **kwargs): |
| 42 | + super().__init__(**kwargs) |
| 43 | + self.associations = [] |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def from_response(SlurmList response, rc): |
| 47 | + running_jobs = parse_running_job_errors(response) |
| 48 | + err = JobsRunningError(errno=rc) |
| 49 | + err.associations = list(running_jobs.values()) |
| 50 | + return err |
| 51 | + |
| 52 | + |
| 53 | +class DefaultAccountError(RPCError): |
| 54 | + |
| 55 | + def __init__(self, **kwargs): |
| 56 | + super().__init__(**kwargs) |
| 57 | + self.associations = [] |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def from_response(SlurmList response, rc): |
| 61 | + err = DefaultAccountError(errno=rc) |
| 62 | + err.associations = parse_default_account_errors(response) |
| 63 | + return err |
| 64 | + |
| 65 | + |
| 66 | +def parse_default_account_errors(SlurmList response): |
| 67 | + cdef SlurmListItem response_ptr |
| 68 | + |
| 69 | + assocs = [] |
| 70 | + for response_ptr in response: |
| 71 | + response_str = response_ptr.to_str() |
| 72 | + if not response_str: |
| 73 | + continue |
| 74 | + |
| 75 | + # The response is a string in the following form: |
| 76 | + # C = X A = X U = X P = X |
| 77 | + # |
| 78 | + # And we have to parse this stuff... The Partition (P) is optional |
| 79 | + resp = response_str.rstrip().lstrip() |
| 80 | + splitted = resp.split(" ") |
| 81 | + values = [] |
| 82 | + for item in splitted: |
| 83 | + if not item: |
| 84 | + continue |
| 85 | + |
| 86 | + key, value = item.split("=") |
| 87 | + values.append(value.strip()) |
| 88 | + |
| 89 | + info = AssociationChangeInfo( |
| 90 | + cluster = values[0], |
| 91 | + account = values[1], |
| 92 | + user = values[2], |
| 93 | + ) |
| 94 | + assoc_str = f"{info.cluster}-{info.account}-{info.user}" |
| 95 | + |
| 96 | + if len(values) > 3: |
| 97 | + info.partition = values[3] |
| 98 | + assoc_str = f"{assoc_str}-{info.partition}" |
| 99 | + |
| 100 | + assocs.append(info) |
| 101 | + |
| 102 | + return assocs |
| 103 | + |
| 104 | + |
| 105 | +def parse_running_job_errors(SlurmList response): |
| 106 | + cdef SlurmListItem response_ptr |
| 107 | + |
| 108 | + running_jobs_for_assoc = {} |
| 109 | + for response_ptr in response: |
| 110 | + response_str = response_ptr.to_str() |
| 111 | + if not response_str: |
| 112 | + continue |
| 113 | + |
| 114 | + # The response is a string in the following form: |
| 115 | + # JobId = X C = X A = X U = X P = X |
| 116 | + # |
| 117 | + # And we have to parse this stuff... The Partition (P) is optional |
| 118 | + resp = response_str.rstrip().lstrip() |
| 119 | + splitted = resp.split(" ") |
| 120 | + values = [] |
| 121 | + for item in splitted: |
| 122 | + if not item: |
| 123 | + continue |
| 124 | + |
| 125 | + key, value = item.split("=") |
| 126 | + values.append(value.strip()) |
| 127 | + |
| 128 | + job_id = int(values[0]) |
| 129 | + cluster = values[1] |
| 130 | + account = values[2] |
| 131 | + user = values[3] |
| 132 | + partition = None |
| 133 | + assoc_str = f"{cluster}-{account}-{user}" |
| 134 | + |
| 135 | + if len(values) > 4: |
| 136 | + partition = values[4] |
| 137 | + assoc_str = f"{assoc_str}-{partition}" |
| 138 | + |
| 139 | + if assoc_str not in running_jobs_for_assoc: |
| 140 | + info = AssociationChangeInfo( |
| 141 | + user = user, |
| 142 | + cluster = cluster, |
| 143 | + account = account, |
| 144 | + partition = partition, |
| 145 | + ) |
| 146 | + running_jobs_for_assoc[assoc_str] = info |
| 147 | + |
| 148 | + running_jobs_for_assoc[assoc_str].running_jobs.append(job_id) |
| 149 | + |
| 150 | + return running_jobs_for_assoc |
0 commit comments