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\Worker;
11 use Friendica\Database\DBA;
12 use Friendica\Util\DateTimeFormat;
14 require_once 'include/dba.php';
18 public static function execute($parameter = '', $generation = 0)
20 $a = 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());
114 // Ensure to have a .htaccess file.
115 // this is a precaution for systems that update automatically
116 $basepath = $a->get_basepath();
117 if (!file_exists($basepath . '/.htaccess')) {
118 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
122 self::pollContacts($parameter, $generation);
126 Config::set('system', 'last_cron', time());
132 * @brief Poll contacts for unreceived messages
134 * @todo Currently it seems as if the following parameter aren't used at all ...
136 * @param string $parameter Parameter (force, restart, ...) for the contact polling
137 * @param integer $generation
139 private static function pollContacts($parameter, $generation) {
145 if ($parameter == 'force') {
148 if ($parameter == 'restart') {
150 $generation = intval($generation);
156 if (intval($parameter)) {
157 $manual_id = intval($parameter);
161 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
163 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
167 $d = DateTimeFormat::utcNow();
169 // Only poll from those with suitable relationships,
170 // and which have a polling address and ignore Diaspora since
171 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
173 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
174 if ($abandon_days < 1) {
177 $abandon_sql = (($abandon_days)
178 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
182 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
183 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
185 STRAIGHT_JOIN `contact`
186 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
187 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
188 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
189 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
191 dbesc(NETWORK_OSTATUS),
192 dbesc(NETWORK_DIASPORA),
197 if (!DBA::is_result($contacts)) {
201 foreach ($contacts as $contact) {
204 $contact['last-update'] = NULL_DATE;
207 // Friendica and OStatus are checked once a day
208 if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
209 $contact['priority'] = 2;
212 if ($contact['subhub'] && in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
214 * We should be getting everything via a hub. But just to be sure, let's check once a day.
215 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
216 * This also lets us update our subscription to the hub, and add or replace hubs in case it
217 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
219 $poll_interval = Config::get('system', 'pushpoll_frequency');
220 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
223 // Check Diaspora contacts or followers once a week
224 if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
225 $contact['priority'] = 4;
228 // Check archived contacts once a month
229 if ($contact['archive']) {
230 $contact['priority'] = 5;
233 if (($contact['priority'] >= 0) && !$force) {
236 $t = $contact['last-update'];
239 * Based on $contact['priority'], should we poll this site now? Or later?
241 switch ($contact['priority']) {
243 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
248 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
253 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
258 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
263 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
269 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
279 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
280 $priority = PRIORITY_MEDIUM;
281 } elseif ($contact['archive']) {
282 $priority = PRIORITY_NEGLIGIBLE;
284 $priority = PRIORITY_LOW;
287 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
289 Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);