Skip to content

Commit

Permalink
otaclient: enable status caching, set interval to 1s
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodong-Yang committed Jul 4, 2024
1 parent a3832d1 commit 20db301
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/otaclient/app/ota_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import gc
import json
import logging
import time
import threading
from concurrent.futures import Future, ThreadPoolExecutor
from functools import partial
Expand Down Expand Up @@ -169,7 +170,13 @@ def __init__(
boot_controller: BootControllerProtocol,
create_standby_cls: Type[StandbySlotCreatorProtocol],
control_flags: OTAClientControlFlags,
status_query_interval: int = 1,
) -> None:
self._shutdown = False
self._update_status = api_types.UpdateStatus()
self._last_status_query_timestamp = 0
self.status_query_interval = status_query_interval

# ------ define OTA temp paths ------ #
self._ota_tmp_on_standby = Path(cfg.MOUNT_POINT) / Path(
cfg.OTA_TMP_STORE
Expand Down Expand Up @@ -512,6 +519,7 @@ def _execute_update(self):
# API

def shutdown(self):
self._shutdown = True
self.update_phase = api_types.UpdatePhase.INITIALIZING
self._downloader_pool.shutdown()
self._update_stats_collector.shutdown_collector()
Expand All @@ -521,9 +529,15 @@ def get_update_status(self) -> api_types.UpdateStatus:
Returns:
A tuple contains the version and the update_progress.
"""
collector = self._update_stats_collector
cur_time = int(time.time())
if (
self._shutdown
or cur_time - self._last_status_query_timestamp < self.status_query_interval
):
return self._update_status

return api_types.UpdateStatus(
collector = self._update_stats_collector
update_stats = api_types.UpdateStatus(
# from OTA image metadata
update_firmware_version=self.updating_version,
total_files_size_uncompressed=self.total_files_size_uncompressed,
Expand Down Expand Up @@ -553,6 +567,8 @@ def get_update_status(self) -> api_types.UpdateStatus:
seconds=collector.apply_update_elapsed_time
),
)
self._update_status, self._last_status_query_timestamp = update_stats, cur_time
return update_stats

def execute(self) -> None:
"""Main entry for executing local OTA update.
Expand Down

0 comments on commit 20db301

Please sign in to comment.