]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
Merge pull request #13785 from foss-/patch-11
[friendica.git] / src / Worker / Cron.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
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;
29 use Friendica\DI;
30 use Friendica\Model\Tag;
31 use Friendica\Protocol\ActivityPub\Queue;
32 use Friendica\Protocol\Relay;
33 use Friendica\Util\DateTimeFormat;
34
35 class Cron
36 {
37         public static function execute()
38         {
39                 $a = DI::app();
40
41                 $last = DI::keyValue()->get('last_cron');
42
43                 $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
44
45                 if ($last) {
46                         $next = $last + ($poll_interval * 60);
47                         if ($next > time()) {
48                                 Logger::notice('cron interval not reached');
49                                 return;
50                         }
51                 }
52
53                 Logger::notice('start');
54
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');
60                 }
61
62                 if (DI::config()->get('system', 'delete_sleeping_processes')) {
63                         self::deleteSleepingProcesses();
64                 }
65
66                 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
67                 Hook::fork(Worker::PRIORITY_MEDIUM, 'cron');
68
69                 // Poll contacts
70                 Worker::add(Worker::PRIORITY_MEDIUM, 'PollContacts');
71
72                 // Update contact information
73                 Worker::add(Worker::PRIORITY_LOW, 'UpdateContacts');
74
75                 // Update server information
76                 Worker::add(Worker::PRIORITY_LOW, 'UpdateGServers');
77
78                 // run the process to update server directories in the background
79                 if (DI::config()->get('system', 'poco_discovery')) {
80                         Worker::add(Worker::PRIORITY_LOW, 'UpdateServerDirectories');
81                 }
82
83                 // Expire and remove user entries
84                 Worker::add(Worker::PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
85
86                 // Call possible post update functions
87                 Worker::add(Worker::PRIORITY_LOW, 'PostUpdate');
88
89                 // Hourly cron calls
90                 if ((DI::keyValue()->get('last_cron_hourly') ?? 0) + 3600 < time()) {
91                         // Update trending tags cache for the community page
92                         Tag::setLocalTrendingHashtags(24, 20);
93                         Tag::setGlobalTrendingHashtags(24, 20);
94
95                         // Remove old pending posts from the queue
96                         Queue::clear();
97
98                         // Process all unprocessed entries
99                         Queue::processAll();
100
101                         // Search for new contacts in the directory
102                         if (DI::config()->get('system', 'synchronize_directory')) {
103                                 Worker::add(Worker::PRIORITY_LOW, 'PullDirectory');
104                         }
105
106                         // Clear cache entries
107                         Worker::add(Worker::PRIORITY_LOW, 'ClearCache');
108
109                         // Update interaction scores
110                         Worker::add(Worker::PRIORITY_LOW, 'UpdateScores');
111
112                         DI::keyValue()->set('last_cron_hourly', time());
113                 }
114
115                 // Daily maintenance cron calls
116                 if (Worker::isInMaintenanceWindow(true)) {
117
118                         Worker::add(Worker::PRIORITY_LOW, 'UpdateContactBirthdays');
119
120                         Worker::add(Worker::PRIORITY_LOW, 'UpdatePhotoAlbums');
121
122                         Worker::add(Worker::PRIORITY_LOW, 'ExpirePosts');
123
124                         Worker::add(Worker::PRIORITY_LOW, 'ExpireActivities');
125
126                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedTags');
127
128                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedContacts');
129
130                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedAvatars');
131
132                         // check upstream version?
133                         Worker::add(Worker::PRIORITY_LOW, 'CheckVersion');
134
135                         Worker::add(Worker::PRIORITY_LOW, 'CheckDeletedContacts');
136
137                         Worker::add(Worker::PRIORITY_LOW, 'UpdateAllSuggestions');
138
139                         if (DI::config()->get('system', 'optimize_tables')) {
140                                 Worker::add(Worker::PRIORITY_LOW, 'OptimizeTables');
141                         }
142
143                         $users = DBA::select('owner-view', ['uid'], ["`homepage_verified` OR (`last-activity` > ? AND `homepage` != ?)", DateTimeFormat::utc('now - 7 days', 'Y-m-d'), '']);
144                         while ($user = DBA::fetch($users)) {
145                                 Worker::add(Worker::PRIORITY_LOW, 'CheckRelMeProfileLink', $user['uid']);
146                         }
147                         DBA::close($users);
148
149                         // Update contact relations for our users
150                         $users = DBA::select('user', ['uid'], ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `uid` > ?", 0]);
151                         while ($user = DBA::fetch($users)) {
152                                 Worker::add(Worker::PRIORITY_LOW, 'ContactDiscoveryForUser', $user['uid']);
153                         }
154                         DBA::close($users);
155
156                         // Resubscribe to relay servers
157                         Relay::reSubscribe();
158
159                         // Update "blocked" status of servers
160                         Worker::add(Worker::PRIORITY_LOW, 'UpdateBlockedServers');
161
162                         Addon::reload();
163
164                         DI::keyValue()->set('last_cron_daily', time());
165                 }
166
167                 Logger::notice('end');
168
169                 DI::keyValue()->set('last_cron', time());
170         }
171
172         /**
173          * Kill sleeping database processes
174          *
175          * @return void
176          */
177         private static function deleteSleepingProcesses()
178         {
179                 Logger::info('Looking for sleeping processes');
180
181                 DBA::deleteSleepingProcesses();
182         }
183 }