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\Temporal;
14 require_once 'include/dba.php';
17 public static function execute($parameter = '', $generation = 0) {
20 require_once 'include/datetime.php';
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 // run queue delivery process in the background
46 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
48 // run the process to discover global contacts in the background
49 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
51 // run the process to update locally stored global contacts in the background
52 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
54 // Expire and remove user entries
55 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
57 // Call possible post update functions
58 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
60 // update nodeinfo data
61 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
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(Temporal::utcNow('d'));
76 if ($d2 != intval($d1)) {
78 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
80 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
82 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
84 Config::set('system', 'last_expire_day', $d2);
86 Worker::add(PRIORITY_LOW, 'Expire');
88 Worker::add(PRIORITY_MEDIUM, 'DBClean');
90 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
92 // Delete all done workerqueue entries
93 dba::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR']);
95 // check upstream version?
96 Worker::add(PRIORITY_LOW, 'CheckVersion');
100 self::pollContacts($parameter, $generation);
104 Config::set('system', 'last_cron', time());
110 * @brief Poll contacts for unreceived messages
112 * @todo Currently it seems as if the following parameter aren't used at all ...
114 * @param string $parameter Parameter (force, restart, ...) for the contact polling
115 * @param integer $generation
117 private static function pollContacts($parameter, $generation) {
123 if ($parameter == 'force') {
126 if ($parameter == 'restart') {
128 $generation = intval($generation);
134 if (intval($parameter)) {
135 $manual_id = intval($parameter);
139 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
141 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
145 $d = Temporal::utcNow();
147 // Only poll from those with suitable relationships,
148 // and which have a polling address and ignore Diaspora since
149 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
151 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
152 if ($abandon_days < 1) {
155 $abandon_sql = (($abandon_days)
156 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
160 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
161 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
163 STRAIGHT_JOIN `contact`
164 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
165 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
166 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
167 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
169 dbesc(NETWORK_OSTATUS),
170 dbesc(NETWORK_DIASPORA),
175 if (!DBM::is_result($contacts)) {
179 foreach ($contacts as $contact) {
182 $contact['last-update'] = NULL_DATE;
185 // Friendica and OStatus are checked once a day
186 if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
187 $contact['priority'] = 2;
190 if ($contact['subhub'] && in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
192 * We should be getting everything via a hub. But just to be sure, let's check once a day.
193 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
194 * This also lets us update our subscription to the hub, and add or replace hubs in case it
195 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
197 $poll_interval = Config::get('system', 'pushpoll_frequency');
198 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
201 // Check Diaspora contacts or followers once a week
202 if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
203 $contact['priority'] = 4;
206 // Check archived contacts once a month
207 if ($contact['archive']) {
208 $contact['priority'] = 5;
211 if (($contact['priority'] >= 0) && !$force) {
214 $t = $contact['last-update'];
217 * Based on $contact['priority'], should we poll this site now? Or later?
219 switch ($contact['priority']) {
221 if (Temporal::utcNow() > Temporal::convert($t . " + 1 month")) {
226 if (Temporal::utcNow() > Temporal::convert($t . " + 1 week")) {
231 if (Temporal::utcNow() > Temporal::convert($t . " + 1 day")) {
236 if (Temporal::utcNow() > Temporal::convert($t . " + 12 hour")) {
241 if (Temporal::utcNow() > Temporal::convert($t . " + 1 hour")) {
247 if (Temporal::utcNow() > Temporal::convert($t . " + ".$min_poll_interval." minute")) {
257 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
258 $priority = PRIORITY_MEDIUM;
259 } elseif ($contact['archive']) {
260 $priority = PRIORITY_NEGLIGIBLE;
262 $priority = PRIORITY_LOW;
265 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
267 Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);