-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement wait_and_log helper method to log periodically du…
…ring waiting for all sub ECUs before reboot (#410) This PR introduces `wait_and_log` helper method, which issues logs during waiting for flag set. otaclient now uses this method when waiting for all sub ECUs before reboot. Other changes: 1. otaclient will issue a warning log before actually calling reboot command. 2. otaclient will sleep 6 seconds before actually reboot the system.
- Loading branch information
1 parent
95735c8
commit cfbd715
Showing
6 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2022 TIER IV, INC. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Common shared utils, only used by otaclient package.""" | ||
|
||
|
||
from __future__ import annotations | ||
|
||
import itertools | ||
import logging | ||
import time | ||
from abc import abstractmethod | ||
from typing import Callable, Protocol | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CheckableFlag(Protocol): | ||
|
||
@abstractmethod | ||
def is_set(self) -> bool: ... | ||
|
||
|
||
def wait_and_log( | ||
flag: CheckableFlag, | ||
message: str = "", | ||
*, | ||
check_interval: int = 2, | ||
log_interval: int = 30, | ||
log_func: Callable[[str], None] = logger.info, | ||
) -> None: | ||
"""Wait for <flag> until it is set while print a log every <log_interval>.""" | ||
log_round = 0 | ||
for seconds in itertools.count(step=check_interval): | ||
if flag.is_set(): | ||
return | ||
|
||
_new_log_round = seconds // log_interval | ||
if _new_log_round > log_round: | ||
log_func(f"wait for {message}: {seconds}s passed ...") | ||
log_round = _new_log_round | ||
time.sleep(check_interval) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2022 TIER IV, INC. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
import time | ||
|
||
import pytest | ||
|
||
from otaclient.utils import wait_and_log | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class _TickingFlag: | ||
|
||
def __init__(self, trigger_in: int) -> None: | ||
self._trigger_time = time.time() + trigger_in | ||
|
||
def is_set(self) -> bool: | ||
_now = time.time() | ||
return _now > self._trigger_time | ||
|
||
|
||
def test_wait_and_log(caplog: pytest.LogCaptureFixture): | ||
# NOTE: allow 2 more seconds for expected_trigger_time | ||
trigger_in, expected_trigger_time = 11, time.time() + 11 + 2 | ||
_flag = _TickingFlag(trigger_in=trigger_in) | ||
_msg = "ticking flag" | ||
|
||
wait_and_log( | ||
_flag, | ||
_msg, | ||
check_interval=1, | ||
log_interval=2, | ||
log_func=logger.warning, | ||
) | ||
|
||
assert len(caplog.records) == 5 | ||
assert caplog.records[0].levelno == logging.WARNING | ||
assert caplog.records[0].msg == f"wait for {_msg}: 2s passed ..." | ||
assert time.time() < expected_trigger_time |
cfbd715
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report