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