]> git.mxchange.org Git - friendica.git/commitdiff
Avoid warnings in the Jetstream process
authorMichael <heluecht@pirati.ca>
Tue, 27 May 2025 21:26:17 +0000 (21:26 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 29 May 2025 04:35:31 +0000 (04:35 +0000)
bin/console.php
src/Protocol/ATProtocol/Actor.php
src/Protocol/ATProtocol/Jetstream.php

index a20f3f7e025348bd31fda63a0deaad6737b7fbe6..88382e1665ca4e28694e69ce91752e84f99e7332 100755 (executable)
@@ -13,6 +13,9 @@ if (php_sapi_name() !== 'cli') {
        exit();
 }
 
+// Ensure that te console is executed from the base path of the installation
+chdir(dirname(__DIR__));
+
 require dirname(__DIR__) . '/vendor/autoload.php';
 
 $container = \Friendica\Core\DiceContainer::fromBasePath(dirname(__DIR__));
index 38da819dfc4186008bba7d267761e14a9b9b25f6..4dae8b75cf357169d31d413f5ad671d6aea678e2 100755 (executable)
@@ -133,7 +133,7 @@ class Actor
                }
 
                $directory = $this->atprotocol->get(ATProtocol::DIRECTORY . '/' . $profile->did);
-               if (!empty($directory)) {
+               if (!empty($directory->service)) {
                        foreach ($directory->service as $service) {
                                if (($service->id == '#atproto_pds') && ($service->type == 'AtprotoPersonalDataServer') && !empty($service->serviceEndpoint)) {
                                        $fields['baseurl'] = $service->serviceEndpoint;
index 8051e47126830e9bbcea35e0a50b15130c1bb934..86e28c0124a2f09395698cc6515452502584626a 100755 (executable)
@@ -84,6 +84,8 @@ class Jetstream
                $timeout_limit = 10;
                $timestamp     = $this->keyValue->get('jetstream_timestamp') ?? 0;
                $cursor        = '';
+               $this->logger->notice('Start listening');
+
                while (true) {
                        if ($timestamp) {
                                $cursor = '&cursor=' . $timestamp;
@@ -92,12 +94,21 @@ class Jetstream
 
                        $this->syncContacts();
                        try {
+                               set_error_handler(function ($errno, $errstr, $errfile, $errline) {
+                                       restore_error_handler();
+                                       throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
+                               }, E_WARNING);
                                // @todo make the path configurable
                                $this->client = new \WebSocket\Client('wss://jetstream1.us-west.bsky.network/subscribe?requireHello=true' . $cursor);
                                $this->client->setTimeout($timeout);
                                $this->client->setLogger($this->logger);
+                               restore_error_handler();
                        } catch (\WebSocket\ConnectionException $e) {
-                               $this->logger->error('Error while trying to establish the connection', ['code' => $e->getCode(), 'message' => $e->getMessage()]);
+                               $this->logger->error('Error while trying to establish the connection', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
+                               echo "Connection wasn't established.\n";
+                               exit(1);
+                       } catch (\ErrorException $e) {
+                               $this->logger->notice('Warning while trying to establish the connection', ['code' => $e->getCode(), 'severity' => $e->getSeverity(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
                                echo "Connection wasn't established.\n";
                                exit(1);
                        }
@@ -105,7 +116,13 @@ class Jetstream
                        $last_timeout = time();
                        while (true) {
                                try {
-                                       $message = @$this->client->receive();
+                                       set_error_handler(function ($errno, $errstr, $errfile, $errline) {
+                                               restore_error_handler();
+                                               throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
+                                       }, E_WARNING);
+                                       $message = $this->client->receive();
+                                       restore_error_handler();
+
                                        if (empty($message)) {
                                                $this->logger->notice('Empty message received');
                                                break;
@@ -128,19 +145,31 @@ class Jetstream
                                                        break;
                                                }
                                                $this->logger->notice('Timeout', ['duration' => $timeout_duration, 'timestamp' => $timestamp, 'code' => $e->getCode(), 'message' => $e->getMessage()]);
+                                               break;
                                        } else {
-                                               $this->logger->error('Error', ['code' => $e->getCode(), 'message' => $e->getMessage()]);
+                                               $this->logger->error('Error while trying to receive a message', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
                                                break;
                                        }
+                               } catch (\ErrorException $e) {
+                                       $this->logger->notice('Warning while trying to receive a message', ['code' => $e->getCode(), 'severity' => $e->getSeverity(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
+                                       break;
                                }
                                $last_timeout = time();
                        }
                        try {
+                               set_error_handler(function ($errno, $errstr, $errfile, $errline) {
+                                       restore_error_handler();
+                                       throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
+                               }, E_WARNING);
                                $this->client->close();
+                               restore_error_handler();
                        } catch (\WebSocket\ConnectionException $e) {
-                               $this->logger->error('Error while trying to close the connection', ['code' => $e->getCode(), 'message' => $e->getMessage()]);
+                               $this->logger->error('Error while trying to close the connection', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
+                       } catch (\ErrorException $e) {
+                               $this->logger->notice('Warning while trying to close the connection', ['code' => $e->getCode(), 'severity' => $e->getSeverity(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
                        }
                }
+               $this->logger->notice('Stop listening');
        }
 
        /**
@@ -234,9 +263,16 @@ class Jetstream
                        ]
                ];
                try {
+                       set_error_handler(function ($errno, $errstr, $errfile, $errline) {
+                               restore_error_handler();
+                               throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
+                       }, E_WARNING);
                        $this->client->send(json_encode($update));
+                       restore_error_handler();
                } catch (\WebSocket\ConnectionException $e) {
-                       $this->logger->error('Error while trying to send options.', ['code' => $e->getCode(), 'message' => $e->getMessage()]);
+                       $this->logger->error('Error while trying to send options.', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
+               } catch (\ErrorException $e) {
+                       $this->logger->notice('Warning while trying to send options.', ['code' => $e->getCode(), 'severity' => $e->getSeverity(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
                }
        }