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'));
33 if (! $poll_interval) {
38 $next = $last + ($poll_interval * 60);
40 Logger::log('cron intervall not reached');
45 Logger::log('cron: start');
47 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
48 Hook::fork($a->queue['priority'], "cron");
50 // run the process to discover global contacts in the background
51 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
53 // run the process to update locally stored global contacts in the background
54 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
56 // Expire and remove user entries
57 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
59 // Call possible post update functions
60 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
62 // Clear cache entries
63 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
65 // Repair missing Diaspora values in contacts
66 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
68 // Repair entries in the database
69 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
71 // once daily run birthday_updates and then expire in background
72 $d1 = Config::get('system', 'last_expire_day');
73 $d2 = intval(DateTimeFormat::utcNow('d'));
76 if ($d2 != intval($d1)) {
78 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
80 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
82 // update nodeinfo data
83 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
85 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
87 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
89 Worker::add(PRIORITY_LOW, 'Expire');
91 Worker::add(PRIORITY_MEDIUM, 'DBClean');
93 // check upstream version?
94 Worker::add(PRIORITY_LOW, 'CheckVersion');
96 Config::set('system', 'last_expire_day', $d2);
100 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
102 // Delete all done workerqueue entries
103 DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
105 // Optimizing this table only last seconds
106 if (Config::get('system', 'optimize_workerqueue', false)) {
107 DBA::e("OPTIMIZE TABLE `workerqueue`");
110 Config::set('system', 'last_cron_hourly', time());
113 // Ensure to have a .htaccess file.
114 // this is a precaution for systems that update automatically
115 $basepath = $a->getBasePath();
116 if (!file_exists($basepath . '/.htaccess')) {
117 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
121 self::pollContacts($parameter, $generation);
123 Logger::log('cron: end');
125 Config::set('system', 'last_cron', time());
131 * @brief Poll contacts for unreceived messages
133 * @todo Currently it seems as if the following parameter aren't used at all ...
135 * @param string $parameter Parameter (force, restart, ...) for the contact polling
136 * @param integer $generation
137 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
139 private static function pollContacts($parameter, $generation) {
144 if ($parameter == 'force') {
147 if ($parameter == 'restart') {
148 $generation = intval($generation);
154 if (intval($parameter)) {
155 $manual_id = intval($parameter);
159 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
161 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
165 // Only poll from those with suitable relationships,
166 // and which have a polling address and ignore Diaspora since
167 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
169 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
170 if ($abandon_days < 1) {
173 $abandon_sql = (($abandon_days)
174 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
178 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
179 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
181 STRAIGHT_JOIN `contact`
182 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
183 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
184 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
185 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
186 DBA::escape(Protocol::DFRN),
187 DBA::escape(Protocol::OSTATUS),
188 DBA::escape(Protocol::DIASPORA),
189 DBA::escape(Protocol::FEED),
190 DBA::escape(Protocol::MAIL)
193 if (!DBA::isResult($contacts)) {
197 foreach ($contacts as $contact) {
200 $contact['last-update'] = DBA::NULL_DATETIME;
203 // Friendica and OStatus are checked once a day
204 if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
205 $contact['priority'] = 2;
208 if ($contact['subhub'] && in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
210 * We should be getting everything via a hub. But just to be sure, let's check once a day.
211 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
212 * This also lets us update our subscription to the hub, and add or replace hubs in case it
213 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
215 $poll_interval = Config::get('system', 'pushpoll_frequency');
216 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
219 // Check Diaspora contacts or followers once a week
220 if (($contact["network"] == Protocol::DIASPORA) || ($contact["rel"] == Contact::FOLLOWER)) {
221 $contact['priority'] = 4;
224 // Check archived contacts once a month
225 if ($contact['archive']) {
226 $contact['priority'] = 5;
229 if (($contact['priority'] >= 0) && !$force) {
232 $t = $contact['last-update'];
235 * Based on $contact['priority'], should we poll this site now? Or later?
237 switch ($contact['priority']) {
239 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
244 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
249 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
254 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
259 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
265 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
275 if (($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) {
276 $priority = PRIORITY_MEDIUM;
277 } elseif ($contact['archive']) {
278 $priority = PRIORITY_NEGLIGIBLE;
280 $priority = PRIORITY_LOW;
283 Logger::log("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
285 Worker::add(['priority' => $priority, 'dont_fork' => true, 'force_priority' => true], 'OnePoll', (int)$contact['id']);