refactoring

This commit is contained in:
Devaev Maxim 2020-06-08 04:56:37 +03:00
parent 241c787e10
commit f3a9ae75f9
3 changed files with 10 additions and 10 deletions

View File

@ -55,8 +55,8 @@ class StreamerApi:
return make_json_response(await self.__streamer.get_state())
@exposed_http("GET", "/streamer/snapshot")
async def __make_snapshot_handler(self, request: Request) -> Response:
if (snapshot := await self.__streamer.make_snapshot(
async def __take_snapshot_handler(self, request: Request) -> Response:
if (snapshot := await self.__streamer.take_snapshot(
save=valid_bool(request.query.get("save", "false")),
load=valid_bool(request.query.get("load", "false")),
allow_offline=valid_bool(request.query.get("allow_offline", "false")),

View File

@ -81,7 +81,7 @@ class Snapshoter: # pylint: disable=too-many-instance-attributes
while True:
live = is_live()
if last_snapshot_ts + (self.__live_interval if live else self.__idle_interval) < time.time():
await self.__make_snapshot(live, notifier)
await self.__take_snapshot(live, notifier)
last_snapshot_ts = time.time()
await asyncio.sleep(min(self.__idle_interval, self.__live_interval))
else:
@ -90,9 +90,9 @@ class Snapshoter: # pylint: disable=too-many-instance-attributes
def snapshoting(self) -> bool:
return self.__snapshoting
async def __make_snapshot(self, live: bool, notifier: aiotools.AioNotifier) -> None:
async def __take_snapshot(self, live: bool, notifier: aiotools.AioNotifier) -> None:
logger = get_logger(0)
logger.info("Time to make the new snapshot (%s)", ("live" if live else "idle"))
logger.info("Time to take the new snapshot (%s)", ("live" if live else "idle"))
try:
self.__snapshoting = True
await notifier.notify()
@ -115,16 +115,16 @@ class Snapshoter: # pylint: disable=too-many-instance-attributes
retries = self.__retries
while retries:
snapshot = await self.__streamer.make_snapshot(save=True, load=False, allow_offline=False)
snapshot = await self.__streamer.take_snapshot(save=True, load=False, allow_offline=False)
if snapshot:
logger.info("New snapshot saved: %dx%d", snapshot.width, snapshot.height)
break
retries -= 1
await asyncio.sleep(self.__retries_delay)
else:
logger.error("Can't make snapshot after %d retries", self.__retries)
logger.error("Can't take snapshot after %d retries", self.__retries)
except Exception: # Этого вообще-то не должно случаться, апи внутри заэксцепчены, но мало ли
logger.exception("Unhandled exception while making snapshot")
logger.exception("Unhandled exception while taking snapshot")
finally:
self.__snapshoting = False
await notifier.notify()

View File

@ -245,7 +245,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
# =====
async def make_snapshot(self, save: bool, load: bool, allow_offline: bool) -> Optional[StreamerSnapshot]:
async def take_snapshot(self, save: bool, load: bool, allow_offline: bool) -> Optional[StreamerSnapshot]:
if load:
return self.__snapshot
else:
@ -280,7 +280,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
return snapshot
logger.error("Stream is offline, no signal or so")
except (aiohttp.ClientConnectionError, aiohttp.ServerConnectionError) as err:
logger.error("Can't make snapshot: %s: %s", type(err).__name__, err)
logger.error("Can't connect to streamer: %s: %s", type(err).__name__, err)
except Exception:
logger.exception("Invalid streamer response from /snapshot")
return None