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 = get_config('system', 'last_cron');
19 $poll_interval = intval(get_config('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 = get_config('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 set_config('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'));
86 cron_poll_contacts($argc, $argv);
90 set_config('system', 'last_cron', time());
96 * @brief Poll contacts for unreceived messages
98 * @param Integer $argc Number of command line arguments
99 * @param Array $argv Array of command line arguments
101 function cron_poll_contacts($argc, $argv) {
107 if (($argc > 1) && ($argv[1] == 'force')) {
110 if (($argc > 1) && ($argv[1] == 'restart')) {
112 $generation = intval($argv[2]);
118 if (($argc > 1) && intval($argv[1])) {
119 $manual_id = intval($argv[1]);
123 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
125 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
129 $d = datetime_convert();
131 // Only poll from those with suitable relationships,
132 // and which have a polling address and ignore Diaspora since
133 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
135 $abandon_days = intval(get_config('system', 'account_abandon_days'));
136 if ($abandon_days < 1) {
139 $abandon_sql = (($abandon_days)
140 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
144 $contacts = q("SELECT `contact`.`id` FROM `user`
145 STRAIGHT_JOIN `contact`
146 ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
147 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
148 AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
149 AND NOT `contact`.`archive`
150 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
151 intval(CONTACT_IS_SHARING),
152 intval(CONTACT_IS_FRIEND),
155 dbesc(NETWORK_OSTATUS),
161 if (!dbm::is_result($contacts)) {
165 foreach ($contacts as $c) {
167 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
171 if (!dbm::is_result($res)) {
175 foreach ($res as $contact) {
180 $contact['last-update'] = NULL_DATE;
183 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
184 $contact['priority'] = 2;
187 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
189 * We should be getting everything via a hub. But just to be sure, let's check once a day.
190 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
191 * This also lets us update our subscription to the hub, and add or replace hubs in case it
192 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
194 $poll_interval = get_config('system', 'pushpoll_frequency');
195 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
198 if (($contact['priority'] >= 0) && !$force) {
201 $t = $contact['last-update'];
204 * Based on $contact['priority'], should we poll this site now? Or later?
206 switch ($contact['priority']) {
208 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
213 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
218 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
223 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
228 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
234 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
244 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
246 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
247 $priority = PRIORITY_MEDIUM;
249 $priority = PRIORITY_LOW;
251 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'onepoll', (int)$contact['id']);