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