1 5
import warnings
2 5
from collections import ChainMap
3 5
from typing import TYPE_CHECKING, Any
4

5 5
import aiohttp
6

7 5
from .channel import Channel, ChannelSubscriber
8

9

10 5
if TYPE_CHECKING:
11 0
    from .containers import DockerContainer
12 0
    from .docker import Docker
13

14

15 5
class DockerLog:
16 5
    def __init__(self, docker: "Docker", container: "DockerContainer") -> None:
17 5
        self.docker = docker
18 5
        self.channel = Channel()
19 5
        self.container = container
20 5
        self.response = None
21

22 5
    def listen(self) -> ChannelSubscriber:
23 0
        warnings.warn(
24
            "use subscribe() method instead", DeprecationWarning, stacklevel=2
25
        )
26 0
        return self.channel.subscribe()
27

28 5
    def subscribe(self) -> ChannelSubscriber:
29 0
        return self.channel.subscribe()
30

31 5
    async def run(self, **params: Any) -> None:
32 0
        if self.response:
33 0
            warnings.warn("already running", RuntimeWarning, stackelevel=2)
34 0
            return
35 0
        forced_params = {"follow": True}
36 0
        default_params = {"stdout": True, "stderr": True}
37 0
        params2 = ChainMap(forced_params, params, default_params)
38 0
        try:
39 0
            self.response = await self.docker._query(
40
                "containers/{self.container._id}/logs".format(self=self), params=params2
41
            )
42 0
            assert self.response is not None
43 0
            while True:
44 0
                msg = await self.response.content.readline()
45 0
                if not msg:
46 0
                    break
47 0
                await self.channel.publish(msg)
48 0
        except (aiohttp.ClientConnectionError, aiohttp.ServerDisconnectedError):
49 0
            pass
50
        finally:
51
            # signal termination to subscribers
52 0
            await self.channel.publish(None)
53 0
            if self.response is not None:
54 0
                try:
55 0
                    await self.response.release()
56 0
                except Exception:
57 0
                    pass
58 0
            self.response = None
59

60 5
    async def stop(self) -> None:
61 0
        if self.response:
62 0
            await self.response.release()

Read our documentation on viewing source code .

Loading