]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
Superfluous todo removed
[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\Relay;
31
32 class Cron
33 {
34         public static function execute()
35         {
36                 $a = DI::app();
37
38                 $last = DI::config()->get('system', 'last_cron');
39
40                 $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
41
42                 if ($last) {
43                         $next = $last + ($poll_interval * 60);
44                         if ($next > time()) {
45                                 Logger::notice('cron intervall not reached');
46                                 return;
47                         }
48                 }
49
50                 Logger::notice('start');
51
52                 // Ensure to have a .htaccess file.
53                 // this is a precaution for systems that update automatically
54                 $basepath = $a->getBasePath();
55                 if (!file_exists($basepath . '/.htaccess') && is_writable($basepath)) {
56                         copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
57                 }
58
59                 if (DI::config()->get('system', 'delete_sleeping_processes')) {
60                         self::deleteSleepingProcesses();
61                 }
62
63                 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
64                 Hook::fork(PRIORITY_MEDIUM, 'cron');
65
66                 // Poll contacts
67                 Worker::add(PRIORITY_MEDIUM, 'PollContacts');
68
69                 // Update contact information
70                 Worker::add(PRIORITY_LOW, 'UpdateContacts');
71
72                 // Update server information
73                 Worker::add(PRIORITY_LOW, 'UpdateGServers');
74
75                 // run the process to update server directories in the background
76                 Worker::add(PRIORITY_LOW, 'UpdateServerDirectories');
77
78                 // Expire and remove user entries
79                 Worker::add(PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
80
81                 // Call possible post update functions
82                 Worker::add(PRIORITY_LOW, 'PostUpdate');
83
84                 // Hourly cron calls
85                 if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) {
86
87                         // Update trending tags cache for the community page
88                         Tag::setLocalTrendingHashtags(24, 20);
89                         Tag::setGlobalTrendingHashtags(24, 20);
90
91                         // Search for new contacts in the directory
92                         if (DI::config()->get('system', 'synchronize_directory')) {
93                                 Worker::add(PRIORITY_LOW, 'PullDirectory');
94                         }
95
96                         // Clear cache entries
97                         Worker::add(PRIORITY_LOW, 'ClearCache');
98
99                         DI::config()->set('system', 'last_cron_hourly', time());
100                 }
101
102                 // Daily maintenance cron calls
103                 if (Worker::isInMaintenanceWindow(true)) {
104
105                         Worker::add(PRIORITY_LOW, 'UpdateContactBirthdays');
106
107                         Worker::add(PRIORITY_LOW, 'UpdatePhotoAlbums');
108
109                         Worker::add(PRIORITY_LOW, 'ExpirePosts');
110
111                         Worker::add(PRIORITY_LOW, 'ExpireConversations');
112
113                         Worker::add(PRIORITY_LOW, 'RemoveUnusedTags');
114
115                         Worker::add(PRIORITY_LOW, 'RemoveUnusedContacts');
116
117                         Worker::add(PRIORITY_LOW, 'RemoveUnusedAvatars');
118
119                         // check upstream version?
120                         Worker::add(PRIORITY_LOW, 'CheckVersion');
121
122                         Worker::add(PRIORITY_LOW, 'CheckDeletedContacts');
123
124                         if (DI::config()->get('system', 'optimize_tables')) {
125                                 Worker::add(PRIORITY_LOW, 'OptimizeTables');
126                         }
127         
128                         DI::config()->set('system', 'last_cron_daily', time());
129
130                         // Resubscribe to relay servers
131                         Relay::reSubscribe();
132                 }
133
134                 Logger::notice('end');
135
136                 DI::config()->set('system', 'last_cron', time());
137         }
138
139         /**
140          * Kill sleeping database processes
141          *
142          * @return void
143          */
144         private static function deleteSleepingProcesses()
145         {
146                 Logger::info('Looking for sleeping processes');
147                 
148                 $processes = DBA::p("SHOW FULL PROCESSLIST");
149                 while ($process = DBA::fetch($processes)) {
150                         if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != DBA::databaseName())) {
151                                 continue;
152                         }
153
154                         DBA::e("KILL ?", $process['Id']);
155                         Logger::notice('Killed sleeping process', ['id' => $process['Id']]);
156                 }
157                 DBA::close($processes);
158         }
159 }