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