3 * @file src/Worker/Cron.php
5 namespace Friendica\Worker;
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBM;
11 use Friendica\Util\DateTimeFormat;
14 require_once 'include/dba.php';
17 public static function execute($parameter = '', $generation = 0) {
20 // Poll contacts with specific parameters
21 if (!empty($parameter)) {
22 self::pollContacts($parameter, $generation);
26 $last = Config::get('system', 'last_cron');
28 $poll_interval = intval(Config::get('system', 'cron_interval'));
29 if (! $poll_interval) {
34 $next = $last + ($poll_interval * 60);
36 logger('cron intervall not reached');
41 logger('cron: start');
43 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
44 Addon::forkHooks($a->queue['priority'], "cron");
46 // run queue delivery process in the background
47 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
49 // run the process to discover global contacts in the background
50 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
52 // run the process to update locally stored global contacts in the background
53 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
55 // Expire and remove user entries
56 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
58 // Call possible post update functions
59 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
61 // update nodeinfo data
62 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
64 // Clear cache entries
65 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
67 // Repair missing Diaspora values in contacts
68 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
70 // Repair entries in the database
71 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
73 // once daily run birthday_updates and then expire in background
74 $d1 = Config::get('system', 'last_expire_day');
75 $d2 = intval(DateTimeFormat::utcNow('d'));
78 if ($d2 != intval($d1)) {
80 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
82 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
84 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
86 Config::set('system', 'last_expire_day', $d2);
88 Worker::add(PRIORITY_LOW, 'Expire');
90 Worker::add(PRIORITY_MEDIUM, 'DBClean');
92 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
94 // check upstream version?
95 Worker::add(PRIORITY_LOW, 'CheckVersion');
99 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
101 // Delete all done workerqueue entries
102 dba::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
104 // Optimizing this table only last seconds
105 dba::e("OPTIMIZE TABLE `workerqueue`");
107 Config::set('system', 'last_cron_hourly', time());
111 self::pollContacts($parameter, $generation);
115 Config::set('system', 'last_cron', time());
121 * @brief Poll contacts for unreceived messages
123 * @todo Currently it seems as if the following parameter aren't used at all ...
125 * @param string $parameter Parameter (force, restart, ...) for the contact polling
126 * @param integer $generation
128 private static function pollContacts($parameter, $generation) {
134 if ($parameter == 'force') {
137 if ($parameter == 'restart') {
139 $generation = intval($generation);
145 if (intval($parameter)) {
146 $manual_id = intval($parameter);
150 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
152 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
156 $d = DateTimeFormat::utcNow();
158 // Only poll from those with suitable relationships,
159 // and which have a polling address and ignore Diaspora since
160 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
162 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
163 if ($abandon_days < 1) {
166 $abandon_sql = (($abandon_days)
167 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
171 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
172 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
174 STRAIGHT_JOIN `contact`
175 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
176 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
177 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
178 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
180 dbesc(NETWORK_OSTATUS),
181 dbesc(NETWORK_DIASPORA),
186 if (!DBM::is_result($contacts)) {
190 foreach ($contacts as $contact) {
193 $contact['last-update'] = NULL_DATE;
196 // Friendica and OStatus are checked once a day
197 if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
198 $contact['priority'] = 2;
201 if ($contact['subhub'] && in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
203 * We should be getting everything via a hub. But just to be sure, let's check once a day.
204 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
205 * This also lets us update our subscription to the hub, and add or replace hubs in case it
206 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
208 $poll_interval = Config::get('system', 'pushpoll_frequency');
209 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
212 // Check Diaspora contacts or followers once a week
213 if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
214 $contact['priority'] = 4;
217 // Check archived contacts once a month
218 if ($contact['archive']) {
219 $contact['priority'] = 5;
222 if (($contact['priority'] >= 0) && !$force) {
225 $t = $contact['last-update'];
228 * Based on $contact['priority'], should we poll this site now? Or later?
230 switch ($contact['priority']) {
232 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
237 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
242 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
247 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
252 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
258 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
268 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
269 $priority = PRIORITY_MEDIUM;
270 } elseif ($contact['archive']) {
271 $priority = PRIORITY_NEGLIGIBLE;
273 $priority = PRIORITY_LOW;
276 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
278 Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);