Skip to content

Commit 6697141

Browse files
authored
Merge branch 'master' into fix-issue-2570-review-recovery-args
2 parents 7ffcd7b + 926b321 commit 6697141

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ the filter the jobs. Not using any value for `-fp` still returns all jobs.
4242
- Fix PS platform to work with the local machine #2374
4343
- Fixed a `ZeroDivisionError` when using RO-Crate or `stats`, and also an issue
4444
where the message said `None` could not be iterable. #2389
45+
- Fixed a bug that produces an infinite loop when it is not possible to create log files #2618
4546

4647
**Enhancements:**
4748

autosubmit/log/log.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ def get_logger(name="Autosubmit") -> None:
201201
logging.getLogger(name)
202202

203203
@staticmethod
204-
def set_file(file_path: str, type='out', level="WARNING") -> None:
204+
def set_file(
205+
file_path: str,
206+
type="out",
207+
level="WARNING",
208+
max_retries: int = 3,
209+
timeout: int = 5,
210+
) -> None:
205211
"""Configure the file to store the log.
206212
207213
If another file was specified earlier, new messages will only go to the new file.
@@ -210,6 +216,8 @@ def set_file(file_path: str, type='out', level="WARNING") -> None:
210216
:type file_path: str
211217
:param type: file type
212218
:param level: log level
219+
:param max_retries: maximum number of retries to create the log file
220+
:param timeout: time to wait between retries (in seconds)
213221
"""
214222
levels = {
215223
"STATUS_FAILED": 500,
@@ -225,9 +233,7 @@ def set_file(file_path: str, type='out', level="WARNING") -> None:
225233

226234
level = levels.get(str(level).upper(), "DEBUG")
227235

228-
max_retries = 3
229-
retries = 1
230-
timeout = 5
236+
retries = 0
231237

232238
while not os.path.exists(file_path) and retries < max_retries:
233239
try:
@@ -271,9 +277,12 @@ def set_file(file_path: str, type='out', level="WARNING") -> None:
271277
status_file_handler.addFilter(custom_filter)
272278
Log.log.addHandler(status_file_handler)
273279
os.chmod(file_path, 509)
274-
except Exception: # retry again
275-
Log.warning(f'Unexpected error setting the log file {str(file_path)}, retry {retries} out '
276-
f'of {max_retries}: {str(e)}')
280+
except Exception as exc: # retry again
281+
retries += 1
282+
if retries >= max_retries:
283+
raise AutosubmitCritical(
284+
message=f"Could not create log file {file_path} after {max_retries} attempts: {exc}"
285+
)
277286
sleep(timeout * retries)
278287

279288
@staticmethod

test/unit/test_log.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with Autosubmit. If not, see <http://www.gnu.org/licenses/>.
1717

18-
from autosubmit.log.log import AutosubmitError, AutosubmitCritical, Log
18+
from typing import TYPE_CHECKING
19+
20+
import pytest
21+
22+
from autosubmit.log.log import AutosubmitCritical, AutosubmitError, Log
23+
24+
if TYPE_CHECKING:
25+
from pytest_mock import MockFixture
1926

2027
"""Tests for the log module."""
2128

@@ -65,3 +72,17 @@ def _send_messages(msg: str):
6572
# Format messages
6673
msg = "Test {foo, bar}"
6774
_send_messages(msg)
75+
76+
77+
def test_set_file_retrial(mocker: 'MockFixture'):
78+
max_retries = 3
79+
80+
# Make os.path.split raise an exception
81+
mocker.patch("os.path.split", side_effect=Exception("Mocked exception"))
82+
83+
sleep = mocker.patch("autosubmit.log.log.sleep")
84+
85+
with pytest.raises(AutosubmitCritical):
86+
Log.set_file("imaginary.log", max_retries=max_retries, timeout=1)
87+
88+
assert sleep.call_count == max_retries - 1

0 commit comments

Comments
 (0)