3 * @copyright Copyright (C) 2010-2022, 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\Hook;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
29 use Friendica\Model\Tag;
30 use Friendica\Protocol\ActivityPub\Queue;
31 use Friendica\Protocol\Relay;
35 public static function execute()
39 $last = DI::config()->get('system', 'last_cron');
41 $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
44 $next = $last + ($poll_interval * 60);
46 Logger::notice('cron intervall not reached');
51 Logger::notice('start');
53 // Ensure to have a .htaccess file.
54 // this is a precaution for systems that update automatically
55 $basepath = $a->getBasePath();
56 if (!file_exists($basepath . '/.htaccess') && is_writable($basepath)) {
57 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
60 if (DI::config()->get('system', 'delete_sleeping_processes')) {
61 self::deleteSleepingProcesses();
64 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
65 Hook::fork(Worker::PRIORITY_MEDIUM, 'cron');
68 Worker::add(Worker::PRIORITY_MEDIUM, 'PollContacts');
70 // Update contact information
71 Worker::add(Worker::PRIORITY_LOW, 'UpdateContacts');
73 // Update server information
74 Worker::add(Worker::PRIORITY_LOW, 'UpdateGServers');
76 // run the process to update server directories in the background
77 Worker::add(Worker::PRIORITY_LOW, 'UpdateServerDirectories');
79 // Expire and remove user entries
80 Worker::add(Worker::PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
82 // Call possible post update functions
83 Worker::add(Worker::PRIORITY_LOW, 'PostUpdate');
86 if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) {
88 // Update trending tags cache for the community page
89 Tag::setLocalTrendingHashtags(24, 20);
90 Tag::setGlobalTrendingHashtags(24, 20);
92 // Remove old pending posts from the queue
95 // Process all unprocessed entries
98 // Search for new contacts in the directory
99 if (DI::config()->get('system', 'synchronize_directory')) {
100 Worker::add(Worker::PRIORITY_LOW, 'PullDirectory');
103 // Clear cache entries
104 Worker::add(Worker::PRIORITY_LOW, 'ClearCache');
106 DI::config()->set('system', 'last_cron_hourly', time());
109 // Daily maintenance cron calls
110 if (Worker::isInMaintenanceWindow(true)) {
112 Worker::add(Worker::PRIORITY_LOW, 'UpdateContactBirthdays');
114 Worker::add(Worker::PRIORITY_LOW, 'UpdatePhotoAlbums');
116 Worker::add(Worker::PRIORITY_LOW, 'ExpirePosts');
118 Worker::add(Worker::PRIORITY_LOW, 'ExpireActivities');
120 Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedTags');
122 Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedContacts');
124 Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedAvatars');
126 // check upstream version?
127 Worker::add(Worker::PRIORITY_LOW, 'CheckVersion');
129 Worker::add(Worker::PRIORITY_LOW, 'CheckDeletedContacts');
131 if (DI::config()->get('system', 'optimize_tables')) {
132 Worker::add(Worker::PRIORITY_LOW, 'OptimizeTables');
135 // Resubscribe to relay servers
136 Relay::reSubscribe();
138 DI::config()->set('system', 'last_cron_daily', time());
141 Logger::notice('end');
143 DI::config()->set('system', 'last_cron', time());
147 * Kill sleeping database processes
151 private static function deleteSleepingProcesses()
153 Logger::info('Looking for sleeping processes');
155 $processes = DBA::p("SHOW FULL PROCESSLIST");
156 while ($process = DBA::fetch($processes)) {
157 if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != DBA::databaseName())) {
161 DBA::e("KILL ?", $process['Id']);
162 Logger::notice('Killed sleeping process', ['id' => $process['Id']]);
164 DBA::close($processes);