]> git.mxchange.org Git - friendica.git/blob - include/cronhooks.php
Merge pull request #2347 from annando/1602-dfrn-forum
[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
31         $load = current_load();
32         if($load) {
33                 if(intval($load) > $maxsysload) {
34                         logger('system: load ' . $load . ' too high. Cronhooks deferred to next scheduled run.');
35                         return;
36                 }
37         }
38
39         $last = get_config('system','last_cronhook');
40
41         $poll_interval = intval(get_config('system','cronhook_interval'));
42         if(! $poll_interval)
43                 $poll_interval = 9;
44
45         if($last) {
46                 $next = $last + ($poll_interval * 60);
47                 if($next > time()) {
48                         logger('cronhook intervall not reached');
49                         return;
50                 }
51         }
52
53         $lockpath = get_lockpath();
54         if ($lockpath != '') {
55                 $pidfile = new pidfile($lockpath, 'cronhooks');
56                 if($pidfile->is_already_running()) {
57                         logger("cronhooks: Already running");
58                         if ($pidfile->running_time() > 19*60) {
59                                 $pidfile->kill();
60                                 logger("cronhooks: killed stale process");
61                                 // Calling a new instance
62                                 proc_run('php','include/cronhooks.php');
63                         }
64                         exit;
65                 }
66         }
67
68         $a->set_baseurl(get_config('system','url'));
69
70         load_hooks();
71
72         logger('cronhooks: start');
73
74         $d = datetime_convert();
75
76         call_hooks('cron', $d);
77
78         logger('cronhooks: end');
79
80         set_config('system','last_cronhook', time());
81
82         return;
83 }
84
85 if (array_search(__file__,get_included_files())===0){
86         cronhooks_run($_SERVER["argv"],$_SERVER["argc"]);
87         killme();
88 }