Ward againt contact_id = 0 in UpdateContact worker

- Add parameter validation in Worker\UpdateContact::add
- Address https://github.com/friendica/friendica/issues/12487#issuecomment-1366833644
This commit is contained in:
Hypolite Petovan 2022-12-28 19:09:34 -05:00
parent 5af956b92f
commit e563de4de7
5 changed files with 71 additions and 17 deletions

View file

@ -47,6 +47,7 @@ use Friendica\Util\Images;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\Proxy; use Friendica\Util\Proxy;
use Friendica\Util\Strings; use Friendica\Util\Strings;
use Friendica\Worker\UpdateContact;
/** /**
* functions for interacting with a contact * functions for interacting with a contact
@ -362,7 +363,11 @@ class Contact
// Update the contact in the background if needed // Update the contact in the background if needed
if ($background_update && !self::isLocal($url) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) { if ($background_update && !self::isLocal($url) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']); try {
UpdateContact::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], $contact['id']);
} catch (\InvalidArgumentException $e) {
Logger::notice($e->getMessage(), ['contact' => $contact]);
}
} }
// Remove the internal fields // Remove the internal fields
@ -1276,7 +1281,11 @@ class Contact
$background_update = DI::config()->get('system', 'update_active_contacts') ? $contact['local-data'] : true; $background_update = DI::config()->get('system', 'update_active_contacts') ? $contact['local-data'] : true;
if ($background_update && !self::isLocal($url) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) { if ($background_update && !self::isLocal($url) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']); try {
UpdateContact::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], $contact['id']);
} catch (\InvalidArgumentException $e) {
Logger::notice($e->getMessage(), ['contact' => $contact]);
}
} }
if (empty($update) && (!empty($contact['uri-id']) || is_bool($update))) { if (empty($update) && (!empty($contact['uri-id']) || is_bool($update))) {
@ -3078,7 +3087,11 @@ class Contact
if ($probed) { if ($probed) {
self::updateFromProbeArray($contact_id, $ret); self::updateFromProbeArray($contact_id, $ret);
} else { } else {
Worker::add(Worker::PRIORITY_HIGH, 'UpdateContact', $contact_id); try {
UpdateContact::add(Worker::PRIORITY_HIGH, $contact['id']);
} catch (\InvalidArgumentException $e) {
Logger::notice($e->getMessage(), ['contact' => $contact]);
}
} }
$result['success'] = Protocol::follow($uid, $contact, $protocol); $result['success'] = Protocol::follow($uid, $contact, $protocol);
@ -3549,8 +3562,12 @@ class Contact
Worker::add(Worker::PRIORITY_LOW, 'AddContact', 0, $url); Worker::add(Worker::PRIORITY_LOW, 'AddContact', 0, $url);
++$added; ++$added;
} elseif (!empty($contact['network']) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) { } elseif (!empty($contact['network']) && Probe::isProbable($contact['network']) && ($contact['next-update'] < DateTimeFormat::utcNow())) {
Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id']); try {
++$updated; UpdateContact::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], $contact['id']);
++$updated;
} catch (\InvalidArgumentException $e) {
Logger::notice($e->getMessage(), ['contact' => $contact]);
}
} else { } else {
++$unchanged; ++$unchanged;
} }

View file

@ -26,8 +26,10 @@ use Friendica\Content\ContactSelector;
use Friendica\Content\Nav; use Friendica\Content\Nav;
use Friendica\Content\Pager; use Friendica\Content\Pager;
use Friendica\Content\Widget; use Friendica\Content\Widget;
use Friendica\Core\Logger;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Core\Theme; use Friendica\Core\Theme;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
@ -37,6 +39,7 @@ use Friendica\Model\User;
use Friendica\Module\Security\Login; use Friendica\Module\Security\Login;
use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Worker\UpdateContact;
/** /**
* Manages and show Contacts and their content * Manages and show Contacts and their content
@ -129,7 +132,11 @@ class Contact extends BaseModule
// pull feed and consume it, which should subscribe to the hub. // pull feed and consume it, which should subscribe to the hub.
Worker::add(Worker::PRIORITY_HIGH, 'OnePoll', $contact_id, 'force'); Worker::add(Worker::PRIORITY_HIGH, 'OnePoll', $contact_id, 'force');
} else { } else {
Worker::add(Worker::PRIORITY_HIGH, 'UpdateContact', $contact_id); try {
UpdateContact::add(Worker::PRIORITY_HIGH, $contact_id);
} catch (\InvalidArgumentException $e) {
Logger::notice($e->getMessage(), ['contact' => $contact, 'callstack' => System::callstack()]);
}
} }
} }

View file

@ -44,6 +44,7 @@ use Friendica\Util\Images;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\ParseUrl; use Friendica\Util\ParseUrl;
use Friendica\Util\Proxy; use Friendica\Util\Proxy;
use Friendica\Worker\UpdateContact;
/** /**
* Photo Module * Photo Module
@ -351,8 +352,12 @@ class Photo extends BaseModule
Logger::debug('Got return code for avatar', ['return code' => $curlResult->getReturnCode(), 'cid' => $id, 'url' => $contact['url'], 'avatar' => $url]); Logger::debug('Got return code for avatar', ['return code' => $curlResult->getReturnCode(), 'cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
} }
if ($update) { if ($update) {
Logger::info('Invalid file, contact update initiated', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]); try {
Worker::add(Worker::PRIORITY_LOW, 'UpdateContact', $id); UpdateContact::add(Worker::PRIORITY_LOW, $id);
Logger::info('Invalid file, contact update initiated', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
} catch (\InvalidArgumentException $e) {
Logger::notice($e->getMessage(), ['id' => $id, 'contact' => $contact]);
}
} else { } else {
Logger::info('Invalid file', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]); Logger::info('Invalid file', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
} }

View file

@ -22,15 +22,19 @@
namespace Friendica\Worker; namespace Friendica\Worker;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\Worker;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Network\HTTPException\InternalServerErrorException;
class UpdateContact class UpdateContact
{ {
/** /**
* Update contact data via probe * Update contact data via probe
* *
* @param int $contact_id Contact ID * @param int $contact_id Contact ID
* @return void * @return void
* @throws InternalServerErrorException
* @throws \ImagickException
*/ */
public static function execute(int $contact_id) public static function execute(int $contact_id)
{ {
@ -38,4 +42,19 @@ class UpdateContact
Logger::info('Updated from probe', ['id' => $contact_id, 'success' => $success]); Logger::info('Updated from probe', ['id' => $contact_id, 'success' => $success]);
} }
/**
* @param array|int $run_parameters Priority constant or array of options described in Worker::add
* @param int $contact_id
* @return int
* @throws InternalServerErrorException
*/
public static function add($run_parameters, int $contact_id): int
{
if (!$contact_id) {
throw new \InvalidArgumentException('Invalid value provided for contact_id');
}
return Worker::add($run_parameters, 'UpdateContact', $contact_id);
}
} }

