3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Worker;
24 use Friendica\Core\Hook;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
29 use Friendica\Util\DateTimeFormat;
33 public static function execute()
37 $last = DI::config()->get('system', 'last_cron');
39 $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
42 $next = $last + ($poll_interval * 60);
44 Logger::notice('cron intervall not reached');
49 Logger::notice('cron: start');
51 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
52 Hook::fork($a->queue['priority'], "cron");
54 // run the process to update server directories in the background
55 Worker::add(PRIORITY_LOW, 'UpdateServerDirectories');
57 // Expire and remove user entries
58 Worker::add(PRIORITY_MEDIUM, 'ExpireAndRemoveUsers');
60 // Call possible post update functions
61 Worker::add(PRIORITY_LOW, 'PostUpdate');
63 // Repair entries in the database
64 Worker::add(PRIORITY_LOW, 'RepairDatabase');
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'));
71 if ($d2 != intval($d1)) {
73 Worker::add(PRIORITY_LOW, 'UpdateContactBirthdays');
75 Worker::add(PRIORITY_LOW, 'UpdatePhotoAlbums');
77 // update nodeinfo data
78 Worker::add(PRIORITY_LOW, 'NodeInfo');
80 Worker::add(PRIORITY_LOW, 'UpdateGServers');
82 Worker::add(PRIORITY_LOW, 'Expire');
84 Worker::add(PRIORITY_MEDIUM, 'DBClean');
86 Worker::add(PRIORITY_LOW, 'ExpireConversations');
88 Worker::add(PRIORITY_LOW, 'CleanItemUri');
90 // check upstream version?
91 Worker::add(PRIORITY_LOW, 'CheckVersion');
93 Worker::add(PRIORITY_LOW, 'CheckdeletedContacts');
95 if (DI::config()->get('system', 'optimize_tables')) {
96 Worker::add(PRIORITY_LOW, 'OptimizeTables');
99 DI::config()->set('system', 'last_expire_day', $d2);
103 if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) {
105 // Search for new contacts in the directory
106 if (DI::config()->get('system', 'synchronize_directory')) {
107 Worker::add(PRIORITY_LOW, 'PullDirectory');
110 // Delete all done workerqueue entries
111 DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
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);
122 DI::lock()->release(Worker::LOCK_PROCESS);
126 // Clear cache entries
127 Worker::add(PRIORITY_LOW, 'ClearCache');
129 DI::config()->set('system', 'last_cron_hourly', time());
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');
140 Worker::add(PRIORITY_HIGH, 'PollContacts');
142 // Update contact information
143 Worker::add(PRIORITY_LOW, 'UpdatePublicContacts');
145 Logger::notice('cron: end');
147 DI::config()->set('system', 'last_cron', time());