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