View file

@ -63,15 +63,21 @@ class UpdateContacts
if (Contact::isLocal($contact['url'])) { if (Contact::isLocal($contact['url'])) {
continue; continue;
} }
if ((!empty($contact['gsid']) || !empty($contact['baseurl'])) && GServer::reachable($contact)) {
$stamp = (float)microtime(true); try {
$success = Contact::updateFromProbe($contact['id']); if ((!empty($contact['gsid']) || !empty($contact['baseurl'])) && GServer::reachable($contact)) {
Logger::debug('Direct update', ['id' => $contact['id'], 'count' => $count, 'duration' => round((float)microtime(true) - $stamp, 3), 'success' => $success]); $stamp = (float)microtime(true);
++$count; $success = Contact::updateFromProbe($contact['id']);
} elseif (Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateContact', $contact['id'])) { Logger::debug('Direct update', ['id' => $contact['id'], 'count' => $count, 'duration' => round((float)microtime(true) - $stamp, 3), 'success' => $success]);
Logger::debug('Update by worker', ['id' => $contact['id'], 'count' => $count]); ++$count;
++$count; } elseif (UpdateContact::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], $contact['id'])) {
Logger::debug('Update by worker', ['id' => $contact['id'], 'count' => $count]);
++$count;
}
} catch (\InvalidArgumentException $e) {
Logger::notice($e->getMessage(), ['contact' => $contact]);
} }
Worker::coolDown(); Worker::coolDown();
} }
DBA::close($contacts); DBA::close($contacts);