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