1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 05:58:58 +01:00

Avoid issues with limited execution time

This commit is contained in:
Daniil Gentili 2024-08-23 14:35:26 +02:00
parent e70de92685
commit 30c786eb4d
2 changed files with 12 additions and 20 deletions

View File

@ -20,7 +20,6 @@ declare(strict_types=1);
namespace danog\MadelineProto\Broadcast; namespace danog\MadelineProto\Broadcast;
use Amp\Cancellation;
use danog\MadelineProto\Broadcast\Action\ActionForward; use danog\MadelineProto\Broadcast\Action\ActionForward;
use danog\MadelineProto\Broadcast\Action\ActionSend; use danog\MadelineProto\Broadcast\Action\ActionSend;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
@ -54,9 +53,8 @@ trait Broadcast
* @param array $messages The messages to send: an array of arrays, containing parameters to pass to messages.sendMessage. * @param array $messages The messages to send: an array of arrays, containing parameters to pass to messages.sendMessage.
* @param bool $pin Whether to also pin the last sent message. * @param bool $pin Whether to also pin the last sent message.
* @param float|null $delay Number of seconds to wait between each peer. * @param float|null $delay Number of seconds to wait between each peer.
* @param ?Cancellation $cancellation Cancellation. Note: you may also use cancelBroadcast with the returned broadcast ID. Be aware that when running via web with limited execution time, the broadcast will continue correctly after a restart and cancelBroadcast will still be usable, but the cancellation that is passed here will not be usable.
*/ */
public function broadcastMessages(array $messages, ?Filter $filter = null, bool $pin = false, ?float $delay = null, ?Cancellation $cancellation = null): int public function broadcastMessages(array $messages, ?Filter $filter = null, bool $pin = false, ?float $delay = null): int
{ {
foreach ($messages as &$message) { foreach ($messages as &$message) {
if (isset($message['media']['_']) && if (isset($message['media']['_']) &&
@ -69,11 +67,11 @@ trait Broadcast
) { ) {
$message['media'] = $this->methodCallAsyncRead( $message['media'] = $this->methodCallAsyncRead(
'messages.uploadMedia', 'messages.uploadMedia',
['peer' => 'me', 'media' => $message['media'], 'cancellation' => $cancellation] ['peer' => 'me', 'media' => $message['media']]
); );
} }
} unset($message); } unset($message);
return $this->broadcastCustom(new ActionSend($this, $messages, $pin), $filter, $delay, $cancellation); return $this->broadcastCustom(new ActionSend($this, $messages, $pin), $filter, $delay);
} }
/** /**
* Forwards a list of messages to all peers (users, chats, channels) of the bot. * Forwards a list of messages to all peers (users, chats, channels) of the bot.
@ -92,11 +90,10 @@ trait Broadcast
* @param bool $drop_author If true, will forward messages without quoting the original author. * @param bool $drop_author If true, will forward messages without quoting the original author.
* @param bool $pin Whether to also pin the last sent message. * @param bool $pin Whether to also pin the last sent message.
* @param float|null $delay Number of seconds to wait between each peer. * @param float|null $delay Number of seconds to wait between each peer.
* @param ?Cancellation $cancellation Cancellation. Note: you may also use cancelBroadcast with the returned broadcast ID. Be aware that when running via web with limited execution time, the broadcast will continue correctly after a restart and cancelBroadcast will still be usable, but the cancellation that is passed here will not be usable.
*/ */
public function broadcastForwardMessages(mixed $from_peer, array $message_ids, bool $drop_author = false, ?Filter $filter = null, bool $pin = false, ?float $delay = null, ?Cancellation $cancellation = null): int public function broadcastForwardMessages(mixed $from_peer, array $message_ids, bool $drop_author = false, ?Filter $filter = null, bool $pin = false, ?float $delay = null): int
{ {
return $this->broadcastCustom(new ActionForward($this, $this->getID($from_peer), $message_ids, $drop_author, $pin), $filter, $delay, $cancellation); return $this->broadcastCustom(new ActionForward($this, $this->getID($from_peer), $message_ids, $drop_author, $pin), $filter, $delay);
} }
/** /**
@ -113,16 +110,14 @@ trait Broadcast
* *
* @param Action $action A custom, serializable Action class that will be called once for every peer. * @param Action $action A custom, serializable Action class that will be called once for every peer.
* @param float|null $delay Number of seconds to wait between each peer. * @param float|null $delay Number of seconds to wait between each peer.
* @param ?Cancellation $cancellation Cancellation. Note: you may also use cancelBroadcast with the returned broadcast ID. Be aware that when running via web with limited execution time, the broadcast will continue correctly after a restart and cancelBroadcast will still be usable, but the cancellation that is passed here will not be usable.
*/ */
public function broadcastCustom(Action $action, ?Filter $filter = null, ?float $delay = null, ?Cancellation $cancellation = null): int public function broadcastCustom(Action $action, ?Filter $filter = null, ?float $delay = null): int
{ {
// Ensure it can be serialized // Ensure it can be serialized
Assert::eq(unserialize(serialize($action))::class, $action::class); Assert::eq(unserialize(serialize($action))::class, $action::class);
$id = $this->broadcastId--; $id = $this->broadcastId--;
$this->broadcasts[$id] = new InternalState($id, $this, $action, $filter ?? Filter::default(), $delay); $this->broadcasts[$id] = new InternalState($id, $this, $action, $filter ?? Filter::default(), $delay);
$cancellation?->subscribe(fn () => $this->cancelBroadcast($id));
return $id; return $id;
} }
/** /**

View File

@ -250,11 +250,10 @@ abstract class InternalDoc
* *
* @param Action $action A custom, serializable Action class that will be called once for every peer. * @param Action $action A custom, serializable Action class that will be called once for every peer.
* @param float|null $delay Number of seconds to wait between each peer. * @param float|null $delay Number of seconds to wait between each peer.
* @param ?Cancellation $cancellation Cancellation. Note: you may also use cancelBroadcast with the returned broadcast ID. Be aware that when running via web with limited execution time, the broadcast will continue correctly after a restart and cancelBroadcast will still be usable, but the cancellation that is passed here will not be usable.
*/ */
final public function broadcastCustom(\danog\MadelineProto\Broadcast\Action $action, ?\danog\MadelineProto\Broadcast\Filter $filter = null, ?float $delay = null, ?\Amp\Cancellation $cancellation = null): int final public function broadcastCustom(\danog\MadelineProto\Broadcast\Action $action, ?\danog\MadelineProto\Broadcast\Filter $filter = null, ?float $delay = null): int
{ {
return $this->wrapper->getAPI()->broadcastCustom($action, $filter, $delay, $cancellation); return $this->wrapper->getAPI()->broadcastCustom($action, $filter, $delay);
} }
/** /**
* Forwards a list of messages to all peers (users, chats, channels) of the bot. * Forwards a list of messages to all peers (users, chats, channels) of the bot.
@ -273,11 +272,10 @@ abstract class InternalDoc
* @param bool $drop_author If true, will forward messages without quoting the original author. * @param bool $drop_author If true, will forward messages without quoting the original author.
* @param bool $pin Whether to also pin the last sent message. * @param bool $pin Whether to also pin the last sent message.
* @param float|null $delay Number of seconds to wait between each peer. * @param float|null $delay Number of seconds to wait between each peer.
* @param ?Cancellation $cancellation Cancellation. Note: you may also use cancelBroadcast with the returned broadcast ID. Be aware that when running via web with limited execution time, the broadcast will continue correctly after a restart and cancelBroadcast will still be usable, but the cancellation that is passed here will not be usable.
*/ */
final public function broadcastForwardMessages(mixed $from_peer, array $message_ids, bool $drop_author = false, ?\danog\MadelineProto\Broadcast\Filter $filter = null, bool $pin = false, ?float $delay = null, ?\Amp\Cancellation $cancellation = null): int final public function broadcastForwardMessages(mixed $from_peer, array $message_ids, bool $drop_author = false, ?\danog\MadelineProto\Broadcast\Filter $filter = null, bool $pin = false, ?float $delay = null): int
{ {
return $this->wrapper->getAPI()->broadcastForwardMessages($from_peer, $message_ids, $drop_author, $filter, $pin, $delay, $cancellation); return $this->wrapper->getAPI()->broadcastForwardMessages($from_peer, $message_ids, $drop_author, $filter, $pin, $delay);
} }
/** /**
* Sends a list of messages to all peers (users, chats, channels) of the bot. * Sends a list of messages to all peers (users, chats, channels) of the bot.
@ -296,11 +294,10 @@ abstract class InternalDoc
* @param array $messages The messages to send: an array of arrays, containing parameters to pass to messages.sendMessage. * @param array $messages The messages to send: an array of arrays, containing parameters to pass to messages.sendMessage.
* @param bool $pin Whether to also pin the last sent message. * @param bool $pin Whether to also pin the last sent message.
* @param float|null $delay Number of seconds to wait between each peer. * @param float|null $delay Number of seconds to wait between each peer.
* @param ?Cancellation $cancellation Cancellation. Note: you may also use cancelBroadcast with the returned broadcast ID. Be aware that when running via web with limited execution time, the broadcast will continue correctly after a restart and cancelBroadcast will still be usable, but the cancellation that is passed here will not be usable.
*/ */
final public function broadcastMessages(array $messages, ?\danog\MadelineProto\Broadcast\Filter $filter = null, bool $pin = false, ?float $delay = null, ?\Amp\Cancellation $cancellation = null): int final public function broadcastMessages(array $messages, ?\danog\MadelineProto\Broadcast\Filter $filter = null, bool $pin = false, ?float $delay = null): int
{ {
return $this->wrapper->getAPI()->broadcastMessages($messages, $filter, $pin, $delay, $cancellation); return $this->wrapper->getAPI()->broadcastMessages($messages, $filter, $pin, $delay);
} }
/** /**
* Fork a new green thread and execute the passed function in the background. * Fork a new green thread and execute the passed function in the background.