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