3 * @file src/Worker/Cron.php
5 namespace Friendica\Worker;
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBM;
13 require_once 'include/dba.php';
16 public static function execute($parameter = '', $generation = 0) {
19 require_once 'include/datetime.php';
21 // Poll contacts with specific parameters
22 if (!empty($parameter)) {
23 self::pollContacts($parameter, $generation);
27 $last = Config::get('system', 'last_cron');
29 $poll_interval = intval(Config::get('system', 'cron_interval'));
30 if (! $poll_interval) {
35 $next = $last + ($poll_interval * 60);
37 logger('cron intervall not reached');
42 logger('cron: start');
44 // run queue delivery process in the background
45 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
47 // run the process to discover global contacts in the background
48 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
50 // run the process to update locally stored global contacts in the background
51 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
53 // Expire and remove user entries
54 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
56 // Call possible post update functions
57 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
59 // update nodeinfo data
60 Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
62 // Clear cache entries
63 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
65 // Repair missing Diaspora values in contacts
66 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
68 // Repair entries in the database
69 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
71 // once daily run birthday_updates and then expire in background
72 $d1 = Config::get('system', 'last_expire_day');
73 $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
75 if ($d2 != intval($d1)) {
77 Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
79 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
81 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
83 Config::set('system', 'last_expire_day', $d2);
85 Worker::add(PRIORITY_LOW, 'Expire');
87 Worker::add(PRIORITY_MEDIUM, 'DBClean');
89 Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
91 // Delete all done workerqueue entries
92 dba::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR']);
94 // check upstream version?
95 Worker::add(PRIORITY_LOW, 'CheckVersion');
99 self::pollContacts($parameter, $generation);
103 Config::set('system', 'last_cron', time());
109 * @brief Poll contacts for unreceived messages
111 * @todo Currently it seems as if the following parameter aren't used at all ...
113 * @param string $parameter Parameter (force, restart, ...) for the contact polling
114 * @param integer $generation
116 private static function pollContacts($parameter, $generation) {
122 if ($parameter == 'force') {
125 if ($parameter == 'restart') {
127 $generation = intval($generation);
133 if (intval($parameter)) {
134 $manual_id = intval($parameter);
138 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
140 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
144 $d = datetime_convert();
146 // Only poll from those with suitable relationships,
147 // and which have a polling address and ignore Diaspora since
148 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
150 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
151 if ($abandon_days < 1) {
154 $abandon_sql = (($abandon_days)
155 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
159 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
160 `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
162 STRAIGHT_JOIN `contact`
163 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
164 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
165 AND NOT `contact`.`self` AND NOT `contact`.`blocked`
166 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
168 dbesc(NETWORK_OSTATUS),
169 dbesc(NETWORK_DIASPORA),
174 if (!DBM::is_result($contacts)) {
178 foreach ($contacts as $contact) {
181 $contact['last-update'] = NULL_DATE;
184 // Friendica and OStatus are checked once a day
185 if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
186 $contact['priority'] = 2;
189 if ($contact['subhub'] && in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
191 * We should be getting everything via a hub. But just to be sure, let's check once a day.
192 * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
193 * This also lets us update our subscription to the hub, and add or replace hubs in case it
194 * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
196 $poll_interval = Config::get('system', 'pushpoll_frequency');
197 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
200 // Check Diaspora contacts or followers once a week
201 if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
202 $contact['priority'] = 4;
205 // Check archived contacts once a month
206 if ($contact['archive']) {
207 $contact['priority'] = 5;
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 if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
257 $priority = PRIORITY_MEDIUM;
258 } elseif ($contact['archive']) {
259 $priority = PRIORITY_NEGLIGIBLE;
261 $priority = PRIORITY_LOW;
264 logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
266 Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);