]> git.mxchange.org Git - friendica.git/blob - include/cronhooks.php
Merge pull request #2099 from rabuzarus/2611_new_user_addr
[friendica.git] / include / cronhooks.php
1 <?php
2
3 require_once("boot.php");
4
5
6 function cronhooks_run(&$argv, &$argc){
7         global $a, $db;
8
9         if(is_null($a)) {
10                 $a = new App;
11         }
12
13         if(is_null($db)) {
14                 @include(".htconfig.php");
15                 require_once("include/dba.php");
16                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
17                 unset($db_host, $db_user, $db_pass, $db_data);
18         };
19
20         require_once('include/session.php');
21         require_once('include/datetime.php');
22         require_once('include/pidfile.php');
23
24         load_config('config');
25         load_config('system');
26
27         $maxsysload = intval(get_config('system','maxloadavg'));
28         if($maxsysload < 1)
29                 $maxsysload = 50;
30         if(function_exists('sys_getloadavg')) {
31                 $load = sys_getloadavg();
32                 if(intval($load[0]) > $maxsysload) {
33                         logger('system: load ' . $load[0] . ' too high. Cronhooks deferred to next scheduled run.');
34                         return;
35                 }
36         }
37
38         $last = get_config('system','last_cronhook');
39
40         $poll_interval = intval(get_config('system','cronhook_interval'));
41         if(! $poll_interval)
42                 $poll_interval = 9;
43
44         if($last) {
45                 $next = $last + ($poll_interval * 60);
46                 if($next > time()) {
47                         logger('cronhook intervall not reached');
48                         return;
49                 }
50         }
51
52         $lockpath = get_lockpath();
53         if ($lockpath != '') {
54                 $pidfile = new pidfile($lockpath, 'cronhooks');
55                 if($pidfile->is_already_running()) {
56                         logger("cronhooks: Already running");
57                         if ($pidfile->running_time() > 19*60) {
58                                 $pidfile->kill();
59                                 logger("cronhooks: killed stale process");
60                                 // Calling a new instance
61                                 proc_run('php','include/cronhooks.php');
62                         }
63                         exit;
64                 }
65         }
66
67         $a->set_baseurl(get_config('system','url'));
68
69         load_hooks();
70
71         logger('cronhooks: start');
72
73         $d = datetime_convert();
74
75         call_hooks('cron', $d);
76
77         logger('cronhooks: end');
78
79         set_config('system','last_cronhook', time());
80
81         return;
82 }
83
84 if (array_search(__file__,get_included_files())===0){
85         cronhooks_run($_SERVER["argv"],$_SERVER["argc"]);
86         killme();
87 }