/
input_queue_processor.php
139 lines (107 loc) · 4.84 KB
/
input_queue_processor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project: http://openenergymonitor.org
*/
define('EMONCMS_EXEC', 1);
$fp = fopen("/var/lock/input_queue_processor.lock", "w");
if (! flock($fp, LOCK_EX | LOCK_NB)) { echo "Already running\n"; die; }
chdir(dirname(__FILE__)."/../");
require "Lib/EmonLogger.php";
require "process_settings.php";
if (!$redis_enabled) { echo "Error: setting must be true: redis_enabled\n"; die; }
$log = new EmonLogger(__FILE__);
$log->info("Starting REDIS Input Queue Processor script");
$mysqli = @new mysqli($server,$username,$password,$database,$port);
if ($mysqli->connect_error) { $log->error("Can't connect to database:". $mysqli->connect_error); die('Check log\n'); }
if ($redis_enabled) {
$redis = new Redis();
if (!$redis->connect($redis_server['host'], $redis_server['port'])) {
$log->error("Could not connect to redis at ".$redis_server['host'].":".$redis_server['port']); die('Check log\n');
}
if (!empty($redis_server['prefix'])) $redis->setOption(Redis::OPT_PREFIX, $redis_server['prefix']);
if (!empty($redis_server['auth'])) {
if (!$redis->auth($redis_server['auth'])) {
$log->error("Could not connect to redis at ".$redis_server['host'].", autentication failed"); die('Check log\n');
}
}
} else {
$redis = false;
}
require("Modules/user/user_model.php");
$user = new User($mysqli,$redis,null);
include "Modules/feed/feed_model.php";
$feed = new Feed($mysqli,$redis, $feed_settings);
require "Modules/input/input_model.php";
$input = new Input($mysqli,$redis, $feed);
require "Modules/process/process_model.php";
$process = new Process($mysqli,$input,$feed);
$rn = 0;
$ltime = time();
$usleep = 100000;
while(true)
{
if ((time()-$ltime)>=1)
{
$ltime = time();
$buflength = $redis->llen('buffer');
// A basic throthler to stop the script using up cpu when there is nothing to do.
// Fine tune sleep
if ($buflength<2) {
$usleep += 50;
} else {
$usleep -= 50;
}
// if there is a big buffer reduce sleep to zero to clear buffer.
if ($buflength>100) $usleep = 0;
// if throughput is low then increase sleep significantly
if ($rn==0) $usleep = 100000;
// sleep cant be less than zero
if ($usleep<0) $usleep = 0;
echo "Buffer length: ".$buflength." ".$usleep." ".$rn."\n";
$rn = 0;
}
// check if there is an item in the queue to process
$line_str = false;
if ($redis->llen('buffer')>0)
{
// check if there is an item in the queue to process
$line_str = $redis->lpop('buffer');
}
if ($line_str)
{
$rn++;
//echo $line_str."\n";
$packet = json_decode($line_str);
$userid = $packet->userid;
$time = $packet->time;
$nodeid = $packet->nodeid;
$data = $packet->data;
// Load current user input meta data
// It would be good to avoid repeated calls to this
$dbinputs = $input->get_inputs($userid);
if (!isset($dbinputs[$nodeid]) && (count($dbinputs) >= $max_node_id_limit )) {
$log->error("Reached the maximal allowed number of different NodeIds, limit is $max_node_id_limit. Node '$nodeid' was ignored.");
} else {
$tmp = array();
foreach ($data as $name => $value)
{
if (!isset($dbinputs[$nodeid][$name])) {
$inputid = $input->create_input($userid, $nodeid, $name);
$dbinputs[$nodeid][$name] = true;
$dbinputs[$nodeid][$name] = array('id'=>$inputid, 'processList'=>'');
$input->set_timevalue($dbinputs[$nodeid][$name]['id'],$time,$value);
} else {
$inputid = $dbinputs[$nodeid][$name]['id'];
$input->set_timevalue($dbinputs[$nodeid][$name]['id'],$time,$value);
if ($dbinputs[$nodeid][$name]['processList']) $tmp[] = array('value'=>$value,'processList'=>$dbinputs[$nodeid][$name]['processList']);
}
}
foreach ($tmp as $i) $process->input($time,$i['value'],$i['processList']);
}
}
usleep($usleep);
}