Pincer icon indicating copy to clipboard operation
Pincer copied to clipboard

Proper Shutdown Capabilitiy

Open CircuitSacul opened this issue 2 years ago • 9 comments

Is your feature request related to a problem? Please describe.

I think pincer needs actual shut-down logic. Something like this on Client:

def run(self):
    loop = asyncio.get_event_loop()
    self.stop_event = asyncio.Event(loop=loop)
    loop.run_until_complete(self._run())
    loop.run_until_complete(self.stop_event.wait())
    loop.run_until_complete(self._cleanup())

async def _run(self):
    await self.start_shard(0, 1)

async def _cleanup(self):
    if self.session and not self.session.closed:
        await self.session.close()

    # self.http is cleaned up be __del__ ???

def stop(self):
    """Tells the bot to shutdown"""

    self.stop_event.set()

Describe the solution you would like

Magic

Describe alternatives you have considered

There is no alternative to magic

Additional context

magic

CircuitSacul avatar Jan 22 '22 05:01 CircuitSacul

We laready have a close method within the client


    @property
    def is_closed(self) -> bool:
        """
        Returns
        -------
        bool
            Whether the bot is closed.
        """
        return self.loop.is_running()

    def close(self):
        """
        Ensure close of the http client.
        Allow for script execution to continue.
        """
        if hasattr(self, "http"):
            create_task(self.http.close())

        self.loop.stop()

    def __del__(self):
        if self.loop.is_running():
            self.loop.stop()

        if not self.loop.is_closed():
            self.close()

Sigmanificient avatar Jan 22 '22 12:01 Sigmanificient

not sure what the diff we the code you proposed, maybe the current closing could be improved

Sigmanificient avatar Jan 22 '22 12:01 Sigmanificient

@Sigmanificient does the close thing logout of the bot?

VincentRPS avatar Jan 22 '22 12:01 VincentRPS

I think bot auto logout when discord stop to receive heartbeat

Sigmanificient avatar Jan 22 '22 12:01 Sigmanificient

I think bot auto logout when discord stop to receive heartbeat

pretty sure there is a rest endpoint for logging out tho, might just make the bot immediately go offline instead of it taking a bit idk tho

VincentRPS avatar Jan 22 '22 12:01 VincentRPS

image

Sigmanificient avatar Jan 22 '22 12:01 Sigmanificient

We laready have a close method within the client

    @property
    def is_closed(self) -> bool:
        """
        Returns
        -------
        bool
            Whether the bot is closed.
        """
        return self.loop.is_running()

    def close(self):
        """
        Ensure close of the http client.
        Allow for script execution to continue.
        """
        if hasattr(self, "http"):
            create_task(self.http.close())

        self.loop.stop()

    def __del__(self):
        if self.loop.is_running():
            self.loop.stop()

        if not self.loop.is_closed():
            self.close()

For some reason, Client.close() doesn't seem to exist for me. Maybe I had an outdated version.

CircuitSacul avatar Jan 22 '22 15:01 CircuitSacul

We laready have a close method within the client

    @property
    def is_closed(self) -> bool:
        """
        Returns
        -------
        bool
            Whether the bot is closed.
        """
        return self.loop.is_running()

    def close(self):
        """
        Ensure close of the http client.
        Allow for script execution to continue.
        """
        if hasattr(self, "http"):
            create_task(self.http.close())

        self.loop.stop()

    def __del__(self):
        if self.loop.is_running():
            self.loop.stop()

        if not self.loop.is_closed():
            self.close()

For some reason, Client.close() doesn't seem to exist for me. Maybe I had an outdated version.

Oh you right, I always use version build from main, it hasn't yet be released, I am waiting for @Lunarmagpie pr to be merged before create a 0.16.0

Sigmanificient avatar Jan 22 '22 15:01 Sigmanificient

As quoted from the Docs (Gateway Disconnections):

When you close the connection to the gateway with the close code 1000 or 1001, your session will be invalidated and your bot will appear offline. If you simply close the TCP connection, or use a different close code, the bot session will remain active and timeout after a few minutes. This can be useful for a reconnect, which will resume the previous session.

In other words, our close method should send a message with the close code.

Arthurdw avatar May 26 '22 12:05 Arthurdw