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`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
157 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
159 STRAIGHT_JOIN `contact`
160 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
161 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
162 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
163 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
165 dbesc(NETWORK_OSTATUS),
166 dbesc(NETWORK_DIASPORA),
171 if (!DBM::is_result($contacts)) {
175 foreach ($contacts as $contact) {
178 $contact['last-update'] = NULL_DATE;
181 // Friendica and OStatus are checked once a day
182 if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS))) {
183 $contact['priority'] = 2;
186 if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS))) {
188 * We should be getting everything via a hub. But just to be sure, let's check once a day.
189 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
190 * This also lets us update our subscription to the hub, and add or replace hubs in case it
191 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
193 $poll_interval = Config::get('system', 'pushpoll_frequency');
194 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
197 // Check Diaspora contacts or followers once a week
198 if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
199 $contact['priority'] = 4;
202 // Check archived contacts once a month
203 if ($contact['archive']) {
204 $contact['priority'] = 5;
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 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
254 $priority = PRIORITY_MEDIUM;
255 } elseif ($contact['archive']) {
256 $priority = PRIORITY_NEGLIGIBLE;
258 $priority = PRIORITY_LOW;
261 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
263 Worker::add(array('priority' => $priority, 'dont_fork' => true), 'OnePoll', (int)$contact['id']);