]> git.mxchange.org Git - friendica.git/blob - src/Worker/Cron.php
Add support for legacy $lang config in App->loadConfig
[friendica.git] / src / Worker / Cron.php
1 <?php
2 /**
3  * @file src/Worker/Cron.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBM;
11 use Friendica\Util\DateTimeFormat;
12 use dba;
13
14 require_once 'include/dba.php';
15
16 class Cron
17 {
18         public static function execute($parameter = '', $generation = 0)
19         {
20                 $a = \Friendica\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                 // Poll contacts
115                 self::pollContacts($parameter, $generation);
116
117                 logger('cron: end');
118
119                 Config::set('system', 'last_cron', time());
120
121                 return;
122         }
123
124         /**
125          * @brief Poll contacts for unreceived messages
126          *
127          * @todo Currently it seems as if the following parameter aren't used at all ...
128          *
129          * @param string $parameter Parameter (force, restart, ...) for the contact polling
130          * @param integer $generation
131          */
132         private static function pollContacts($parameter, $generation) {
133                 $manual_id  = 0;
134                 $generation = 0;
135                 $force      = false;
136                 $restart    = false;
137
138                 if ($parameter == 'force') {
139                         $force = true;
140                 }
141                 if ($parameter == 'restart') {
142                         $restart = true;
143                         $generation = intval($generation);
144                         if (!$generation) {
145                                 killme();
146                         }
147                 }
148
149                 if (intval($parameter)) {
150                         $manual_id = intval($parameter);
151                         $force     = true;
152                 }
153
154                 $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
155
156                 $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
157
158                 Addon::reload();
159
160                 $d = DateTimeFormat::utcNow();
161
162                 // Only poll from those with suitable relationships,
163                 // and which have a polling address and ignore Diaspora since
164                 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
165
166                 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
167                 if ($abandon_days < 1) {
168                         $abandon_days = 0;
169                 }
170                 $abandon_sql = (($abandon_days)
171                         ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
172                         : ''
173                 );
174
175                 $contacts = q("SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
176                                         `contact`.`last-update`, `contact`.`priority`, `contact`.`rel`, `contact`.`subhub`
177                                 FROM `user`
178                                 STRAIGHT_JOIN `contact`
179                                 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
180                                         AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s') $sql_extra
181                                         AND NOT `contact`.`self` AND NOT `contact`.`blocked`
182                                 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql",
183                         dbesc(NETWORK_DFRN),
184                         dbesc(NETWORK_OSTATUS),
185                         dbesc(NETWORK_DIASPORA),
186                         dbesc(NETWORK_FEED),
187                         dbesc(NETWORK_MAIL)
188                 );
189
190                 if (!DBM::is_result($contacts)) {
191                         return;
192                 }
193
194                 foreach ($contacts as $contact) {
195
196                         if ($manual_id) {
197                                 $contact['last-update'] = NULL_DATE;
198                         }
199
200                         // Friendica and OStatus are checked once a day
201                         if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
202                                 $contact['priority'] = 2;
203                         }
204
205                         if ($contact['subhub'] && in_array($contact['network'], [NETWORK_DFRN, NETWORK_OSTATUS])) {
206                                 /*
207                                  * We should be getting everything via a hub. But just to be sure, let's check once a day.
208                                  * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
209                                  * This also lets us update our subscription to the hub, and add or replace hubs in case it
210                                  * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
211                                  */
212                                 $poll_interval = Config::get('system', 'pushpoll_frequency');
213                                 $contact['priority'] = (!is_null($poll_interval) ? intval($poll_interval) : 3);
214                         }
215
216                         // Check Diaspora contacts or followers once a week
217                         if (($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
218                                 $contact['priority'] = 4;
219                         }
220
221                         // Check archived contacts once a month
222                         if ($contact['archive']) {
223                                 $contact['priority'] = 5;
224                         }
225
226                         if (($contact['priority'] >= 0) && !$force) {
227                                 $update = false;
228
229                                 $t = $contact['last-update'];
230
231                                 /*
232                                  * Based on $contact['priority'], should we poll this site now? Or later?
233                                  */
234                                 switch ($contact['priority']) {
235                                         case 5:
236                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 month")) {
237                                                         $update = true;
238                                                 }
239                                                 break;
240                                         case 4:
241                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 week")) {
242                                                         $update = true;
243                                                 }
244                                                 break;
245                                         case 3:
246                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
247                                                         $update = true;
248                                                 }
249                                                 break;
250                                         case 2:
251                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 12 hour")) {
252                                                         $update = true;
253                                                 }
254                                                 break;
255                                         case 1:
256                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 hour")) {
257                                                         $update = true;
258                                                 }
259                                                 break;
260                                         case 0:
261                                         default:
262                                                 if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + ".$min_poll_interval." minute")) {
263                                                         $update = true;
264                                                 }
265                                                 break;
266                                 }
267                                 if (!$update) {
268                                         continue;
269                                 }
270                         }
271
272                         if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
273                                 $priority = PRIORITY_MEDIUM;
274                         } elseif ($contact['archive']) {
275                                 $priority = PRIORITY_NEGLIGIBLE;
276                         } else {
277                                 $priority = PRIORITY_LOW;
278                         }
279
280                         logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
281
282                         Worker::add(['priority' => $priority, 'dont_fork' => true], 'OnePoll', (int)$contact['id']);
283                 }
284         }
285 }