-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsync_run.php
75 lines (60 loc) · 2.14 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
<?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";
chdir($scriptPath);
require "lib/phpfina.php";
require "lib/phptimeseries.php";
require_once "/var/www/emoncms/Lib/load_emoncms.php";
require_once "Modules/feed/feed_model.php";
$feed = new Feed($mysqli,$redis,$settings["feed"]);
echo "SYNC: Connected to Redis\n";
// -----------------------------------------------------------------
// 1. Seperate sync queue into feeds to download and feeds to upload
// -----------------------------------------------------------------
$download_queue = array();
// $upload_queue = array();
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") {
$download_queue[] = $params;
}
if ($params->action=="upload") {
// No longer handled here (use sync_upload via emoncms_sync.service).
// $upload_queue[] = $params;
}
} else {
break;
}
}
// -----------------------------------------------------------------
// 2. Process download
// -----------------------------------------------------------------
foreach ($download_queue as $params) {
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);
}