2 namespace Friendica\Worker;
4 use Friendica\Core\Config;
5 use Friendica\Core\Worker;
6 use Friendica\Database\DBM;
10 public static function execute($parameter = '', $generation = 0) {
13 require_once 'include/datetime.php';
15 // Poll contacts with specific parameters
16 if (!empty($parameter)) {
17 self::pollContacts($parameter, $generation);
21 $last = Config::get('system', 'last_cron');
23 $poll_interval = intval(Config::get('system', 'cron_interval'));
24 if (! $poll_interval) {
29 $next = $last + ($poll_interval * 60);
31 logger('cron intervall not reached');
36 logger('cron: start');
38 // run queue delivery process in the background
39 Worker::add(PRIORITY_NEGLIGIBLE, "queue");
41 // run the process to discover global contacts in the background
42 Worker::add(PRIORITY_LOW, "discover_poco");
44 // run the process to update locally stored global contacts in the background
45 Worker::add(PRIORITY_LOW, "discover_poco", "checkcontact");
47 // Expire and remove user entries
48 Worker::add(PRIORITY_MEDIUM, "cronjobs", "expire_and_remove_users");
50 // Call possible post update functions
51 Worker::add(PRIORITY_LOW, "cronjobs", "post_update");
53 // update nodeinfo data
54 Worker::add(PRIORITY_LOW, "cronjobs", "nodeinfo");
56 // Clear cache entries
57 Worker::add(PRIORITY_LOW, "cronjobs", "clear_cache");
59 // Repair missing Diaspora values in contacts
60 Worker::add(PRIORITY_LOW, "cronjobs", "repair_diaspora");
62 // Repair entries in the database
63 Worker::add(PRIORITY_LOW, "cronjobs", "repair_database");
65 // once daily run birthday_updates and then expire in background
66 $d1 = Config::get('system', 'last_expire_day');
67 $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
69 if ($d2 != intval($d1)) {
71 Worker::add(PRIORITY_LOW, "cronjobs", "update_contact_birthdays");
73 Worker::add(PRIORITY_LOW, "discover_poco", "update_server");
75 Worker::add(PRIORITY_LOW, "discover_poco", "suggestions");
77 Config::set('system', 'last_expire_day', $d2);
79 Worker::add(PRIORITY_LOW, 'expire');
81 Worker::add(PRIORITY_MEDIUM, 'dbclean');
83 Worker::add(PRIORITY_LOW, "cronjobs", "update_photo_albums");
85 // Delete all done workerqueue entries
86 dba::delete('workerqueue', array('`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR'));
88 // check upstream version?
89 Worker::add(PRIORITY_LOW, 'CheckVersion');
93 self::pollContacts($parameter, $generation);
97 Config::set('system', 'last_cron', time());
103 * @brief Poll contacts for unreceived messages
105 * @todo Currently it seems as if the following parameter aren't used at all ...
107 * @param string $parameter Parameter (force, restart, ...) for the contact polling
108 * @param integer $generation
110 private static function pollContacts($parameter, $generation) {
116 if ($parameter == 'force') {
119 if ($parameter == 'restart') {
121 $generation = intval($generation);
127 if (intval($parameter)) {
128 $manual_id = intval($parameter);
132 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
134 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
138 $d = datetime_convert();
140 // Only poll from those with suitable relationships,
141 // and which have a polling address and ignore Diaspora since
142 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
144 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
145 if ($abandon_days < 1) {
148 $abandon_sql = (($abandon_days)
149 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
153 $contacts = q("SELECT `contact`.`id` FROM `user`
154 STRAIGHT_JOIN `contact`
155 ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
156 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
157 AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
158 AND NOT `contact`.`archive`
159 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
160 intval(CONTACT_IS_SHARING),
161 intval(CONTACT_IS_FRIEND),
164 dbesc(NETWORK_OSTATUS),
170 if (!DBM::is_result($contacts)) {
174 foreach ($contacts as $c) {
176 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
180 if (!DBM::is_result($res)) {
184 foreach ($res as $contact) {
189 $contact['last-update'] = NULL_DATE;
192 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
193 $contact['priority'] = 2;
196 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
198 * We should be getting everything via a hub. But just to be sure, let's check once a day.
199 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
200 * This also lets us update our subscription to the hub, and add or replace hubs in case it
201 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
203 $poll_interval = Config::get('system', 'pushpoll_frequency');
204 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
207 if (($contact['priority'] >= 0) && !$force) {
210 $t = $contact['last-update'];
213 * Based on $contact['priority'], should we poll this site now? Or later?
215 switch ($contact['priority']) {
217 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
222 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
227 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
232 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
237 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
243 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
253 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
255 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
256 $priority = PRIORITY_MEDIUM;
258 $priority = PRIORITY_LOW;
260 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'OnePoll', (int)$contact['id']);