]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
Split cron tasks in several worker tasks
[friendica.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\Util\DateTimeFormat;
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::log('cron intervall not reached');
45                                 return;
46                         }
47                 }
48
49                 Logger::log('cron: start');
50
51                 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
52                 Hook::fork($a->queue['priority'], "cron");
53
54                 // run the process to update server directories in the background
55                 Worker::add(PRIORITY_LOW, 'UpdateServerDirectories');
56
57                 // Expire and remove user entries
58                 Worker::add(PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
59
60                 // Call possible post update functions
61                 Worker::add(PRIORITY_LOW, 'PostUpdate');
62
63                 // Repair entries in the database
64                 Worker::add(PRIORITY_LOW, 'RepairDatabase');
65
66                 // once daily run birthday_updates and then expire in background
67                 $d1 = DI::config()->get('system', 'last_expire_day');
68                 $d2 = intval(DateTimeFormat::utcNow('d'));
69
70                 // Daily cron calls
71                 if ($d2 != intval($d1)) {
72
73                         Worker::add(PRIORITY_LOW, 'UpdateContactBirthdays');
74
75                         Worker::add(PRIORITY_LOW, 'UpdatePhotoAlbums');
76
77                         // update nodeinfo data
78                         Worker::add(PRIORITY_LOW, 'NodeInfo');
79
80                         Worker::add(PRIORITY_LOW, 'UpdateGServers');
81
82                         Worker::add(PRIORITY_LOW, 'Expire');
83
84                         Worker::add(PRIORITY_MEDIUM, 'DBClean');
85
86                         Worker::add(PRIORITY_LOW, 'ExpireConversations');
87
88                         Worker::add(PRIORITY_LOW, 'CleanItemUri');
89
90                         // check upstream version?
91                         Worker::add(PRIORITY_LOW, 'CheckVersion');
92
93                         Worker::add(PRIORITY_LOW, 'CheckdeletedContacts');
94
95                         if (DI::config()->get('system', 'optimize_tables')) {
96                                 Worker::add(PRIORITY_LOW, 'OptimizeTables');
97                         }
98         
99                         DI::config()->set('system', 'last_expire_day', $d2);
100                 }
101
102                 // Hourly cron calls
103                 if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) {
104
105                         // Search for new contacts in the directory
106                         if (DI::config()->get('system', 'synchronize_directory')) {
107                                 Worker::add(PRIORITY_LOW, 'PullDirectory');
108                         }
109
110                         // Delete all done workerqueue entries
111                         DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
112
113                         // Optimizing this table only last seconds
114                         if (DI::config()->get('system', 'optimize_tables')) {
115                                 // We are acquiring the two locks from the worker to avoid locking problems
116                                 if (DI::lock()->acquire(Worker::LOCK_PROCESS, 10)) {
117                                         if (DI::lock()->acquire(Worker::LOCK_WORKER, 10)) {
118                                                 DBA::e("OPTIMIZE TABLE `workerqueue`");
119                                                 DBA::e("OPTIMIZE TABLE `process`");                     
120                                                 DI::lock()->release(Worker::LOCK_WORKER);
121                                         }
122                                         DI::lock()->release(Worker::LOCK_PROCESS);
123                                 }
124                         }
125
126                         // Clear cache entries
127                         Worker::add(PRIORITY_LOW, 'ClearCache');
128
129                         DI::config()->set('system', 'last_cron_hourly', time());
130                 }
131
132                 // Ensure to have a .htaccess file.
133                 // this is a precaution for systems that update automatically
134                 $basepath = $a->getBasePath();
135                 if (!file_exists($basepath . '/.htaccess') && is_writable($basepath)) {
136                         copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
137                 }
138
139                 // Poll contacts
140                 Worker::add(PRIORITY_HIGH, 'PollContacts');
141
142                 // Update contact information
143                 Worker::add(PRIORITY_LOW, 'UpdatePublicContacts');              
144
145                 Logger::log('cron: end');
146
147                 DI::config()->set('system', 'last_cron', time());
148
149                 return;
150         }
151 }