3 * @file src/Worker/Cron.php
5 namespace Friendica\Worker;
7 use Friendica\Core\Config;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBM;
12 require_once 'include/dba.php';
15 public static function execute($parameter = '', $generation = 0) {
18 require_once 'include/datetime.php';
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 // run queue delivery process in the background
44 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
46 // run the process to discover global contacts in the background
47 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
49 // run the process to update locally stored global contacts in the background
50 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
52 // Expire and remove user entries
53 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
55 // Call possible post update functions
56 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
58 // update nodeinfo data
59 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
61 // Clear cache entries
62 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
64 // Repair missing Diaspora values in contacts
65 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
67 // Repair entries in the database
68 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
70 // once daily run birthday_updates and then expire in background
71 $d1 = Config::get('system', 'last_expire_day');
72 $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
74 if ($d2 != intval($d1)) {
76 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
78 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
80 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
82 Config::set('system', 'last_expire_day', $d2);
84 Worker::add(PRIORITY_LOW, 'Expire');
86 Worker::add(PRIORITY_MEDIUM, 'DBClean');
88 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
90 // Delete all done workerqueue entries
91 dba::delete('workerqueue', array('`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR'));
93 // check upstream version?
94 Worker::add(PRIORITY_LOW, 'CheckVersion');
98 self::pollContacts($parameter, $generation);
102 Config::set('system', 'last_cron', time());
108 * @brief Poll contacts for unreceived messages
110 * @todo Currently it seems as if the following parameter aren't used at all ...
112 * @param string $parameter Parameter (force, restart, ...) for the contact polling
113 * @param integer $generation
115 private static function pollContacts($parameter, $generation) {
121 if ($parameter == 'force') {
124 if ($parameter == 'restart') {
126 $generation = intval($generation);
132 if (intval($parameter)) {
133 $manual_id = intval($parameter);
137 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
139 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
143 $d = datetime_convert();
145 // Only poll from those with suitable relationships,
146 // and which have a polling address and ignore Diaspora since
147 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
149 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
150 if ($abandon_days < 1) {
153 $abandon_sql = (($abandon_days)
154 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
158 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
159 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
161 STRAIGHT_JOIN `contact`
162 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
163 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
164 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
165 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
167 dbesc(NETWORK_OSTATUS),
168 dbesc(NETWORK_DIASPORA),
173 if (!DBM::is_result($contacts)) {
177 foreach ($contacts as $contact) {
180 $contact['last-update'] = NULL_DATE;
183 // Friendica and OStatus are checked once a day
184 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS))) {
185 $contact['priority'] = 2;
188 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS))) {
190 * We should be getting everything via a hub. But just to be sure, let's check once a day.
191 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
192 * This also lets us update our subscription to the hub, and add or replace hubs in case it
193 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
195 $poll_interval = Config::get('system', 'pushpoll_frequency');
196 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
199 // Check Diaspora contacts or followers once a week
200 if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
201 $contact['priority'] = 4;
204 // Check archived contacts once a month
205 if ($contact['archive']) {
206 $contact['priority'] = 5;
209 if (($contact['priority'] >= 0) && !$force) {
212 $t = $contact['last-update'];
215 * Based on $contact['priority'], should we poll this site now? Or later?
217 switch ($contact['priority']) {
219 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
224 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
229 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
234 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
239 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
245 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
255 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
256 $priority = PRIORITY_MEDIUM;
257 } elseif ($contact['archive']) {
258 $priority = PRIORITY_NEGLIGIBLE;
260 $priority = PRIORITY_LOW;
263 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
265 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'OnePoll', (int)$contact['id']);