]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
Merge pull request #5433 from annando/mysqli
[friendica.git] / src / Worker / Cron.php
1 <?php
2 /**
3  * @file src/Worker/Cron.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\BaseObject;
8 use Friendica\Core\Addon;
9 use Friendica\Core\Config;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBA;
12 use Friendica\Database\DBM;
13 use Friendica\Util\DateTimeFormat;
14
15 require_once 'include/dba.php';
16
17 class Cron
18 {
19         public static function execute($parameter = '', $generation = 0)
20         {
21                 $a = BaseObject::getApp();
22
23                 // Poll contacts with specific parameters
24                 if (!empty($parameter)) {
25                         self::pollContacts($parameter, $generation);
26                         return;
27                 }
28
29                 $last = Config::get('system', 'last_cron');
30
31                 $poll_interval = intval(Config::get('system', 'cron_interval'));
32                 if (! $poll_interval) {
33                         $poll_interval = 10;
34                 }
35
36                 if ($last) {
37                         $next = $last + ($poll_interval * 60);
38                         if ($next > time()) {
39                                 logger('cron intervall not reached');
40                                 return;
41                         }
42                 }
43
44                 logger('cron: start');
45
46                 // Fork the cron jobs in separate parts to avoid problems when one of them is crashing
47                 Addon::forkHooks($a->queue['priority'], "cron");
48
49                 // run queue delivery process in the background
50                 Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
51
52                 // run the process to discover global contacts in the background
53                 Worker::add(PRIORITY_LOW, "DiscoverPoCo");
54
55                 // run the process to update locally stored global contacts in the background
56                 Worker::add(PRIORITY_LOW, "DiscoverPoCo", "checkcontact");
57
58                 // Expire and remove user entries
59                 Worker::add(PRIORITY_MEDIUM, "CronJobs", "expire_and_remove_users");
60
61                 // Call possible post update functions
62                 Worker::add(PRIORITY_LOW, "CronJobs", "post_update");
63
64                 // Clear cache entries
65                 Worker::add(PRIORITY_LOW, "CronJobs", "clear_cache");
66
67                 // Repair missing Diaspora values in contacts
68                 Worker::add(PRIORITY_LOW, "CronJobs", "repair_diaspora");
69
70                 // Repair entries in the database
71                 Worker::add(PRIORITY_LOW, "CronJobs", "repair_database");
72
73                 // once daily run birthday_updates and then expire in background
74                 $d1 = Config::get('system', 'last_expire_day');
75                 $d2 = intval(DateTimeFormat::utcNow('d'));
76
77                 // Daily cron calls
78                 if ($d2 != intval($d1)) {
79
80                         Worker::add(PRIORITY_LOW, "CronJobs", "update_contact_birthdays");
81
82                         Worker::add(PRIORITY_LOW, "CronJobs", "update_photo_albums");
83
84                         // update nodeinfo data
85                         Worker::add(PRIORITY_LOW, "CronJobs", "nodeinfo");
86
87                         Worker::add(PRIORITY_LOW, "DiscoverPoCo", "update_server");
88
89                         Worker::add(PRIORITY_LOW, "DiscoverPoCo", "suggestions");
90
91                         Worker::add(PRIORITY_LOW, 'Expire');
92
93                         Worker::add(PRIORITY_MEDIUM, 'DBClean');
94
95                         // check upstream version?
96                         Worker::add(PRIORITY_LOW, 'CheckVersion');
97
98                         Config::set('system', 'last_expire_day', $d2);
99                 }
100
101                 // Hourly cron calls
102                 if (Config::get('system', 'last_cron_hourly', 0) + 3600 < time()) {
103
104                         // Delete all done workerqueue entries
105                         DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
106
107                         // Optimizing this table only last seconds
108                         if (Config::get('system', 'optimize_workerqueue', false)) {
109                                 DBA::e("OPTIMIZE TABLE `workerqueue`");
110                         }
111
112                         Config::set('system', 'last_cron_hourly', time());
113                 }
114
115                 // Poll contacts
116                 self::pollContacts($parameter, $generation);
117
118                 logger('cron: end');
119
120                 Config::set('system', 'last_cron', time());
121
122                 return;
123         }
124
125         /**
126          * @brief Poll contacts for unreceived messages
127          *
128          * @todo Currently it seems as if the following parameter aren't used at all ...
129          *
130          * @param string $parameter Parameter (force, restart, ...) for the contact polling
131          * @param integer $generation
132          */
133         private static function pollContacts($parameter, $generation) {
134                 $manual_id  = 0;
135                 $generation = 0;
136                 $force      = false;
137                 $restart    = false;
138
139                 if ($parameter == 'force') {
140                         $force = true;
141                 }
142                 if ($parameter == 'restart') {
143                         $restart = true;
144                         $generation = intval($generation);
145                         if (!$generation) {
146                                 killme();
147                         }
148                 }
149
150                 if (intval($parameter)) {
151                         $manual_id = intval($parameter);
152                         $force     = true;
153                 }
154
155                 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
156
157                 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
158
159                 Addon::reload();
160
161                 $d = DateTimeFormat::utcNow();
162
163                 // Only poll from those with suitable relationships,
164                 // and which have a polling address and ignore Diaspora since
165                 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
166
167                 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
168                 if ($abandon_days < 1) {
169                         $abandon_days = 0;
170                 }
171                 $abandon_sql = (($abandon_days)
172                         ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
173                         : ''
174                 );
175
176                 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
177                                         `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
178                                 FROM `user`
179                                 STRAIGHT_JOIN `contact`
180                                 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
181                                         AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
182                                         AND NOT `contact`.`self` AND NOT `contact`.`blocked`
183                                 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
184                         dbesc(NETWORK_DFRN),
185                         dbesc(NETWORK_OSTATUS),
186                         dbesc(NETWORK_DIASPORA),
187                         dbesc(NETWORK_FEED),
188                         dbesc(NETWORK_MAIL)
189                 );
190
191                 if (!DBM::is_result($contacts)) {
192                         return;
193                 }
194
195                 foreach ($contacts as $contact) {
196
197                         if ($manual_id) {
198                                 $contact['last-update'] = NULL_DATE;
199                         }
200
201                         // Friendica and OStatus are checked once a day
202                         if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
203                                 $contact['priority'] = 2;
204                         }
205
206                         if ($contact['subhub'] && in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
207                                 /*
208                                  * We should be getting everything via a hub. But just to be sure, let's check once a day.
209                                  * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
210                                  * This also lets us update our subscription to the hub, and add or replace hubs in case it
211                                  * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
212                                  */
213                                 $poll_interval = Config::get('system', 'pushpoll_frequency');
214                                 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
215                         }
216
217                         // Check Diaspora contacts or followers once a week
218                         if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
219                                 $contact['priority'] = 4;
220                         }
221
222                         // Check archived contacts once a month
223                         if ($contact['archive']) {
224                                 $contact['priority'] = 5;
225                         }
226
227                         if (($contact['priority'] >= 0) && !$force) {
228                                 $update = false;
229
230                                 $t = $contact['last-update'];
231
232                                 /*
233                                  * Based on $contact['priority'], should we poll this site now? Or later?
234                                  */
235                                 switch ($contact['priority']) {
236                                         case 5:
237                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
238                                                         $update = true;
239                                                 }
240                                                 break;
241                                         case 4:
242                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
243                                                         $update = true;
244                                                 }
245                                                 break;
246                                         case 3:
247                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
248                                                         $update = true;
249                                                 }
250                                                 break;
251                                         case 2:
252                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
253                                                         $update = true;
254                                                 }
255                                                 break;
256                                         case 1:
257                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
258                                                         $update = true;
259                                                 }
260                                                 break;
261                                         case 0:
262                                         default:
263                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
264                                                         $update = true;
265                                                 }
266                                                 break;
267                                 }
268                                 if (!$update) {
269                                         continue;
270                                 }
271                         }
272
273                         if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
274                                 $priority = PRIORITY_MEDIUM;
275                         } elseif ($contact['archive']) {
276                                 $priority = PRIORITY_NEGLIGIBLE;
277                         } else {
278                                 $priority = PRIORITY_LOW;
279                         }
280
281                         logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
282
283                         Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);
284                 }
285         }
286 }