2020-09-01 10:09:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2024-01-02 21:57:26 +01:00
|
|
|
* @copyright Copyright (C) 2010-2024, the Friendica project
|
2020-09-01 10:09:16 +02:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Worker;
|
|
|
|
|
|
|
|
use Friendica\Core\Logger;
|
|
|
|
use Friendica\Core\Worker;
|
|
|
|
use Friendica\Database\DBA;
|
2020-09-27 13:55:31 +02:00
|
|
|
use Friendica\DI;
|
2022-12-28 15:56:12 +01:00
|
|
|
use Friendica\Model\Contact;
|
|
|
|
use Friendica\Model\GServer;
|
2020-09-01 10:09:16 +02:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
|
|
|
|
/**
|
2020-12-04 06:53:11 +01:00
|
|
|
* Update federated contacts
|
2020-09-01 10:09:16 +02:00
|
|
|
*/
|
2020-12-04 06:53:11 +01:00
|
|
|
class UpdateContacts
|
2020-09-01 10:09:16 +02:00
|
|
|
{
|
|
|
|
public static function execute()
|
|
|
|
{
|
2020-12-04 06:53:11 +01:00
|
|
|
$update_limit = DI::config()->get('system', 'contact_update_limit');
|
|
|
|
if (empty($update_limit)) {
|
2020-12-03 16:47:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-04 06:53:11 +01:00
|
|
|
$updating = Worker::countWorkersByCommand('UpdateContact');
|
|
|
|
$limit = $update_limit - $updating;
|
|
|
|
if ($limit <= 0) {
|
|
|
|
Logger::info('The number of currently running jobs exceed the limit');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-13 11:01:48 +02:00
|
|
|
Logger::info('Updating contact', ['count' => $limit]);
|
2021-03-01 23:19:47 +01:00
|
|
|
|
2022-08-13 11:01:48 +02:00
|
|
|
$condition = ['self' => false];
|
2021-03-01 23:19:47 +01:00
|
|
|
|
2022-08-13 11:01:48 +02:00
|
|
|
if (DI::config()->get('system', 'update_active_contacts')) {
|
|
|
|
$condition = array_merge(['local-data' => true], $condition);
|
2020-10-03 12:52:34 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 10:08:58 +02:00
|
|
|
$condition = DBA::mergeConditions(["`next-update` < ?", DateTimeFormat::utcNow()], $condition);
|
2022-12-28 15:56:12 +01:00
|
|
|
$contacts = DBA::select('contact', ['id', 'url', 'gsid', 'baseurl'], $condition, ['order' => ['next-update'], 'limit' => $limit]);
|
2020-12-04 08:54:29 +01:00
|
|
|
$count = 0;
|
2022-08-13 11:01:48 +02:00
|
|
|
while ($contact = DBA::fetch($contacts)) {
|
2022-12-28 15:56:12 +01:00
|
|
|
if (Contact::isLocal($contact['url'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-29 01:09:34 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if ((!empty($contact['gsid']) || !empty($contact['baseurl'])) && GServer::reachable($contact)) {
|
|
|
|
$stamp = (float)microtime(true);
|
|
|
|
$success = Contact::updateFromProbe($contact['id']);
|
|
|
|
Logger::debug('Direct update', ['id' => $contact['id'], 'count' => $count, 'duration' => round((float)microtime(true) - $stamp, 3), 'success' => $success]);
|
|
|
|
++$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]);
|
2020-12-05 22:07:48 +01:00
|
|
|
}
|
2022-12-29 01:09:34 +01:00
|
|
|
|
2022-09-21 21:03:07 +02:00
|
|
|
Worker::coolDown();
|
2022-12-28 16:16:07 +01:00
|
|
|
}
|
2022-08-13 11:01:48 +02:00
|
|
|
DBA::close($contacts);
|
2020-09-27 13:55:31 +02:00
|
|
|
|
2020-12-04 06:53:11 +01:00
|
|
|
Logger::info('Initiated update for federated contacts', ['count' => $count]);
|
2020-10-03 12:52:34 +02:00
|
|
|
}
|
2020-09-01 10:09:16 +02:00
|
|
|
}
|