Skip to content

Commit

Permalink
Avoid issues with limited execution time
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Aug 23, 2024
1 parent e70de92 commit 30c786e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
17 changes: 6 additions & 11 deletions src/Broadcast/Broadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

namespace danog\MadelineProto\Broadcast;

use Amp\Cancellation;
use danog\MadelineProto\Broadcast\Action\ActionForward;
use danog\MadelineProto\Broadcast\Action\ActionSend;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -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 bool $pin Whether to also pin the last sent message.
* @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) {
if (isset($message['media']['_']) &&
Expand All @@ -69,11 +67,11 @@ public function broadcastMessages(array $messages, ?Filter $filter = null, bool
) {
$message['media'] = $this->methodCallAsyncRead(
'messages.uploadMedia',
['peer' => 'me', 'media' => $message['media'], 'cancellation' => $cancellation]
['peer' => 'me', 'media' => $message['media']]
);
}
} 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.
Expand All @@ -92,11 +90,10 @@ public function broadcastMessages(array $messages, ?Filter $filter = null, bool
* @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 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);
}

/**
Expand All @@ -113,16 +110,14 @@ public function broadcastForwardMessages(mixed $from_peer, array $message_ids, b
*
* @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 ?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
Assert::eq(unserialize(serialize($action))::class, $action::class);

$id = $this->broadcastId--;
$this->broadcasts[$id] = new InternalState($id, $this, $action, $filter ?? Filter::default(), $delay);
$cancellation?->subscribe(fn () => $this->cancelBroadcast($id));
return $id;
}
/**
Expand Down
15 changes: 6 additions & 9 deletions src/InternalDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,10 @@ final public function botLogin(string $token): ?array
*
* @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 ?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.
Expand All @@ -273,11 +272,10 @@ final public function broadcastCustom(\danog\MadelineProto\Broadcast\Action $act
* @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 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.
Expand All @@ -296,11 +294,10 @@ final public function broadcastForwardMessages(mixed $from_peer, array $message_
* @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 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.
Expand Down

0 comments on commit 30c786e

Please sign in to comment.