Introduced a shutdown API (docs)
When running the
loop() method via web, MadelineProto will automatically enable a magical self-restart hack, to keep the bot running even on webhosts with limited execution time. It relies on the shutdown function, so you must not set a custom shutdown function with register_shutdown_function in your code, and instead use the MadelineProto shutdown static API:
use danog\MadelineProto\Shutdown;You can of course pass non-static functions, any type of callable is accepted.
$id = Shutdown::addCallback(static function () {
// This function will run on shutdown
});
$id = Shutdown::addCallback(static function () {
// This function will run on shutdown
}, 'custom id');
$id = Shutdown::addCallback(static function () {
// This function will overwrite the previously set function with custom id
}, 'custom id');
$ok = Shutdown::removeCallback($id);
A second optional parameter can also be accepted, containing the ID of the callable: you can use this if you want to later overwrite the callable with another callback, or remove it altogether.
The
removeCallback will return true if the callback exists and it was removed correctly, false otherwise (as with all new MadelineProto 4.0 APIs, there are PHPDOCs for these methods so you'll see them in your IDE).