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()) return make_json_response(await self.__streamer.get_state())
@exposed_http("GET", "/streamer/snapshot") @exposed_http("GET", "/streamer/snapshot")
async def __make_snapshot_handler(self, request: Request) -> Response: async def __take_snapshot_handler(self, request: Request) -> Response:
if (snapshot := await self.__streamer.make_snapshot( if (snapshot := await self.__streamer.take_snapshot(
save=valid_bool(request.query.get("save", "false")), save=valid_bool(request.query.get("save", "false")),
load=valid_bool(request.query.get("load", "false")), load=valid_bool(request.query.get("load", "false")),
allow_offline=valid_bool(request.query.get("allow_offline", "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: while True:
live = is_live() live = is_live()
if last_snapshot_ts + (self.__live_interval if live else self.__idle_interval) < time.time(): 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() last_snapshot_ts = time.time()
await asyncio.sleep(min(self.__idle_interval, self.__live_interval)) await asyncio.sleep(min(self.__idle_interval, self.__live_interval))
else: else:
@ -90,9 +90,9 @@ class Snapshoter: # pylint: disable=too-many-instance-attributes
def snapshoting(self) -> bool: def snapshoting(self) -> bool:
return self.__snapshoting 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 = 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: try:
self.__snapshoting = True self.__snapshoting = True
await notifier.notify() await notifier.notify()
@ -115,16 +115,16 @@ class Snapshoter: # pylint: disable=too-many-instance-attributes
retries = self.__retries retries = self.__retries
while 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: if snapshot:
logger.info("New snapshot saved: %dx%d", snapshot.width, snapshot.height) logger.info("New snapshot saved: %dx%d", snapshot.width, snapshot.height)
break break
retries -= 1 retries -= 1
await asyncio.sleep(self.__retries_delay) await asyncio.sleep(self.__retries_delay)
else: 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: # Этого вообще-то не должно случаться, апи внутри заэксцепчены, но мало ли except Exception: # Этого вообще-то не должно случаться, апи внутри заэксцепчены, но мало ли
logger.exception("Unhandled exception while making snapshot") logger.exception("Unhandled exception while taking snapshot")
finally: finally:
self.__snapshoting = False self.__snapshoting = False
await notifier.notify() 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: if load:
return self.__snapshot return self.__snapshot
else: else:
@ -280,7 +280,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
return snapshot return snapshot
logger.error("Stream is offline, no signal or so") logger.error("Stream is offline, no signal or so")
except (aiohttp.ClientConnectionError, aiohttp.ServerConnectionError) as err: 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: except Exception:
logger.exception("Invalid streamer response from /snapshot") logger.exception("Invalid streamer response from /snapshot")
return None return None