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\Protocol;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Contact;
15 use Friendica\Util\DateTimeFormat;
17 require_once 'include/dba.php';
21 public static function execute($parameter = '', $generation = 0)
23 $a = BaseObject::getApp();
25 // Poll contacts with specific parameters
26 if (!empty($parameter)) {
27 self::pollContacts($parameter, $generation);
31 $last = Config::get('system', 'last_cron');
33 $poll_interval = intval(Config::get('system', 'cron_interval'));
34 if (! $poll_interval) {
39 $next = $last + ($poll_interval * 60);
41 logger('cron intervall not reached');
46 logger('cron: start');
48 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
49 Hook::fork($a->queue['priority'], "cron");
51 // run queue delivery process in the background
52 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
54 // run the process to discover global contacts in the background
55 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
57 // run the process to update locally stored global contacts in the background
58 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
60 // Expire and remove user entries
61 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
63 // Call possible post update functions
64 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
66 // Clear cache entries
67 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
69 // Repair missing Diaspora values in contacts
70 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
72 // Repair entries in the database
73 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
75 // once daily run birthday_updates and then expire in background
76 $d1 = Config::get('system', 'last_expire_day');
77 $d2 = intval(DateTimeFormat::utcNow('d'));
80 if ($d2 != intval($d1)) {
82 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
84 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
86 // update nodeinfo data
87 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
89 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
91 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
93 Worker::add(PRIORITY_LOW, 'Expire');
95 Worker::add(PRIORITY_MEDIUM, 'DBClean');
97 // check upstream version?
98 Worker::add(PRIORITY_LOW, 'CheckVersion');
100 Config::set('system', 'last_expire_day', $d2);
104 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
106 // Delete all done workerqueue entries
107 DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
109 // Optimizing this table only last seconds
110 if (Config::get('system', 'optimize_workerqueue', false)) {
111 DBA::e("OPTIMIZE TABLE `workerqueue`");
114 Config::set('system', 'last_cron_hourly', time());
117 // Ensure to have a .htaccess file.
118 // this is a precaution for systems that update automatically
119 $basepath = $a->getBasePath();
120 if (!file_exists($basepath . '/.htaccess')) {
121 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
125 self::pollContacts($parameter, $generation);
129 Config::set('system', 'last_cron', time());
135 * @brief Poll contacts for unreceived messages
137 * @todo Currently it seems as if the following parameter aren't used at all ...
139 * @param string $parameter Parameter (force, restart, ...) for the contact polling
140 * @param integer $generation
142 private static function pollContacts($parameter, $generation) {
148 if ($parameter == 'force') {
151 if ($parameter == 'restart') {
153 $generation = intval($generation);
159 if (intval($parameter)) {
160 $manual_id = intval($parameter);
164 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
166 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
170 $d = DateTimeFormat::utcNow();
172 // Only poll from those with suitable relationships,
173 // and which have a polling address and ignore Diaspora since
174 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
176 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
177 if ($abandon_days < 1) {
180 $abandon_sql = (($abandon_days)
181 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
185 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
186 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
188 STRAIGHT_JOIN `contact`
189 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
190 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
191 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
192 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
193 DBA::escape(Protocol::DFRN),
194 DBA::escape(Protocol::OSTATUS),
195 DBA::escape(Protocol::DIASPORA),
196 DBA::escape(Protocol::FEED),
197 DBA::escape(Protocol::MAIL)
200 if (!DBA::isResult($contacts)) {
204 foreach ($contacts as $contact) {
207 $contact['last-update'] = DBA::NULL_DATETIME;
210 // Friendica and OStatus are checked once a day
211 if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
212 $contact['priority'] = 2;
215 if ($contact['subhub'] && in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
217 * We should be getting everything via a hub. But just to be sure, let's check once a day.
218 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
219 * This also lets us update our subscription to the hub, and add or replace hubs in case it
220 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
222 $poll_interval = Config::get('system', 'pushpoll_frequency');
223 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
226 // Check Diaspora contacts or followers once a week
227 if (($contact["network"] == Protocol::DIASPORA) || ($contact["rel"] == Contact::FOLLOWER)) {
228 $contact['priority'] = 4;
231 // Check archived contacts once a month
232 if ($contact['archive']) {
233 $contact['priority'] = 5;
236 if (($contact['priority'] >= 0) && !$force) {
239 $t = $contact['last-update'];
242 * Based on $contact['priority'], should we poll this site now? Or later?
244 switch ($contact['priority']) {
246 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
251 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
256 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
261 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
266 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
272 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
282 if (($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) {
283 $priority = PRIORITY_MEDIUM;
284 } elseif ($contact['archive']) {
285 $priority = PRIORITY_NEGLIGIBLE;
287 $priority = PRIORITY_LOW;
290 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
292 Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);