]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
Improved Emoji detection
[friendica.git] / src / Worker / Cron.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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                 Worker::add(Worker::PRIORITY_LOW, 'UpdateServerDirectories');
80
81                 // Expire and remove user entries
82                 Worker::add(Worker::PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
83
84                 // Call possible post update functions
85                 Worker::add(Worker::PRIORITY_LOW, 'PostUpdate');
86
87                 // Hourly cron calls
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);
92
93                         // Remove old pending posts from the queue
94                         Queue::clear();
95
96                         // Process all unprocessed entries
97                         Queue::processAll();
98
99                         // Search for new contacts in the directory
100                         if (DI::config()->get('system', 'synchronize_directory')) {
101                                 Worker::add(Worker::PRIORITY_LOW, 'PullDirectory');
102                         }
103
104                         // Clear cache entries
105                         Worker::add(Worker::PRIORITY_LOW, 'ClearCache');
106
107                         // Update interaction scores
108                         Worker::add(Worker::PRIORITY_LOW, 'UpdateScores');
109
110                         DI::keyValue()->set('last_cron_hourly', time());
111                 }
112
113                 // Daily maintenance cron calls
114                 if (Worker::isInMaintenanceWindow(true)) {
115
116                         Worker::add(Worker::PRIORITY_LOW, 'UpdateContactBirthdays');
117
118                         Worker::add(Worker::PRIORITY_LOW, 'UpdatePhotoAlbums');
119
120                         Worker::add(Worker::PRIORITY_LOW, 'ExpirePosts');
121
122                         Worker::add(Worker::PRIORITY_LOW, 'ExpireActivities');
123
124                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedTags');
125
126                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedContacts');
127
128                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedAvatars');
129
130                         // check upstream version?
131                         Worker::add(Worker::PRIORITY_LOW, 'CheckVersion');
132
133                         Worker::add(Worker::PRIORITY_LOW, 'CheckDeletedContacts');
134
135                         Worker::add(Worker::PRIORITY_LOW, 'UpdateAllSuggestions');
136
137                         if (DI::config()->get('system', 'optimize_tables')) {
138                                 Worker::add(Worker::PRIORITY_LOW, 'OptimizeTables');
139                         }
140
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']);
144                         }
145                         DBA::close($users);
146
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']);
151                         }
152                         DBA::close($users);
153
154                         // Resubscribe to relay servers
155                         Relay::reSubscribe();
156
157                         // Update "blocked" status of servers
158                         Worker::add(Worker::PRIORITY_LOW, 'UpdateBlockedServers');
159
160                         Addon::reload();
161
162                         DI::keyValue()->set('last_cron_daily', time());
163                 }
164
165                 Logger::notice('end');
166
167                 DI::keyValue()->set('last_cron', time());
168         }
169
170         /**
171          * Kill sleeping database processes
172          *
173          * @return void
174          */
175         private static function deleteSleepingProcesses()
176         {
177                 Logger::info('Looking for sleeping processes');
178
179                 DBA::deleteSleepingProcesses();
180         }
181 }