forked from emoncms/sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_run.php
98 lines (79 loc) · 3.17 KB
/
sync_run.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
<?php
// Get script location
list($scriptPath) = get_included_files();
$scriptPath = str_replace("/sync_run.php","",$scriptPath);
$fp = fopen("/tmp/sync-runlock", "w");
if (! flock($fp, LOCK_EX | LOCK_NB)) { echo "Already running\n"; die; }
echo "SYNC: Starting\n";
define('EMONCMS_EXEC', 1);
chdir("/var/www/emoncms");
require "process_settings.php";
chdir($scriptPath);
require "lib/phpfina.php";
require "lib/phptimeseries.php";
// Load redis
if (!$settings['redis']['enabled']) { echo "ERROR: Redis is not enabled"; die; }
$redis = new Redis();
$connected = $redis->connect($settings['redis']['host'], $settings['redis']['port']);
if (!$connected) { echo "Can't connect to redis at ".$settings['redis']['host'].":".$settings['redis']['port']; die; }
if (!empty($settings['redis']['prefix'])) $redis->setOption(Redis::OPT_PREFIX, $settings['redis']['prefix']);
if (!empty($settings['redis']['auth'])) {
if (!$redis->auth($settings['redis']['auth'])) {
echo "Can't connect to redis at ".$settings['redis']['host'].", autentication failed"; die;
}
}
echo "SYNC: Connected to Redis\n";
while(true){
$len = $redis->llen("sync-queue");
if ($len>0) {
$syncitem = $redis->lpop("sync-queue");
print $syncitem."\n";
$params = json_decode($syncitem);
// ----------------------------------------------------------------------------
if ($params->action=="download") {
if ($params->engine==Engine::PHPFINA) {
$lastvalue = phpfina_download(
$settings['feed']['phpfina']['datadir'],
$params->local_id,
$params->remote_server,
$params->remote_id,
$params->remote_apikey
);
}
if ($params->engine==Engine::PHPTIMESERIES) {
$lastvalue = phptimeseries_download(
$settings['feed']['phptimeseries']['datadir'],
$params->local_id,
$params->remote_server,
$params->remote_id,
$params->remote_apikey
);
}
if ($lastvalue) $redis->hMset("feed:".$params->local_id, $lastvalue);
}
// ----------------------------------------------------------------------------
if ($params->action=="upload") {
if ($params->engine==Engine::PHPFINA) {
phpfina_upload(
$settings['feed']['phpfina']['datadir'],
$params->local_id,
$params->remote_server,
$params->remote_id,
$params->remote_apikey
);
}
if ($params->engine==Engine::PHPTIMESERIES) {
phptimeseries_upload(
$settings['feed']['phptimeseries']['datadir'],
$params->local_id,
$params->remote_server,
$params->remote_id,
$params->remote_apikey
);
}
}
} else {
break;
}
sleep(1);
}