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