3 * @file src/Worker/Cron.php
5 namespace Friendica\Worker;
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config;
9 use Friendica\Core\Hook;
10 use Friendica\Core\Logger;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Util\DateTimeFormat;
20 public static function execute()
24 $last = Config::get('system', 'last_cron');
26 $poll_interval = intval(Config::get('system', 'cron_interval'));
29 $next = $last + ($poll_interval * 60);
31 Logger::log('cron intervall not reached');
36 Logger::log('cron: start');
38 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
39 Hook::fork($a->queue['priority'], "cron");
41 // run the process to update server directories in the background
42 Worker::add(PRIORITY_LOW, 'UpdateServerDirectories');
44 // run the process to update locally stored global contacts in the background
45 Worker::add(PRIORITY_LOW, 'UpdateGContacts');
47 // Expire and remove user entries
48 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
50 // Call possible post update functions
51 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
53 // Clear cache entries
54 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
56 // Repair missing Diaspora values in contacts
57 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
59 // Repair entries in the database
60 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
62 // once daily run birthday_updates and then expire in background
63 $d1 = Config::get('system', 'last_expire_day');
64 $d2 = intval(DateTimeFormat::utcNow('d'));
67 if ($d2 != intval($d1)) {
69 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
71 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
73 // update nodeinfo data
74 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
76 Worker::add(PRIORITY_LOW, 'UpdateGServers');
78 Worker::add(PRIORITY_LOW, 'UpdateSuggestions');
80 Worker::add(PRIORITY_LOW, 'Expire');
82 Worker::add(PRIORITY_MEDIUM, 'DBClean');
84 // check upstream version?
85 Worker::add(PRIORITY_LOW, 'CheckVersion');
87 self::checkdeletedContacts();
89 Config::set('system', 'last_expire_day', $d2);
93 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
95 // Delete all done workerqueue entries
96 DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
98 // Optimizing this table only last seconds
99 if (Config::get('system', 'optimize_workerqueue', false)) {
100 DBA::e("OPTIMIZE TABLE `workerqueue`");
103 Config::set('system', 'last_cron_hourly', time());
106 // Ensure to have a .htaccess file.
107 // this is a precaution for systems that update automatically
108 $basepath = $a->getBasePath();
109 if (!file_exists($basepath . '/.htaccess') && is_writable($basepath)) {
110 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
114 self::pollContacts();
116 // Update contact information
117 self::updatePublicContacts();
119 Logger::log('cron: end');
121 Config::set('system', 'last_cron', time());
127 * Checks for contacts that are about to be deleted and ensures that they are removed.
128 * This should be done automatically in the "remove" function. This here is a cleanup job.
130 private static function checkdeletedContacts()
132 $contacts = DBA::select('contact', ['id'], ['deleted' => true]);
133 while ($contact = DBA::fetch($contacts)) {
134 Worker::add(PRIORITY_MEDIUM, 'RemoveContact', $contact['id']);
136 DBA::close($contacts);
140 * Update public contacts
141 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
143 private static function updatePublicContacts() {
145 $last_updated = DateTimeFormat::utc('now - 1 week');
146 $condition = ["`network` IN (?, ?, ?, ?) AND `uid` = ? AND NOT `self` AND `last-update` < ?",
147 Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, 0, $last_updated];
149 $total = DBA::count('contact', $condition);
152 $contacts = DBA::select('contact', ['id', 'last-update'], $condition, ['limit' => 100, 'order' => ['last-update']]);
153 while ($contact = DBA::fetch($contacts)) {
154 if (empty($oldest_id)) {
155 $oldest_id = $contact['id'];
156 $oldest_date = $contact['last-update'];
158 Worker::add(PRIORITY_LOW, "UpdateContact", $contact['id'], 'force');
161 Logger::info('Initiated update for public contacts', ['interval' => $count, 'total' => $total, 'id' => $oldest_id, 'oldest' => $oldest_date]);
162 DBA::close($contacts);
166 * Poll contacts for unreceived messages
168 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
170 private static function pollContacts() {
171 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
175 $sql = "SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
176 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
178 STRAIGHT_JOIN `contact`
179 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
180 AND `contact`.`network` IN (?, ?, ?, ?, ?)
181 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
182 AND `contact`.`rel` != ?
183 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed`";
185 $parameters = [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Contact::FOLLOWER];
187 // Only poll from those with suitable relationships,
188 // and which have a polling address and ignore Diaspora since
189 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
190 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
191 if ($abandon_days < 1) {
195 if (!empty($abandon_days)) {
196 $sql .= " AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL ? DAY";
197 $parameters[] = $abandon_days;
200 $contacts = DBA::p($sql, $parameters);
202 if (!DBA::isResult($contacts)) {
206 while ($contact = DBA::fetch($contacts)) {
207 // Friendica and OStatus are checked once a day
208 if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
209 $contact['priority'] = 3;
212 // ActivityPub is checked once a week
213 if ($contact['network'] == Protocol::ACTIVITYPUB) {
214 $contact['priority'] = 4;
217 // Check archived contacts once a month
218 if ($contact['archive']) {
219 $contact['priority'] = 5;
222 if ($contact['priority'] >= 0) {
225 $t = $contact['last-update'];
228 * Based on $contact['priority'], should we poll this site now? Or later?
230 switch ($contact['priority']) {
232 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
237 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
242 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
247 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
252 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
258 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + " . $min_poll_interval . " minute")) {
268 if (($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) {
269 $priority = PRIORITY_MEDIUM;
270 } elseif ($contact['archive']) {
271 $priority = PRIORITY_NEGLIGIBLE;
273 $priority = PRIORITY_LOW;
276 Logger::log("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
278 Worker::add(['priority' => $priority, 'dont_fork' => true, 'force_priority' => true], 'OnePoll', (int)$contact['id']);
280 DBA::close($contacts);