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\Protocol;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Util\DateTimeFormat;
16 require_once 'include/dba.php';
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('cron intervall not reached');
45 logger('cron: start');
47 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
48 Addon::forkHooks($a->queue['priority'], "cron");
50 // run queue delivery process in the background
51 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
53 // run the process to discover global contacts in the background
54 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
56 // run the process to update locally stored global contacts in the background
57 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
59 // Expire and remove user entries
60 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
62 // Call possible post update functions
63 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
65 // Clear cache entries
66 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
68 // Repair missing Diaspora values in contacts
69 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
71 // Repair entries in the database
72 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
74 // once daily run birthday_updates and then expire in background
75 $d1 = Config::get('system', 'last_expire_day');
76 $d2 = intval(DateTimeFormat::utcNow('d'));
79 if ($d2 != intval($d1)) {
81 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
83 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
85 // update nodeinfo data
86 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
88 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
90 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
92 Worker::add(PRIORITY_LOW, 'Expire');
94 Worker::add(PRIORITY_MEDIUM, 'DBClean');
96 // check upstream version?
97 Worker::add(PRIORITY_LOW, 'CheckVersion');
99 Config::set('system', 'last_expire_day', $d2);
103 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
105 // Delete all done workerqueue entries
106 DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
108 // Optimizing this table only last seconds
109 if (Config::get('system', 'optimize_workerqueue', false)) {
110 DBA::e("OPTIMIZE TABLE `workerqueue`");
113 Config::set('system', 'last_cron_hourly', time());
116 // Ensure to have a .htaccess file.
117 // this is a precaution for systems that update automatically
118 $basepath = $a->get_basepath();
119 if (!file_exists($basepath . '/.htaccess')) {
120 copy($basepath . '/.htaccess-dist', $basepath . '/.htaccess');
124 self::pollContacts($parameter, $generation);
128 Config::set('system', 'last_cron', time());
134 * @brief Poll contacts for unreceived messages
136 * @todo Currently it seems as if the following parameter aren't used at all ...
138 * @param string $parameter Parameter (force, restart, ...) for the contact polling
139 * @param integer $generation
141 private static function pollContacts($parameter, $generation) {
147 if ($parameter == 'force') {
150 if ($parameter == 'restart') {
152 $generation = intval($generation);
158 if (intval($parameter)) {
159 $manual_id = intval($parameter);
163 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
165 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
169 $d = DateTimeFormat::utcNow();
171 // Only poll from those with suitable relationships,
172 // and which have a polling address and ignore Diaspora since
173 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
175 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
176 if ($abandon_days < 1) {
179 $abandon_sql = (($abandon_days)
180 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
184 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
185 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
187 STRAIGHT_JOIN `contact`
188 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
189 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
190 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
191 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
192 DBA::escape(Protocol::DFRN),
193 DBA::escape(Protocol::OSTATUS),
194 DBA::escape(Protocol::DIASPORA),
195 DBA::escape(Protocol::FEED),
196 DBA::escape(Protocol::MAIL)
199 if (!DBA::isResult($contacts)) {
203 foreach ($contacts as $contact) {
206 $contact['last-update'] = NULL_DATE;
209 // Friendica and OStatus are checked once a day
210 if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
211 $contact['priority'] = 2;
214 if ($contact['subhub'] && in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
216 * We should be getting everything via a hub. But just to be sure, let's check once a day.
217 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
218 * This also lets us update our subscription to the hub, and add or replace hubs in case it
219 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
221 $poll_interval = Config::get('system', 'pushpoll_frequency');
222 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
225 // Check Diaspora contacts or followers once a week
226 if (($contact["network"] == Protocol::DIASPORA) || ($contact["rel"] == Contact::FOLLOWER)) {
227 $contact['priority'] = 4;
230 // Check archived contacts once a month
231 if ($contact['archive']) {
232 $contact['priority'] = 5;
235 if (($contact['priority'] >= 0) && !$force) {
238 $t = $contact['last-update'];
241 * Based on $contact['priority'], should we poll this site now? Or later?
243 switch ($contact['priority']) {
245 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
250 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
255 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
260 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
265 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
271 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
281 if (($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) {
282 $priority = PRIORITY_MEDIUM;
283 } elseif ($contact['archive']) {
284 $priority = PRIORITY_NEGLIGIBLE;
286 $priority = PRIORITY_LOW;
289 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
291 Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);