3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Addon;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Worker;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Tag;
31 use Friendica\Protocol\ActivityPub\Queue;
32 use Friendica\Protocol\Relay;
33 use Friendica\Util\DateTimeFormat;
37 public static function execute()
41 $last = DI::keyValue()->get('last_cron');
43 $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
46 $next = $last + ($poll_interval * 60);
48 Logger::notice('cron interval not reached');
53 Logger::notice('start');
55 // Ensure to have a .htaccess file.
56 // this is a precaution for systems that update automatically
57 $basepath = $a->getBasePath();
58 if (!file_exists($basepath . '/.htaccess') && is_writable($basepath)) {
59 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
62 if (DI::config()->get('system', 'delete_sleeping_processes')) {
63 self::deleteSleepingProcesses();
66 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
67 Hook::fork(Worker::PRIORITY_MEDIUM, 'cron');
70 Worker::add(Worker::PRIORITY_MEDIUM, 'PollContacts');
72 // Update contact information
73 Worker::add(Worker::PRIORITY_LOW, 'UpdateContacts');
75 // Update server information
76 Worker::add(Worker::PRIORITY_LOW, 'UpdateGServers');
78 // run the process to update server directories in the background
79 Worker::add(Worker::PRIORITY_LOW, 'UpdateServerDirectories');
81 // Expire and remove user entries
82 Worker::add(Worker::PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
84 // Call possible post update functions
85 Worker::add(Worker::PRIORITY_LOW, 'PostUpdate');
88 if ((DI::keyValue()->get('last_cron_hourly') ?? 0) + 3600 < time()) {
89 // Update trending tags cache for the community page
90 Tag::setLocalTrendingHashtags(24, 20);
91 Tag::setGlobalTrendingHashtags(24, 20);
93 // Remove old pending posts from the queue
96 // Process all unprocessed entries
99 // Search for new contacts in the directory
100 if (DI::config()->get('system', 'synchronize_directory')) {
101 Worker::add(Worker::PRIORITY_LOW, 'PullDirectory');
104 // Clear cache entries
105 Worker::add(Worker::PRIORITY_LOW, 'ClearCache');
107 // Update interaction scores
108 Worker::add(Worker::PRIORITY_LOW, 'UpdateScores');
110 DI::keyValue()->set('last_cron_hourly', time());
113 // Daily maintenance cron calls
114 if (Worker::isInMaintenanceWindow(true)) {
116 Worker::add(Worker::PRIORITY_LOW, 'UpdateContactBirthdays');
118 Worker::add(Worker::PRIORITY_LOW, 'UpdatePhotoAlbums');
120 Worker::add(Worker::PRIORITY_LOW, 'ExpirePosts');
122 Worker::add(Worker::PRIORITY_LOW, 'ExpireActivities');
124 Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedTags');
126 Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedContacts');
128 Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedAvatars');
130 // check upstream version?
131 Worker::add(Worker::PRIORITY_LOW, 'CheckVersion');
133 Worker::add(Worker::PRIORITY_LOW, 'CheckDeletedContacts');
135 Worker::add(Worker::PRIORITY_LOW, 'UpdateAllSuggestions');
137 if (DI::config()->get('system', 'optimize_tables')) {
138 Worker::add(Worker::PRIORITY_LOW, 'OptimizeTables');
141 $users = DBA::select('owner-view', ['uid'], ["`homepage_verified` OR (`last-activity` > ? AND `homepage` != ?)", DateTimeFormat::utc('now - 7 days', 'Y-m-d'), '']);
142 while ($user = DBA::fetch($users)) {
143 Worker::add(Worker::PRIORITY_LOW, 'CheckRelMeProfileLink', $user['uid']);
147 // Update contact relations for our users
148 $users = DBA::select('user', ['uid'], ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `uid` > ?", 0]);
149 while ($user = DBA::fetch($users)) {
150 Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $user['uid']);
154 // Resubscribe to relay servers
155 Relay::reSubscribe();
157 // Update "blocked" status of servers
158 Worker::add(Worker::PRIORITY_LOW, 'UpdateBlockedServers');
162 DI::keyValue()->set('last_cron_daily', time());
165 Logger::notice('end');
167 DI::keyValue()->set('last_cron', time());
171 * Kill sleeping database processes
175 private static function deleteSleepingProcesses()
177 Logger::info('Looking for sleeping processes');
179 DBA::deleteSleepingProcesses();