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;
13 public static function execute($parameter = '', $generation = 0) {
16 require_once 'include/datetime.php';
18 // Poll contacts with specific parameters
19 if (!empty($parameter)) {
20 self::pollContacts($parameter, $generation);
24 $last = Config::get('system', 'last_cron');
26 $poll_interval = intval(Config::get('system', 'cron_interval'));
27 if (! $poll_interval) {
32 $next = $last + ($poll_interval * 60);
34 logger('cron intervall not reached');
39 logger('cron: start');
41 // run queue delivery process in the background
42 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
44 // run the process to discover global contacts in the background
45 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
47 // run the process to update locally stored global contacts in the background
48 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
50 // Expire and remove user entries
51 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
53 // Call possible post update functions
54 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
56 // update nodeinfo data
57 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
59 // Clear cache entries
60 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
62 // Repair missing Diaspora values in contacts
63 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
65 // Repair entries in the database
66 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
68 // once daily run birthday_updates and then expire in background
69 $d1 = Config::get('system', 'last_expire_day');
70 $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
72 if ($d2 != intval($d1)) {
74 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
76 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
78 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
80 Config::set('system', 'last_expire_day', $d2);
82 Worker::add(PRIORITY_LOW, 'Expire');
84 Worker::add(PRIORITY_MEDIUM, 'DBClean');
86 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
88 // Delete all done workerqueue entries
89 dba::delete('workerqueue', array('`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR'));
91 // check upstream version?
92 Worker::add(PRIORITY_LOW, 'CheckVersion');
96 self::pollContacts($parameter, $generation);
100 Config::set('system', 'last_cron', time());
106 * @brief Poll contacts for unreceived messages
108 * @todo Currently it seems as if the following parameter aren't used at all ...
110 * @param string $parameter Parameter (force, restart, ...) for the contact polling
111 * @param integer $generation
113 private static function pollContacts($parameter, $generation) {
119 if ($parameter == 'force') {
122 if ($parameter == 'restart') {
124 $generation = intval($generation);
130 if (intval($parameter)) {
131 $manual_id = intval($parameter);
135 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
137 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
141 $d = datetime_convert();
143 // Only poll from those with suitable relationships,
144 // and which have a polling address and ignore Diaspora since
145 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
147 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
148 if ($abandon_days < 1) {
151 $abandon_sql = (($abandon_days)
152 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
156 $contacts = q("SELECT `contact`.`id` FROM `user`
157 STRAIGHT_JOIN `contact`
158 ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
159 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
160 AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
161 AND NOT `contact`.`archive`
162 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
163 intval(CONTACT_IS_SHARING),
164 intval(CONTACT_IS_FRIEND),
167 dbesc(NETWORK_OSTATUS),
173 if (!DBM::is_result($contacts)) {
177 foreach ($contacts as $c) {
179 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
183 if (!DBM::is_result($res)) {
187 foreach ($res as $contact) {
192 $contact['last-update'] = NULL_DATE;
195 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
196 $contact['priority'] = 2;
199 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
201 * We should be getting everything via a hub. But just to be sure, let's check once a day.
202 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
203 * This also lets us update our subscription to the hub, and add or replace hubs in case it
204 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
206 $poll_interval = Config::get('system', 'pushpoll_frequency');
207 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
210 if (($contact['priority'] >= 0) && !$force) {
213 $t = $contact['last-update'];
216 * Based on $contact['priority'], should we poll this site now? Or later?
218 switch ($contact['priority']) {
220 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
225 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
230 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
235 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
240 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
246 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
256 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
258 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
259 $priority = PRIORITY_MEDIUM;
261 $priority = PRIORITY_LOW;
263 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'OnePoll', (int)$contact['id']);