3 * @file src/Worker/Cron.php
5 namespace Friendica\Worker;
7 use Friendica\BaseObject;
8 use Friendica\Core\Addon;
9 use Friendica\Core\Config;
10 use Friendica\Core\Hook;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Util\DateTimeFormat;
20 public static function execute($parameter = '', $generation = 0)
22 $a = BaseObject::getApp();
24 // Poll contacts with specific parameters
25 if (!empty($parameter)) {
26 self::pollContacts($parameter, $generation);
30 $last = Config::get('system', 'last_cron');
32 $poll_interval = intval(Config::get('system', 'cron_interval'));
35 $next = $last + ($poll_interval * 60);
37 Logger::log('cron intervall not reached');
42 Logger::log('cron: start');
44 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
45 Hook::fork($a->queue['priority'], "cron");
47 // run the process to discover global contacts in the background
48 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
50 // run the process to update locally stored global contacts in the background
51 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
53 // Expire and remove user entries
54 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
56 // Call possible post update functions
57 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
59 // Clear cache entries
60 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
62 // Repair missing Diaspora values in contacts
63 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
65 // Repair entries in the database
66 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
68 // once daily run birthday_updates and then expire in background
69 $d1 = Config::get('system', 'last_expire_day');
70 $d2 = intval(DateTimeFormat::utcNow('d'));
73 if ($d2 != intval($d1)) {
75 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
77 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
79 // update nodeinfo data
80 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
82 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
84 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
86 Worker::add(PRIORITY_LOW, 'Expire');
88 Worker::add(PRIORITY_MEDIUM, 'DBClean');
90 // check upstream version?
91 Worker::add(PRIORITY_LOW, 'CheckVersion');
93 Config::set('system', 'last_expire_day', $d2);
97 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
99 // Delete all done workerqueue entries
100 DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
102 // Optimizing this table only last seconds
103 if (Config::get('system', 'optimize_workerqueue', false)) {
104 DBA::e("OPTIMIZE TABLE `workerqueue`");
107 Config::set('system', 'last_cron_hourly', time());
110 // Ensure to have a .htaccess file.
111 // this is a precaution for systems that update automatically
112 $basepath = $a->getBasePath();
113 if (!file_exists($basepath . '/.htaccess')) {
114 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
118 self::pollContacts($parameter, $generation);
120 Logger::log('cron: end');
122 Config::set('system', 'last_cron', time());
128 * @brief Poll contacts for unreceived messages
130 * @todo Currently it seems as if the following parameter aren't used at all ...
132 * @param string $parameter Parameter (force, restart, ...) for the contact polling
133 * @param integer $generation
134 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
136 private static function pollContacts($parameter, $generation) {
141 if ($parameter == 'force') {
144 if ($parameter == 'restart') {
145 $generation = intval($generation);
151 if (intval($parameter)) {
152 $manual_id = intval($parameter);
156 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
158 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
162 // Only poll from those with suitable relationships,
163 // and which have a polling address and ignore Diaspora since
164 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
166 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
167 if ($abandon_days < 1) {
170 $abandon_sql = (($abandon_days)
171 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
175 $contacts = q("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 ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
181 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
182 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
183 DBA::escape(Protocol::ACTIVITYPUB),
184 DBA::escape(Protocol::DFRN),
185 DBA::escape(Protocol::OSTATUS),
186 DBA::escape(Protocol::DIASPORA),
187 DBA::escape(Protocol::FEED),
188 DBA::escape(Protocol::MAIL)
191 if (!DBA::isResult($contacts)) {
195 foreach ($contacts as $contact) {
198 $contact['last-update'] = DBA::NULL_DATETIME;
201 // Friendica and OStatus are checked once a day
202 if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
203 $contact['priority'] = 2;
206 if ($contact['subhub'] && in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
208 * We should be getting everything via a hub. But just to be sure, let's check once a day.
209 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
210 * This also lets us update our subscription to the hub, and add or replace hubs in case it
211 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
213 $poll_interval = Config::get('system', 'pushpoll_frequency');
214 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
217 // Check ActivityPub and Diaspora contacts or followers once a week
218 if (in_array($contact["network"], [Protocol::ACTIVITYPUB, Protocol::DIASPORA]) || ($contact["rel"] == Contact::FOLLOWER)) {
219 $contact['priority'] = 4;
222 // Check archived contacts once a month
223 if ($contact['archive']) {
224 $contact['priority'] = 5;
227 if (($contact['priority'] >= 0) && !$force) {
230 $t = $contact['last-update'];
233 * Based on $contact['priority'], should we poll this site now? Or later?
235 switch ($contact['priority']) {
237 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
242 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
247 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
252 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
257 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
263 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
273 if (($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) {
274 $priority = PRIORITY_MEDIUM;
275 } elseif ($contact['archive']) {
276 $priority = PRIORITY_NEGLIGIBLE;
278 $priority = PRIORITY_LOW;
281 Logger::log("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
283 Worker::add(['priority' => $priority, 'dont_fork' => true, 'force_priority' => true], 'OnePoll', (int)$contact['id']);