Fix self-restart, avoid exceptions surfacing in the download bot

This commit is contained in:
Daniil Gentili 2019-12-29 20:32:19 +01:00
parent 4de96f2e45
commit f85f7bd8ba
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
9 changed files with 84 additions and 31 deletions

View File

@ -174,12 +174,12 @@ $settings = [
$MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings);
$MadelineProto->async(true);
while (true) {
try {
$MadelineProto->loop(function () use ($MadelineProto) {
yield $MadelineProto->start();
yield $MadelineProto->setEventHandler('\EventHandler');
});
while (true) {
try {
$MadelineProto->loop();
} catch (\Throwable $e) {
try {

View File

@ -28,14 +28,14 @@ namespace danog\MadelineProto\Async;
class AsyncParameters
{
/**
* Async callable
* Async callable.
*
* @var callable
*/
private $callable;
/**
* Create async parameters
* Create async parameters.
*
* @param callable $callable Async callable that will return parameters
*/
@ -46,7 +46,7 @@ class AsyncParameters
/**
* Create async parameters
* Create async parameters.
*
* @param callable $callable Async callable that will return parameters
*/
@ -56,7 +56,7 @@ class AsyncParameters
}
/**
* Get parameters asynchronously
* Get parameters asynchronously.
*
* @return \Generator<array>|\Amp\Promise<array>
*/

View File

@ -534,9 +534,13 @@ class DataCenterConnection implements JsonSerializable
public function waitGetConnection(): Promise
{
if (empty($this->availableConnections)) {
return $this->connectionsPromise->onResolve(function ($e, $v) {
return $this->getConnection();
});
$deferred = new Deferred;
$this->connectionsPromise->onResolve(
function ($e, $v) use ($deferred) {
$deferred->resolve($this->getConnection());
}
);
return $deferred->promise();
}
return new Success($this->getConnection());
}

View File

@ -19,22 +19,55 @@
namespace danog\MadelineProto;
/**
* File callback interface.
*/
class FileCallback implements FileCallbackInterface
{
/**
* File to download/upload.
*
* @var mixed
*/
private $file;
/**
* Callback.
*
* @var callable
*/
private $callback;
public function __construct($file, $callback)
/**
* Construct file callback.
*
* @param mixed $file File to download/upload
* @param callable $callback Callback
*/
public function __construct($file, callable $callback)
{
$this->file = $file;
$this->callback = $callback;
}
/**
* Get file.
*
* @return mixed
*/
public function getFile()
{
return $this->file;
}
/**
* Invoke callback.
*
* @param int $percent Percent
* @param int $speed Speed in mbps
* @param int $time Time
*
* @return mixed
*/
public function __invoke($percent, $speed, $time)
{
$callback = $this->callback;

View File

@ -19,9 +19,26 @@
namespace danog\MadelineProto;
/**
* File callback interface.
*/
interface FileCallbackInterface
{
/**
* Get file.
*
* @return mixed
*/
public function getFile();
/**
* Invoke callback.
*
* @param int $percent Percent
* @param int $speed Speed in mbps
* @param int $time Time
*
* @return mixed
*/
public function __invoke($percent, $speed, $time);
}

View File

@ -4403,30 +4403,30 @@ class InternalDoc extends APIFactory
* Upload file.
*
* @param FileCallbackInterface|string|array $file File, URL or Telegram file to upload
* @param string $file_name File name
* @param string $fileName File name
* @param callable $cb Callback (DEPRECATED, use FileCallbackInterface)
* @param boolean $encrypted Whether to encrypt file for secret chats
*
* @return \Generator<array>
*/
public function upload($file, string $file_name = '', $cb = null, bool $encrypted = false, array $extra = [])
public function upload($file, string $fileName = '', $cb = null, bool $encrypted = false, array $extra = [])
{
return $this->__call(__FUNCTION__, [$file, $file_name, $cb, $encrypted, $extra]);
return $this->__call(__FUNCTION__, [$file, $fileName, $cb, $encrypted, $extra]);
}
/**
* Upload file from URL.
*
* @param string|FileCallbackInterface $url URL of file
* @param integer $size Size of file
* @param string $file_name File name
* @param string $fileName File name
* @param callable $cb Callback (DEPRECATED, use FileCallbackInterface)
* @param boolean $encrypted Whether to encrypt file for secret chats
*
* @return array
*/
public function uploadFromUrl($url, int $size = 0, string $file_name = '', $cb = null, bool $encrypted = false, array $extra = [])
public function uploadFromUrl($url, int $size = 0, string $fileName = '', $cb = null, bool $encrypted = false, array $extra = [])
{
return $this->__call(__FUNCTION__, [$url, $size, $file_name, $cb, $encrypted, $extra]);
return $this->__call(__FUNCTION__, [$url, $size, $fileName, $cb, $encrypted, $extra]);
}
/**
* Upload file from stream.
@ -4434,15 +4434,15 @@ class InternalDoc extends APIFactory
* @param mixed $stream PHP resource or AMPHP async stream
* @param integer $size File size
* @param string $mime Mime type
* @param string $file_name File name
* @param string $fileName File name
* @param callable $cb Callback (DEPRECATED, use FileCallbackInterface)
* @param boolean $encrypted Whether to encrypt file for secret chats
*
* @return array
*/
public function uploadFromStream($stream, int $size, string $mime, string $file_name = '', $cb = null, bool $encrypted = false, array $extra = [])
public function uploadFromStream($stream, int $size, string $mime, string $fileName = '', $cb = null, bool $encrypted = false, array $extra = [])
{
return $this->__call(__FUNCTION__, [$stream, $size, $mime, $file_name, $cb, $encrypted, $extra]);
return $this->__call(__FUNCTION__, [$stream, $size, $mime, $fileName, $cb, $encrypted, $extra]);
}
/**
* Upload file from callable.
@ -4453,29 +4453,29 @@ class InternalDoc extends APIFactory
* @param mixed $callable Callable
* @param integer $size File size
* @param string $mime Mime type
* @param string $file_name File name
* @param string $fileName File name
* @param callable $cb Callback (DEPRECATED, use FileCallbackInterface)
* @param boolean $seekable Whether chunks can be fetched out of order
* @param boolean $encrypted Whether to encrypt file for secret chats
*
* @return \Generator<array>
*/
public function uploadFromCallable($callable, int $size, string $mime, string $file_name = '', $cb = null, bool $seekable = true, bool $encrypted = false, array $extra = [])
public function uploadFromCallable($callable, int $size, string $mime, string $fileName = '', $cb = null, bool $seekable = true, bool $encrypted = false, array $extra = [])
{
return $this->__call(__FUNCTION__, [$callable, $size, $mime, $file_name, $cb, $seekable, $encrypted, $extra]);
return $this->__call(__FUNCTION__, [$callable, $size, $mime, $fileName, $cb, $seekable, $encrypted, $extra]);
}
/**
* Upload file to secret chat.
*
* @param FileCallbackInterface|string|array $file File, URL or Telegram file to upload
* @param string $file_name File name
* @param string $fileName File name
* @param callable $cb Callback (DEPRECATED, use FileCallbackInterface)
*
* @return \Generator<array>
*/
public function uploadEncrypted($file, string $file_name = '', $cb = null, array $extra = [])
public function uploadEncrypted($file, string $fileName = '', $cb = null, array $extra = [])
{
return $this->__call(__FUNCTION__, [$file, $file_name, $cb, $extra]);
return $this->__call(__FUNCTION__, [$file, $fileName, $cb, $extra]);
}
/**
* Reupload telegram file.

View File

@ -23,7 +23,6 @@ use Amp\Deferred;
use Amp\Promise;
use Amp\Success;
use danog\MadelineProto\Async\AsyncParameters;
use danog\MadelineProto\Async\Parameters;
use danog\MadelineProto\Tools;
use function Amp\Promise\all;

View File

@ -412,7 +412,7 @@ trait Files
*/
private $partSize;
/**
* Offset for callback
* Offset for callback.
*
* @var int
*/
@ -460,7 +460,7 @@ trait Files
return $this->read[$offset]->promise();
}
/**
* Read callback, called when the chunk is read and fully resent
* Read callback, called when the chunk is read and fully resent.
*
* @return void
*/

View File

@ -131,7 +131,7 @@ trait Loop
$params = $_GET;
$params['MadelineSelfRestart'] = Tools::randomInt();
$url = \explode($uri, '?', 2)[0] ?? '';
$url = \explode('?', $uri, 2)[0] ?? '';
$query = \http_build_query($params);
$uri = \implode('?', [$url, $query]);