]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
Merge pull request #11805 from annando/no-standard-decoupling
[friendica.git] / src / Worker / Cron.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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
33 class Cron
34 {
35         public static function execute()
36         {
37                 $a = DI::app();
38
39                 $last = DI::config()->get('system', 'last_cron');
40
41                 $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
42
43                 if ($last) {
44                         $next = $last + ($poll_interval * 60);
45                         if ($next > time()) {
46                                 Logger::notice('cron intervall not reached');
47                                 return;
48                         }
49                 }
50
51                 Logger::notice('start');
52
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');
58                 }
59
60                 if (DI::config()->get('system', 'delete_sleeping_processes')) {
61                         self::deleteSleepingProcesses();
62                 }
63
64                 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
65                 Hook::fork(PRIORITY_MEDIUM, 'cron');
66
67                 // Poll contacts
68                 Worker::add(PRIORITY_MEDIUM, 'PollContacts');
69
70                 // Update contact information
71                 Worker::add(PRIORITY_LOW, 'UpdateContacts');
72
73                 // Update server information
74                 Worker::add(PRIORITY_LOW, 'UpdateGServers');
75
76                 // run the process to update server directories in the background
77                 Worker::add(PRIORITY_LOW, 'UpdateServerDirectories');
78
79                 // Expire and remove user entries
80                 Worker::add(PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
81
82                 // Call possible post update functions
83                 Worker::add(PRIORITY_LOW, 'PostUpdate');
84
85                 // Hourly cron calls
86                 if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) {
87
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                         if (DI::config()->get('system', 'decoupled_receiver')) {
97                                 Queue::processAll();
98                         }
99
100                         // Search for new contacts in the directory
101                         if (DI::config()->get('system', 'synchronize_directory')) {
102                                 Worker::add(PRIORITY_LOW, 'PullDirectory');
103                         }
104
105                         // Clear cache entries
106                         Worker::add(PRIORITY_LOW, 'ClearCache');
107
108                         DI::config()->set('system', 'last_cron_hourly', time());
109                 }
110
111                 // Daily maintenance cron calls
112                 if (Worker::isInMaintenanceWindow(true)) {
113
114                         Worker::add(PRIORITY_LOW, 'UpdateContactBirthdays');
115
116                         Worker::add(PRIORITY_LOW, 'UpdatePhotoAlbums');
117
118                         Worker::add(PRIORITY_LOW, 'ExpirePosts');
119
120                         Worker::add(PRIORITY_LOW, 'ExpireActivities');
121
122                         Worker::add(PRIORITY_LOW, 'RemoveUnusedTags');
123
124                         Worker::add(PRIORITY_LOW, 'RemoveUnusedContacts');
125
126                         Worker::add(PRIORITY_LOW, 'RemoveUnusedAvatars');
127
128                         // check upstream version?
129                         Worker::add(PRIORITY_LOW, 'CheckVersion');
130
131                         Worker::add(PRIORITY_LOW, 'CheckDeletedContacts');
132
133                         if (DI::config()->get('system', 'optimize_tables')) {
134                                 Worker::add(PRIORITY_LOW, 'OptimizeTables');
135                         }
136
137                         // Resubscribe to relay servers
138                         Relay::reSubscribe();
139
140                         DI::config()->set('system', 'last_cron_daily', time());
141                 }
142
143                 Logger::notice('end');
144
145                 DI::config()->set('system', 'last_cron', time());
146         }
147
148         /**
149          * Kill sleeping database processes
150          *
151          * @return void
152          */
153         private static function deleteSleepingProcesses()
154         {
155                 Logger::info('Looking for sleeping processes');
156                 
157                 $processes = DBA::p("SHOW FULL PROCESSLIST");
158                 while ($process = DBA::fetch($processes)) {
159                         if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != DBA::databaseName())) {
160                                 continue;
161                         }
162
163                         DBA::e("KILL ?", $process['Id']);
164                         Logger::notice('Killed sleeping process', ['id' => $process['Id']]);
165                 }
166                 DBA::close($processes);
167         }
168 }