3 use Friendica\Core\Config;
4 use Friendica\Core\Worker;
6 function cron_run(&$argv, &$argc){
9 require_once 'include/datetime.php';
11 // Poll contacts with specific parameters
13 cron_poll_contacts($argc, $argv);
17 $last = Config::get('system', 'last_cron');
19 $poll_interval = intval(Config::get('system', 'cron_interval'));
20 if (! $poll_interval) {
25 $next = $last + ($poll_interval * 60);
27 logger('cron intervall not reached');
32 logger('cron: start');
34 // run queue delivery process in the background
35 Worker::add(PRIORITY_NEGLIGIBLE, "queue");
37 // run the process to discover global contacts in the background
38 Worker::add(PRIORITY_LOW, "discover_poco");
40 // run the process to update locally stored global contacts in the background
41 Worker::add(PRIORITY_LOW, "discover_poco", "checkcontact");
43 // Expire and remove user entries
44 Worker::add(PRIORITY_MEDIUM, "cronjobs", "expire_and_remove_users");
46 // Call possible post update functions
47 Worker::add(PRIORITY_LOW, "cronjobs", "post_update");
49 // update nodeinfo data
50 Worker::add(PRIORITY_LOW, "cronjobs", "nodeinfo");
52 // Clear cache entries
53 Worker::add(PRIORITY_LOW, "cronjobs", "clear_cache");
55 // Repair missing Diaspora values in contacts
56 Worker::add(PRIORITY_LOW, "cronjobs", "repair_diaspora");
58 // Repair entries in the database
59 Worker::add(PRIORITY_LOW, "cronjobs", "repair_database");
61 // once daily run birthday_updates and then expire in background
62 $d1 = Config::get('system', 'last_expire_day');
63 $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
65 if ($d2 != intval($d1)) {
67 Worker::add(PRIORITY_LOW, "cronjobs", "update_contact_birthdays");
69 Worker::add(PRIORITY_LOW, "discover_poco", "update_server");
71 Worker::add(PRIORITY_LOW, "discover_poco", "suggestions");
73 Config::set('system', 'last_expire_day', $d2);
75 Worker::add(PRIORITY_LOW, 'expire');
77 Worker::add(PRIORITY_MEDIUM, 'dbclean');
79 Worker::add(PRIORITY_LOW, "cronjobs", "update_photo_albums");
81 // Delete all done workerqueue entries
82 dba::delete('workerqueue', array('`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR'));
84 // check upstream version?
85 Worker::add(PRIORITY_LOW, 'checkversion');
89 cron_poll_contacts($argc, $argv);
93 Config::set('system', 'last_cron', time());
99 * @brief Poll contacts for unreceived messages
101 * @param Integer $argc Number of command line arguments
102 * @param Array $argv Array of command line arguments
104 function cron_poll_contacts($argc, $argv) {
110 if (($argc > 1) && ($argv[1] == 'force')) {
113 if (($argc > 1) && ($argv[1] == 'restart')) {
115 $generation = intval($argv[2]);
121 if (($argc > 1) && intval($argv[1])) {
122 $manual_id = intval($argv[1]);
126 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
128 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
132 $d = datetime_convert();
134 // Only poll from those with suitable relationships,
135 // and which have a polling address and ignore Diaspora since
136 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
138 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
139 if ($abandon_days < 1) {
142 $abandon_sql = (($abandon_days)
143 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
147 $contacts = q("SELECT `contact`.`id` FROM `user`
148 STRAIGHT_JOIN `contact`
149 ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
150 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
151 AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
152 AND NOT `contact`.`archive`
153 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
154 intval(CONTACT_IS_SHARING),
155 intval(CONTACT_IS_FRIEND),
158 dbesc(NETWORK_OSTATUS),
164 if (!dbm::is_result($contacts)) {
168 foreach ($contacts as $c) {
170 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
174 if (!dbm::is_result($res)) {
178 foreach ($res as $contact) {
183 $contact['last-update'] = NULL_DATE;
186 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
187 $contact['priority'] = 2;
190 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
192 * We should be getting everything via a hub. But just to be sure, let's check once a day.
193 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
194 * This also lets us update our subscription to the hub, and add or replace hubs in case it
195 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
197 $poll_interval = Config::get('system', 'pushpoll_frequency');
198 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
201 if (($contact['priority'] >= 0) && !$force) {
204 $t = $contact['last-update'];
207 * Based on $contact['priority'], should we poll this site now? Or later?
209 switch ($contact['priority']) {
211 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
216 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
221 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
226 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
231 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
237 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
247 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
249 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
250 $priority = PRIORITY_MEDIUM;
252 $priority = PRIORITY_LOW;
254 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'onepoll', (int)$contact['id']);