From 04cf2dff613a64382b3b135df2020bdcde756b1e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 6 Jun 2019 15:57:08 +0200 Subject: [PATCH] Add shutdown API docs --- docs/docs/UPDATES.md | 30 ++++++++++++++++++++++++++++++ docs/index.md | 1 + 2 files changed, 31 insertions(+) diff --git a/docs/docs/UPDATES.md b/docs/docs/UPDATES.md index 422667b7..0389373f 100644 --- a/docs/docs/UPDATES.md +++ b/docs/docs/UPDATES.md @@ -7,6 +7,7 @@ image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png Update handling can be done in different ways: +* [Self-restart on webhosts](#self-restart-on-webhosts) * [Async Event driven](#async-event-driven) * [Multi-account: Async Combined Event driven update handling](#async-combined-event-driven) * [Async Callback](#async-callback) @@ -14,6 +15,35 @@ Update handling can be done in different ways: * [Fetch all updates from the beginning](#fetch-all-updates-from-the-beginning) +## Self-restart on webhosts + +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 in your code, and instead use the **MadelineProto shutdown static API**: + +```php +use danog\MadelineProto\Shutdown; + +$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); +``` + +You can of course pass non-static functions, any type of callable is accepted. +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). + ## Async Event driven ```php diff --git a/docs/index.md b/docs/index.md index b6e31d22..812446ab 100644 --- a/docs/index.md +++ b/docs/index.md @@ -103,6 +103,7 @@ Tip: if you receive an error (or nothing), [send us](https://t.me/pwrtelegramgro * [Composer from scratch](https://docs.madelineproto.xyz/docs/INSTALLATION.html#composer-from-scratch) * [Composer from existing project](https://docs.madelineproto.xyz/docs/INSTALLATION.html#composer-from-existing-project) * [Handling updates (new messages)](https://docs.madelineproto.xyz/docs/UPDATES.html) + * [Self-restart on webhosts](https://docs.madelineproto.xyz/docs/UPDATES.html#self-restart-on-webhosts) * [Async Event driven](https://docs.madelineproto.xyz/docs/UPDATES.html#async-event-driven) * [Multi-account: Async Combined Event driven update handling](https://docs.madelineproto.xyz/docs/UPDATES.html#async-combined-event-driven) * [Async Callback](https://docs.madelineproto.xyz/docs/UPDATES.html#async-callback)