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\Addon;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Worker;
29 use Friendica\Database\DBA;
31 use Friendica\Model\Contact;
32 use Friendica\Util\DateTimeFormat;
36 public static function execute()
40 $last = DI::config()->get('system', 'last_cron');
42 $poll_interval = intval(DI::config()->get('system', 'cron_interval'));
45 $next = $last + ($poll_interval * 60);
47 Logger::log('cron intervall not reached');
52 Logger::log('cron: start');
54 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
55 Hook::fork($a->queue['priority'], "cron");
57 // run the process to update server directories in the background
58 Worker::add(PRIORITY_LOW, 'UpdateServerDirectories');
60 // Expire and remove user entries
61 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
63 // Call possible post update functions
64 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
66 // Clear cache entries
67 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
69 // Repair entries in the database
70 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
72 // once daily run birthday_updates and then expire in background
73 $d1 = DI::config()->get('system', 'last_expire_day');
74 $d2 = intval(DateTimeFormat::utcNow('d'));
77 if ($d2 != intval($d1)) {
79 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
81 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
83 // update nodeinfo data
84 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
86 Worker::add(PRIORITY_LOW, 'UpdateGServers');
88 Worker::add(PRIORITY_LOW, 'Expire');
90 Worker::add(PRIORITY_MEDIUM, 'DBClean');
92 // check upstream version?
93 Worker::add(PRIORITY_LOW, 'CheckVersion');
95 self::checkdeletedContacts();
97 DI::config()->set('system', 'last_expire_day', $d2);
101 if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) {
103 // Search for new contacts in the directory
104 if (DI::config()->get('system', 'synchronize_directory')) {
105 Worker::add(PRIORITY_LOW, 'PullDirectory');
108 // Delete all done workerqueue entries
109 DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
111 // Optimizing this table only last seconds
112 if (DI::config()->get('system', 'optimize_tables')) {
113 DBA::e("OPTIMIZE TABLE `workerqueue`");
116 DI::config()->set('system', 'last_cron_hourly', time());
119 // Ensure to have a .htaccess file.
120 // this is a precaution for systems that update automatically
121 $basepath = $a->getBasePath();
122 if (!file_exists($basepath . '/.htaccess') && is_writable($basepath)) {
123 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
127 self::pollContacts();
129 // Update contact information
130 self::updatePublicContacts();
132 Logger::log('cron: end');
134 DI::config()->set('system', 'last_cron', time());
140 * Checks for contacts that are about to be deleted and ensures that they are removed.
141 * This should be done automatically in the "remove" function. This here is a cleanup job.
143 private static function checkdeletedContacts()
145 $contacts = DBA::select('contact', ['id'], ['deleted' => true]);
146 while ($contact = DBA::fetch($contacts)) {
147 Worker::add(PRIORITY_MEDIUM, 'RemoveContact', $contact['id']);
149 DBA::close($contacts);
153 * Update public contacts
155 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
157 private static function updatePublicContacts() {
159 $last_updated = DateTimeFormat::utc('now - 1 week');
160 $condition = ["`network` IN (?, ?, ?, ?) AND `uid` = ? AND NOT `self` AND `last-update` < ?",
161 Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, 0, $last_updated];
165 $contacts = DBA::select('contact', ['id', 'last-update'], $condition, ['limit' => 100, 'order' => ['last-update']]);
166 while ($contact = DBA::fetch($contacts)) {
167 if (empty($oldest_id)) {
168 $oldest_id = $contact['id'];
169 $oldest_date = $contact['last-update'];
171 Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id'], 'force');
174 Logger::info('Initiated update for public contacts', ['interval' => $count, 'id' => $oldest_id, 'oldest' => $oldest_date]);
175 DBA::close($contacts);
179 * Poll contacts for unreceived messages
181 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
183 private static function pollContacts() {
184 $min_poll_interval = DI::config()->get('system', 'min_poll_interval', 1);
188 $sql = "SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
189 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
191 STRAIGHT_JOIN `contact`
192 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
193 AND `contact`.`network` IN (?, ?, ?, ?, ?)
194 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
195 AND `contact`.`rel` != ?
196 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed`";
198 $parameters = [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Contact::FOLLOWER];
200 // Only poll from those with suitable relationships,
201 // and which have a polling address and ignore Diaspora since
202 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
203 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
204 if ($abandon_days < 1) {
208 if (!empty($abandon_days)) {
209 $sql .= " AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL ? DAY";
210 $parameters[] = $abandon_days;
213 $contacts = DBA::p($sql, $parameters);
215 if (!DBA::isResult($contacts)) {
219 while ($contact = DBA::fetch($contacts)) {
220 // Friendica and OStatus are checked once a day
221 if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
222 $contact['priority'] = 3;
225 // ActivityPub is checked once a week
226 if ($contact['network'] == Protocol::ACTIVITYPUB) {
227 $contact['priority'] = 4;
230 // Check archived contacts once a month
231 if ($contact['archive']) {
232 $contact['priority'] = 5;
235 if ($contact['priority'] >= 0) {
238 $t = $contact['last-update'];
241 * Based on $contact['priority'], should we poll this site now? Or later?
243 switch ($contact['priority']) {
245 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
250 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
255 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
260 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
265 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
271 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + " . $min_poll_interval . " minute")) {
281 if ((($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) || ($contact['network'] == Protocol::MAIL)) {
282 $priority = PRIORITY_MEDIUM;
283 } elseif ($contact['archive']) {
284 $priority = PRIORITY_NEGLIGIBLE;
286 $priority = PRIORITY_LOW;
289 Logger::log("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
291 Worker::add(['priority' => $priority, 'dont_fork' => true, 'force_priority' => true], 'OnePoll', (int)$contact['id']);
293 DBA::close($contacts);