]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
spelling: interval
[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\Hook;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Tag;
30 use Friendica\Protocol\ActivityPub\Queue;
31 use Friendica\Protocol\Relay;
32 use Friendica\Util\DateTimeFormat;
33
34 class Cron
35 {
36         public static function execute()
37         {
38                 $a = DI::app();
39
40                 $last = DI::keyValue()->get('last_cron');
41
42                 $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
43
44                 if ($last) {
45                         $next = $last + ($poll_interval * 60);
46                         if ($next > time()) {
47                                 Logger::notice('cron interval not reached');
48                                 return;
49                         }
50                 }
51
52                 Logger::notice('start');
53
54                 // Ensure to have a .htaccess file.
55                 // this is a precaution for systems that update automatically
56                 $basepath = $a->getBasePath();
57                 if (!file_exists($basepath . '/.htaccess') && is_writable($basepath)) {
58                         copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
59                 }
60
61                 if (DI::config()->get('system', 'delete_sleeping_processes')) {
62                         self::deleteSleepingProcesses();
63                 }
64
65                 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
66                 Hook::fork(Worker::PRIORITY_MEDIUM, 'cron');
67
68                 // Poll contacts
69                 Worker::add(Worker::PRIORITY_MEDIUM, 'PollContacts');
70
71                 // Update contact information
72                 Worker::add(Worker::PRIORITY_LOW, 'UpdateContacts');
73
74                 // Update server information
75                 Worker::add(Worker::PRIORITY_LOW, 'UpdateGServers');
76
77                 // run the process to update server directories in the background
78                 Worker::add(Worker::PRIORITY_LOW, 'UpdateServerDirectories');
79
80                 // Expire and remove user entries
81                 Worker::add(Worker::PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
82
83                 // Call possible post update functions
84                 Worker::add(Worker::PRIORITY_LOW, 'PostUpdate');
85
86                 // Hourly cron calls
87                 if ((DI::keyValue()->get('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);
91
92                         // Remove old pending posts from the queue
93                         Queue::clear();
94
95                         // Process all unprocessed entries
96                         Queue::processAll();
97
98                         // Search for new contacts in the directory
99                         if (DI::config()->get('system', 'synchronize_directory')) {
100                                 Worker::add(Worker::PRIORITY_LOW, 'PullDirectory');
101                         }
102
103                         // Clear cache entries
104                         Worker::add(Worker::PRIORITY_LOW, 'ClearCache');
105
106                         DI::keyValue()->set('last_cron_hourly', time());
107                 }
108
109                 // Daily maintenance cron calls
110                 if (Worker::isInMaintenanceWindow(true)) {
111
112                         Worker::add(Worker::PRIORITY_LOW, 'UpdateContactBirthdays');
113
114                         Worker::add(Worker::PRIORITY_LOW, 'UpdatePhotoAlbums');
115
116                         Worker::add(Worker::PRIORITY_LOW, 'ExpirePosts');
117
118                         Worker::add(Worker::PRIORITY_LOW, 'ExpireActivities');
119
120                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedTags');
121
122                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedContacts');
123
124                         Worker::add(Worker::PRIORITY_LOW, 'RemoveUnusedAvatars');
125
126                         // check upstream version?
127                         Worker::add(Worker::PRIORITY_LOW, 'CheckVersion');
128
129                         Worker::add(Worker::PRIORITY_LOW, 'CheckDeletedContacts');
130
131                         Worker::add(Worker::PRIORITY_LOW, 'UpdateAllSuggestions');
132
133                         if (DI::config()->get('system', 'optimize_tables')) {
134                                 Worker::add(Worker::PRIORITY_LOW, 'OptimizeTables');
135                         }
136
137                         $users = DBA::select('owner-view', ['uid'], ["`homepage_verified` OR (`last-activity` > ? AND `homepage` != ?)", DateTimeFormat::utc('now - 7 days', 'Y-m-d'), '']);
138                         while ($user = DBA::fetch($users)) {
139                                 Worker::add(Worker::PRIORITY_LOW, 'CheckRelMeProfileLink', $user['uid']);
140                         }
141                         DBA::close($users);
142
143                         // Resubscribe to relay servers
144                         Relay::reSubscribe();
145
146                         // Update "blocked" status of servers
147                         Worker::add(Worker::PRIORITY_LOW, 'UpdateBlockedServers');
148
149                         DI::keyValue()->set('last_cron_daily', time());
150                 }
151
152                 Logger::notice('end');
153
154                 DI::keyValue()->set('last_cron', time());
155         }
156
157         /**
158          * Kill sleeping database processes
159          *
160          * @return void
161          */
162         private static function deleteSleepingProcesses()
163         {
164                 Logger::info('Looking for sleeping processes');
165
166                 $processes = DBA::p("SHOW FULL PROCESSLIST");
167                 while ($process = DBA::fetch($processes)) {
168                         if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != DBA::databaseName())) {
169                                 continue;
170                         }
171
172                         DBA::e("KILL ?", $process['Id']);
173                         Logger::notice('Killed sleeping process', ['id' => $process['Id']]);
174                 }
175                 DBA::close($processes);
176         }
177 }