friendica-github/src/Worker/Cron.php

171 lines
4.8 KiB
PHP
Raw Normal View History

<?php
2024-08-24 15:27:00 +02:00
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace Friendica\Worker;
use Friendica\Core\Addon;
use Friendica\Core\Hook;
2018-10-29 17:20:46 -04:00
use Friendica\Core\Logger;
use Friendica\Core\Worker;
2021-01-06 23:05:55 +00:00
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Tag;
2022-05-24 07:02:42 +00:00
use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat;
class Cron
{
public static function execute()
{
$basepath = DI::appHelper()->getBasePath();
$last = DI::keyValue()->get('last_cron');
$poll_interval = intval(DI::config()->get('system', 'cron_interval'));
if ($last) {
$next = $last + ($poll_interval * 60);
if ($next > time()) {
Logger::notice('cron interval not reached');
return;
}
}
2020-09-01 08:30:12 +00:00
Logger::notice('start');
// Ensure to have a .htaccess file.
// this is a precaution for systems that update automatically
if (!file_exists($basepath . '/.htaccess') && is_writable($basepath)) {
copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
}
2021-01-06 23:05:55 +00:00
if (DI::config()->get('system', 'delete_sleeping_processes')) {
self::deleteSleepingProcesses();
}
// Fork the cron jobs in separate parts to avoid problems when one of them is crashing
2022-10-17 05:49:55 +00:00
Hook::fork(Worker::PRIORITY_MEDIUM, 'cron');
2020-09-01 08:30:12 +00:00
// Poll contacts
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_MEDIUM, 'PollContacts');
2020-09-01 08:30:12 +00:00
// Update contact information
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'UpdateContacts');
2020-09-01 08:30:12 +00:00
2020-12-03 15:47:50 +00:00
// Update server information
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'UpdateGServers');
2020-12-03 15:47:50 +00:00
2019-12-21 18:57:00 +00:00
// run the process to update server directories in the background
if (DI::config()->get('system', 'poco_discovery')) {
Worker::add(Worker::PRIORITY_LOW, 'UpdateServerDirectories');
}
// Expire and remove user entries
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
// Call possible post update functions
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'PostUpdate');
2020-09-01 08:30:12 +00:00
// Hourly cron calls
if ((DI::keyValue()->get('last_cron_hourly') ?? 0) + 3600 < time()) {
// Update trending tags cache for the community page
Tag::setLocalTrendingHashtags(24, 20);
Tag::setGlobalTrendingHashtags(24, 20);
2022-07-24 13:09:35 +00:00
// Process all unprocessed entries
Worker::add(Worker::PRIORITY_LOW, 'ProcessUnprocessedEntries');
2022-07-24 13:09:35 +00:00
2020-09-01 08:30:12 +00:00
// Search for new contacts in the directory
if (DI::config()->get('system', 'synchronize_directory')) {
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'PullDirectory');
2020-09-01 08:30:12 +00:00
}
// Clear cache entries
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'ClearCache');
2020-09-01 08:30:12 +00:00
2023-08-30 19:17:42 +00:00
// Update interaction scores
Worker::add(Worker::PRIORITY_LOW, 'UpdateScores');
DI::keyValue()->set('last_cron_hourly', time());
2020-09-01 08:30:12 +00:00
}
2020-10-17 12:39:42 +00:00
// Daily maintenance cron calls
if (Worker::isInMaintenanceWindow(true)) {
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'UpdateContactBirthdays');
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'UpdatePhotoAlbums');
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'ExpirePosts');
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'ExpireActivities');
2020-08-26 05:33:37 +00:00
2024-01-21 16:24:59 +00:00
Worker::add(Worker::PRIORITY_LOW, 'ExpireSearchIndex');
Worker::add(Worker::PRIORITY_LOW, 'Expire');
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedTags');
2022-04-24 15:27:20 +00:00
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedContacts');
2020-12-05 21:07:48 +00:00
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedAvatars');
Worker::add(Worker::PRIORITY_LOW, 'NodeInfo');
// check upstream version?
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'CheckVersion');
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'CheckDeletedContacts');
2022-11-30 05:59:27 +00:00
Worker::add(Worker::PRIORITY_LOW, 'UpdateAllSuggestions');
2020-08-19 19:41:22 +00:00
if (DI::config()->get('system', 'optimize_tables')) {
2022-10-17 05:49:55 +00:00
Worker::add(Worker::PRIORITY_LOW, 'OptimizeTables');
2020-08-19 18:16:48 +00:00
}
2022-07-24 09:26:52 +00:00
2022-12-23 06:26:58 +00:00
$users = DBA::select('owner-view', ['uid'], ["`homepage_verified` OR (`last-activity` > ? AND `homepage` != ?)", DateTimeFormat::utc('now - 7 days', 'Y-m-d'), '']);
while ($user = DBA::fetch($users)) {
Worker::add(Worker::PRIORITY_LOW, 'CheckRelMeProfileLink', $user['uid']);
}
DBA::close($users);
2023-01-01 09:36:24 -05:00
// Update contact relations for our users
$users = DBA::select('user', ['uid'], ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `uid` > ?", 0]);
while ($user = DBA::fetch($users)) {
Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $user['uid']);
}
DBA::close($users);
2022-05-24 07:02:42 +00:00
// Resubscribe to relay servers
Relay::reSubscribe();
2022-07-24 09:26:52 +00:00
// Update "blocked" status of servers
Worker::add(Worker::PRIORITY_LOW, 'UpdateBlockedServers');
Addon::reload();
DI::keyValue()->set('last_cron_daily', time());
}
2020-09-01 08:30:12 +00:00
Logger::notice('end');
DI::keyValue()->set('last_cron', time());
}
2021-01-06 23:05:55 +00:00
/**
* Kill sleeping database processes
*
* @return void
*/
private static function deleteSleepingProcesses()
{
Logger::info('Looking for sleeping processes');
2023-01-01 09:36:24 -05:00
2023-05-13 22:14:52 +02:00
DBA::deleteSleepingProcesses();
2021-01-06 23:05:55 +00:00
}
}