3 * @file src/Worker/Cron.php
5 namespace Friendica\Worker;
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBM;
11 use Friendica\Util\DateTimeFormat;
14 require_once 'include/dba.php';
18 public static function execute($parameter = '', $generation = 0)
20 $a = \Friendica\BaseObject::getApp();
22 // Poll contacts with specific parameters
23 if (!empty($parameter)) {
24 self::pollContacts($parameter, $generation);
28 $last = Config::get('system', 'last_cron');
30 $poll_interval = intval(Config::get('system', 'cron_interval'));
31 if (! $poll_interval) {
36 $next = $last + ($poll_interval * 60);
38 logger('cron intervall not reached');
43 logger('cron: start');
45 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
46 Addon::forkHooks($a->queue['priority'], "cron");
48 // run queue delivery process in the background
49 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
51 // run the process to discover global contacts in the background
52 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
54 // run the process to update locally stored global contacts in the background
55 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
57 // Expire and remove user entries
58 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
60 // Call possible post update functions
61 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
63 // Clear cache entries
64 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
66 // Repair missing Diaspora values in contacts
67 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
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 = 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, "DiscoverPoCo", "update_server");
88 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
90 Worker::add(PRIORITY_LOW, 'Expire');
92 Worker::add(PRIORITY_MEDIUM, 'DBClean');
94 // check upstream version?
95 Worker::add(PRIORITY_LOW, 'CheckVersion');
97 Config::set('system', 'last_expire_day', $d2);
101 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
103 // Delete all done workerqueue entries
104 dba::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
106 // Optimizing this table only last seconds
107 if (Config::get('system', 'optimize_workerqueue', false)) {
108 dba::e("OPTIMIZE TABLE `workerqueue`");
111 Config::set('system', 'last_cron_hourly', time());
115 self::pollContacts($parameter, $generation);
119 Config::set('system', 'last_cron', time());
125 * @brief Poll contacts for unreceived messages
127 * @todo Currently it seems as if the following parameter aren't used at all ...
129 * @param string $parameter Parameter (force, restart, ...) for the contact polling
130 * @param integer $generation
132 private static function pollContacts($parameter, $generation) {
138 if ($parameter == 'force') {
141 if ($parameter == 'restart') {
143 $generation = intval($generation);
149 if (intval($parameter)) {
150 $manual_id = intval($parameter);
154 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
156 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
160 $d = DateTimeFormat::utcNow();
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') $sql_extra
181 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
182 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
184 dbesc(NETWORK_OSTATUS),
185 dbesc(NETWORK_DIASPORA),
190 if (!DBM::is_result($contacts)) {
194 foreach ($contacts as $contact) {
197 $contact['last-update'] = NULL_DATE;
200 // Friendica and OStatus are checked once a day
201 if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
202 $contact['priority'] = 2;
205 if ($contact['subhub'] && in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
207 * We should be getting everything via a hub. But just to be sure, let's check once a day.
208 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
209 * This also lets us update our subscription to the hub, and add or replace hubs in case it
210 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
212 $poll_interval = Config::get('system', 'pushpoll_frequency');
213 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
216 // Check Diaspora contacts or followers once a week
217 if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
218 $contact['priority'] = 4;
221 // Check archived contacts once a month
222 if ($contact['archive']) {
223 $contact['priority'] = 5;
226 if (($contact['priority'] >= 0) && !$force) {
229 $t = $contact['last-update'];
232 * Based on $contact['priority'], should we poll this site now? Or later?
234 switch ($contact['priority']) {
236 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
241 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
246 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
251 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
256 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
262 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
272 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
273 $priority = PRIORITY_MEDIUM;
274 } elseif ($contact['archive']) {
275 $priority = PRIORITY_NEGLIGIBLE;
277 $priority = PRIORITY_LOW;
280 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
282 Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);