]> git.mxchange.org Git - friendica.git/blob - include/cron.php
Semaphore based locking and hopefully the fix for the workerqueue
[friendica.git] / include / cron.php
1 <?php
2
3 use Friendica\Core\Config;
4
5 function cron_run(&$argv, &$argc){
6         global $a;
7
8         require_once 'include/datetime.php';
9
10         // Poll contacts with specific parameters
11         if ($argc > 1) {
12                 cron_poll_contacts($argc, $argv);
13                 return;
14         }
15
16         $last = get_config('system', 'last_cron');
17
18         $poll_interval = intval(get_config('system', 'cron_interval'));
19         if (! $poll_interval) {
20                 $poll_interval = 10;
21         }
22
23         if ($last) {
24                 $next = $last + ($poll_interval * 60);
25                 if ($next > time()) {
26                         logger('cron intervall not reached');
27                         return;
28                 }
29         }
30
31         logger('cron: start');
32
33         // run queue delivery process in the background
34         proc_run(PRIORITY_NEGLIGIBLE, "include/queue.php");
35
36         // run the process to discover global contacts in the background
37         proc_run(PRIORITY_LOW, "include/discover_poco.php");
38
39         // run the process to update locally stored global contacts in the background
40         proc_run(PRIORITY_LOW, "include/discover_poco.php", "checkcontact");
41
42         // Expire and remove user entries
43         proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "expire_and_remove_users");
44
45         // Check OStatus conversations
46         proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "ostatus_mentions");
47
48         // Check every conversation
49         proc_run(PRIORITY_MEDIUM, "include/cronjobs.php", "ostatus_conversations");
50
51         // Call possible post update functions
52         proc_run(PRIORITY_LOW, "include/cronjobs.php", "post_update");
53
54         // update nodeinfo data
55         proc_run(PRIORITY_LOW, "include/cronjobs.php", "nodeinfo");
56
57         // Clear cache entries
58         proc_run(PRIORITY_LOW, "include/cronjobs.php", "clear_cache");
59
60         // Repair missing Diaspora values in contacts
61         proc_run(PRIORITY_LOW, "include/cronjobs.php", "repair_diaspora");
62
63         // Repair entries in the database
64         proc_run(PRIORITY_LOW, "include/cronjobs.php", "repair_database");
65
66         // once daily run birthday_updates and then expire in background
67         $d1 = get_config('system', 'last_expire_day');
68         $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
69
70         if ($d2 != intval($d1)) {
71
72                 proc_run(PRIORITY_LOW, "include/cronjobs.php", "update_contact_birthdays");
73
74                 proc_run(PRIORITY_LOW, "include/discover_poco.php", "update_server");
75
76                 proc_run(PRIORITY_LOW, "include/discover_poco.php", "suggestions");
77
78                 set_config('system', 'last_expire_day', $d2);
79
80                 proc_run(PRIORITY_LOW, 'include/expire.php');
81
82                 proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
83
84                 proc_run(PRIORITY_LOW, "include/cronjobs.php", "update_photo_albums");
85
86                 // Delete all done workerqueue entries
87                 dba::e('DELETE FROM `workerqueue` WHERE `done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 12 HOUR');
88         }
89
90         // Poll contacts
91         cron_poll_contacts($argc, $argv);
92
93         logger('cron: end');
94
95         set_config('system', 'last_cron', time());
96
97         return;
98 }
99
100 /**
101  * @brief Poll contacts for unreceived messages
102  *
103  * @param Integer $argc Number of command line arguments
104  * @param Array $argv Array of command line arguments
105  */
106 function cron_poll_contacts($argc, $argv) {
107         $manual_id  = 0;
108         $generation = 0;
109         $force      = false;
110         $restart    = false;
111
112         if (($argc > 1) && ($argv[1] == 'force')) {
113                 $force = true;
114         }
115         if (($argc > 1) && ($argv[1] == 'restart')) {
116                 $restart = true;
117                 $generation = intval($argv[2]);
118                 if (!$generation) {
119                         killme();
120                 }
121         }
122
123         if (($argc > 1) && intval($argv[1])) {
124                 $manual_id = intval($argv[1]);
125                 $force     = true;
126         }
127
128         $min_poll_interval = Config::get('system', 'min_poll_interval', 1);
129
130         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
131
132         reload_plugins();
133
134         $d = datetime_convert();
135
136         // Only poll from those with suitable relationships,
137         // and which have a polling address and ignore Diaspora since
138         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
139
140         $abandon_days = intval(get_config('system', 'account_abandon_days'));
141         if ($abandon_days < 1) {
142                 $abandon_days = 0;
143         }
144         $abandon_sql = (($abandon_days)
145                 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
146                 : ''
147         );
148
149         $contacts = q("SELECT `contact`.`id` FROM `user`
150                         STRAIGHT_JOIN `contact`
151                         ON `contact`.`uid` = `user`.`uid` AND `contact`.`rel` IN (%d, %d) AND `contact`.`poll` != ''
152                                 AND `contact`.`network` IN ('%s', '%s', '%s', '%s', '%s', '%s') $sql_extra
153                                 AND NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly`
154                                 AND NOT `contact`.`archive`
155                         WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
156                 intval(CONTACT_IS_SHARING),
157                 intval(CONTACT_IS_FRIEND),
158                 dbesc(NETWORK_DFRN),
159                 dbesc(NETWORK_ZOT),
160                 dbesc(NETWORK_OSTATUS),
161                 dbesc(NETWORK_FEED),
162                 dbesc(NETWORK_MAIL),
163                 dbesc(NETWORK_MAIL2)
164         );
165
166         if (!dbm::is_result($contacts)) {
167                 return;
168         }
169
170         foreach ($contacts as $c) {
171
172                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
173                         intval($c['id'])
174                 );
175
176                 if (!dbm::is_result($res)) {
177                         continue;
178                 }
179
180                 foreach ($res as $contact) {
181
182                         $xml = false;
183
184                         if ($manual_id) {
185                                 $contact['last-update'] = NULL_DATE;
186                         }
187
188                         if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
189                                 $contact['priority'] = 2;
190                         }
191
192                         if ($contact['subhub'] && in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
193                                 /*
194                                  * We should be getting everything via a hub. But just to be sure, let's check once a day.
195                                  * (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
196                                  * This also lets us update our subscription to the hub, and add or replace hubs in case it
197                                  * changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
198                                  */
199                                 $poll_interval = get_config('system', 'pushpoll_frequency');
200                                 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
201                         }
202
203                         if (($contact['priority'] >= 0) && !$force) {
204                                 $update = false;
205
206                                 $t = $contact['last-update'];
207
208                                 /*
209                                  * Based on $contact['priority'], should we poll this site now? Or later?
210                                  */
211                                 switch ($contact['priority']) {
212                                         case 5:
213                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
214                                                         $update = true;
215                                                 }
216                                                 break;
217                                         case 4:
218                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
219                                                         $update = true;
220                                                 }
221                                                 break;
222                                         case 3:
223                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
224                                                         $update = true;
225                                                 }
226                                                 break;
227                                         case 2:
228                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
229                                                         $update = true;
230                                                 }
231                                                 break;
232                                         case 1:
233                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
234                                                         $update = true;
235                                                 }
236                                                 break;
237                                         case 0:
238                                         default:
239                                                 if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + ".$min_poll_interval." minute")) {
240                                                         $update = true;
241                                                 }
242                                                 break;
243                                 }
244                                 if (!$update) {
245                                         continue;
246                                 }
247                         }
248
249                         logger("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact["nick"] . " " . $contact["name"]);
250
251                         if (($contact['network'] == NETWORK_FEED) && ($contact['priority'] <= 3)) {
252                                 $priority = PRIORITY_MEDIUM;
253                         } else {
254                                 $priority = PRIORITY_LOW;
255                         }
256                         proc_run(array('priority' => $priority, 'dont_fork' => true), 'include/onepoll.php', intval($contact['id']));
257                 }
258         }
259 }