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;
18 require_once 'include/dba.php';
22 public static function execute($parameter = '', $generation = 0)
24 $a = BaseObject::getApp();
26 // Poll contacts with specific parameters
27 if (!empty($parameter)) {
28 self::pollContacts($parameter, $generation);
32 $last = Config::get('system', 'last_cron');
34 $poll_interval = intval(Config::get('system', 'cron_interval'));
35 if (! $poll_interval) {
40 $next = $last + ($poll_interval * 60);
42 Logger::log('cron intervall not reached');
47 Logger::log('cron: start');
49 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
50 Hook::fork($a->queue['priority'], "cron");
52 // run queue delivery process in the background
53 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
55 // run the process to discover global contacts in the background
56 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
58 // run the process to update locally stored global contacts in the background
59 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
61 // Expire and remove user entries
62 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
64 // Call possible post update functions
65 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
67 // Clear cache entries
68 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
70 // Repair missing Diaspora values in contacts
71 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
73 // Repair entries in the database
74 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
76 // once daily run birthday_updates and then expire in background
77 $d1 = Config::get('system', 'last_expire_day');
78 $d2 = intval(DateTimeFormat::utcNow('d'));
81 if ($d2 != intval($d1)) {
83 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
85 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
87 // update nodeinfo data
88 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
90 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
92 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
94 Worker::add(PRIORITY_LOW, 'Expire');
96 Worker::add(PRIORITY_MEDIUM, 'DBClean');
98 // check upstream version?
99 Worker::add(PRIORITY_LOW, 'CheckVersion');
101 Config::set('system', 'last_expire_day', $d2);
105 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
107 // Delete all done workerqueue entries
108 DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
110 // Optimizing this table only last seconds
111 if (Config::get('system', 'optimize_workerqueue', false)) {
112 DBA::e("OPTIMIZE TABLE `workerqueue`");
115 Config::set('system', 'last_cron_hourly', time());
118 // Ensure to have a .htaccess file.
119 // this is a precaution for systems that update automatically
120 $basepath = $a->getBasePath();
121 if (!file_exists($basepath . '/.htaccess')) {
122 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
126 self::pollContacts($parameter, $generation);
128 Logger::log('cron: end');
130 Config::set('system', 'last_cron', time());
136 * @brief Poll contacts for unreceived messages
138 * @todo Currently it seems as if the following parameter aren't used at all ...
140 * @param string $parameter Parameter (force, restart, ...) for the contact polling
141 * @param integer $generation
143 private static function pollContacts($parameter, $generation) {
149 if ($parameter == 'force') {
152 if ($parameter == 'restart') {
154 $generation = intval($generation);
160 if (intval($parameter)) {
161 $manual_id = intval($parameter);
165 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
167 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
171 $d = DateTimeFormat::utcNow();
173 // Only poll from those with suitable relationships,
174 // and which have a polling address and ignore Diaspora since
175 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
177 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
178 if ($abandon_days < 1) {
181 $abandon_sql = (($abandon_days)
182 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
186 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
187 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
189 STRAIGHT_JOIN `contact`
190 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
191 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
192 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
193 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
194 DBA::escape(Protocol::DFRN),
195 DBA::escape(Protocol::OSTATUS),
196 DBA::escape(Protocol::DIASPORA),
197 DBA::escape(Protocol::FEED),
198 DBA::escape(Protocol::MAIL)
201 if (!DBA::isResult($contacts)) {
205 foreach ($contacts as $contact) {
208 $contact['last-update'] = DBA::NULL_DATETIME;
211 // Friendica and OStatus are checked once a day
212 if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
213 $contact['priority'] = 2;
216 if ($contact['subhub'] && in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
218 * We should be getting everything via a hub. But just to be sure, let's check once a day.
219 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
220 * This also lets us update our subscription to the hub, and add or replace hubs in case it
221 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
223 $poll_interval = Config::get('system', 'pushpoll_frequency');
224 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
227 // Check Diaspora contacts or followers once a week
228 if (($contact["network"] == Protocol::DIASPORA) || ($contact["rel"] == Contact::FOLLOWER)) {
229 $contact['priority'] = 4;
232 // Check archived contacts once a month
233 if ($contact['archive']) {
234 $contact['priority'] = 5;
237 if (($contact['priority'] >= 0) && !$force) {
240 $t = $contact['last-update'];
243 * Based on $contact['priority'], should we poll this site now? Or later?
245 switch ($contact['priority']) {
247 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
252 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
257 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
262 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
267 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
273 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
283 if (($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) {
284 $priority = PRIORITY_MEDIUM;
285 } elseif ($contact['archive']) {
286 $priority = PRIORITY_NEGLIGIBLE;
288 $priority = PRIORITY_LOW;
291 Logger::log("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
293 Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);