<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
namespace Friendica\Worker;
use Friendica\Core\Logger;
-use Friendica\Core\Protocol;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
+use Friendica\Model\Contact;
+use Friendica\Model\GServer;
use Friendica\Util\DateTimeFormat;
/**
{
public static function execute()
{
- $base_condition = ['network' => array_merge(Protocol::FEDERATED, [Protocol::ZOT, Protocol::PHANTOM]), 'self' => false];
-
$update_limit = DI::config()->get('system', 'contact_update_limit');
if (empty($update_limit)) {
return;
return;
}
- // Add every contact our system interacted with and hadn't been updated for a week if unarchived
- // or for a month if archived.
- $condition = DBA::mergeConditions($base_condition, ["(`id` IN (SELECT `author-id` FROM `item`) OR
- `id` IN (SELECT `owner-id` FROM `item`) OR `id` IN (SELECT `causer-id` FROM `item`) OR
- `id` IN (SELECT `cid` FROM `post-tag`) OR `id` IN (SELECT `cid` FROM `user-contact`) OR `uid` != ?) AND
- (`last-update` < ? OR (NOT `archive` AND `last-update` < ?))",
- 0, DateTimeFormat::utc('now - 1 month'), DateTimeFormat::utc('now - 1 week')]);
- Logger::info('Updatable interacting federated contacts', ['count' => DBA::count('contact', $condition)]);
- $ids = self::getContactsToUpdate($condition, [], $limit);
- Logger::info('Fetched interacting federated contacts', ['count' => count($ids)]);
+ Logger::info('Updating contact', ['count' => $limit]);
+
+ $condition = ['self' => false];
- if (!DI::config()->get('system', 'update_active_contacts')) {
- // Add every contact (mostly failed ones) that hadn't been updated for six months
- // and every non failed contact that hadn't been updated for a month
- $condition = DBA::mergeConditions($base_condition,
- ["(`last-update` < ? OR (NOT `archive` AND `last-update` < ?))",
- DateTimeFormat::utc('now - 6 month'), DateTimeFormat::utc('now - 1 month')]);
- Logger::info('Updatable federated contacts', ['count' => DBA::count('contact', $condition)]);
- $previous = count($ids);
- $ids = self::getContactsToUpdate($condition, $ids, $limit - $previous);
- Logger::info('Fetched federated contacts', ['count' => count($ids) - $previous]);
+ if (DI::config()->get('system', 'update_active_contacts')) {
+ $condition = array_merge(['local-data' => true], $condition);
}
+ $condition = DBA::mergeConditions(["`next-update` < ?", DateTimeFormat::utcNow()], $condition);
+ $contacts = DBA::select('contact', ['id', 'url', 'gsid', 'baseurl'], $condition, ['order' => ['next-update'], 'limit' => $limit]);
$count = 0;
- foreach ($ids as $id) {
- if (Worker::add(PRIORITY_LOW, "UpdateContact", $id)) {
- ++$count;
+ while ($contact = DBA::fetch($contacts)) {
+ if (Contact::isLocal($contact['url'])) {
+ continue;
}
- }
- Logger::info('Initiated update for federated contacts', ['count' => $count]);
- }
+ 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]);
+ }
- /**
- * Returns contact ids based on a given condition
- *
- * @param array $condition
- * @param array $ids
- * @return array contact ids
- */
- private static function getContactsToUpdate(array $condition, array $ids = [], int $limit)
- {
- $contacts = DBA::select('contact', ['id'], $condition, ['limit' => $limit, 'order' => ['last-update']]);
- while ($contact = DBA::fetch($contacts)) {
- $ids[] = $contact['id'];
+ Worker::coolDown();
}
DBA::close($contacts);
- return $ids;
+
+ Logger::info('Initiated update for federated contacts', ['count' => $count]);
}
}