X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2FWorker%2FUpdatePublicContacts.php;h=939d9fa8d77eadeaa4e1cfaea1d3c2971acce9a9;hb=edbdfbae6b78086c220c0ad116c0fa7deb03a6e8;hp=519478e1110af74e29676e07964250a389653ee8;hpb=9702b1d67975b24bf6ad26eb28a24fada1b79a36;p=friendica.git diff --git a/src/Worker/UpdatePublicContacts.php b/src/Worker/UpdatePublicContacts.php index 519478e111..939d9fa8d7 100644 --- a/src/Worker/UpdatePublicContacts.php +++ b/src/Worker/UpdatePublicContacts.php @@ -36,28 +36,61 @@ class UpdatePublicContacts public static function execute() { $count = 0; - $last_updated = DateTimeFormat::utc('now - 1 week'); - $condition = ["`network` IN (?, ?, ?, ?) AND `uid` = ? AND NOT `self` AND `last-update` < ?", - Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, 0, $last_updated]; + $ids = []; + $base_condition = ['network' => Protocol::FEDERATED, 'uid' => 0, 'self' => false]; - if (DI::config()->get('system', 'update_active_contacts')) { - $condition = DBA::mergeConditions($condition, ["(`id` IN (SELECT `author-id` FROM `item`) OR + $existing = Worker::countWorkersByCommand('UpdateContact'); + Logger::info('Already existing jobs', ['existing' => $existing]); + if ($existing > 100) { + return; + } + + $limit = 100 - $existing; + + if (!DI::config()->get('system', 'update_active_contacts')) { + $part = 3; + // Add every contact (mostly failed ones) that hadn't been updated for six months + $condition = DBA::mergeConditions($base_condition, + ["`last-update` < ?", DateTimeFormat::utc('now - 6 month')]); + $ids = self::getContactsToUpdate($condition, $ids, round($limit / $part)); + + // Add every non failed contact that hadn't been updated for a month + $condition = DBA::mergeConditions($base_condition, + ["NOT `failed` AND `last-update` < ?", DateTimeFormat::utc('now - 1 month')]); + $ids = self::getContactsToUpdate($condition, $ids, round($limit / $part)); + } else { + $part = 1; + } + + // Add every contact our system interacted with and hadn't been updated for a week + $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`))"]); + `id` IN (SELECT `cid` FROM `post-tag`) OR `id` IN (SELECT `cid` FROM `user-contact`)) AND + `last-update` < ?", DateTimeFormat::utc('now - 1 week')]); + $ids = self::getContactsToUpdate($condition, $ids, round($limit / $part)); + + foreach ($ids as $id) { + Worker::add(PRIORITY_LOW, "UpdateContact", $id); + ++$count; } - $oldest_date = ''; - $oldest_id = ''; - $contacts = DBA::select('contact', ['id', 'last-update'], $condition, ['limit' => 100, 'order' => ['last-update']]); + Logger::info('Initiated update for public contacts', ['count' => $count]); + } + + /** + * 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)) { - if (empty($oldest_id)) { - $oldest_id = $contact['id']; - $oldest_date = $contact['last-update']; - } - Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id']); - ++$count; + $ids[] = $contact['id']; } - Logger::info('Initiated update for public contacts', ['interval' => $count, 'id' => $oldest_id, 'oldest' => $oldest_date]); DBA::close($contacts); + return $ids; } }