Unverified Commit 8348ebf1 authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

Refactor #1196 ping (#1227)

refactor(frontend): ping backend WS

issue #1196
parent 508a229e
......@@ -1148,6 +1148,10 @@ sub list_network_interfaces($self, %args) {
return $interfaces;
}
sub _dbh {
return $CONNECTOR->dbh;
}
=head2 version
Returns the version of the main module
......
......@@ -10,7 +10,7 @@ use Moose;
no warnings "experimental::signatures";
use feature qw(signatures);
my $DEBUG=0;
my $DEBUG=1;
has clients => (
is => 'ro'
......@@ -133,16 +133,54 @@ sub _get_machine_info($rvd, $args) {
return $info;
}
sub _list_recent_requests($rvd, $seconds) {
my @now = localtime(time-$seconds);
$now[4]++;
for (0 .. 4) {
$now[$_] = "0".$now[$_] if length($now[$_])<2;
}
my $time_recent = ($now[5]+=1900)."-".$now[4]."-".$now[3]
." ".$now[2].":".$now[1].":".$now[0];
my $sth = $rvd->_dbh->prepare(
"SELECT id,command, status "
." FROM requests "
." WHERE "
." date_changed >= ? "
." ORDER BY date_changed "
);
$sth->execute($time_recent);
my @reqs;
while ( my $row = $sth->fetchrow_hashref ) {
push @reqs,($row);
}
return @reqs;
}
sub _ping_backend($rvd, $args) {
my $requests = $rvd->list_requests(undef, 120, 1);
return 1 if !scalar(@$requests);
warn Dumper($requests);
my @requests2 = grep { $_->{status} ne 'requested' } @$requests;
my @reqs = _list_recent_requests($rvd, 20);
my $requested = scalar( grep { $_->{status} eq 'requested' } @reqs );
# If there are requests in state different that requested it's ok
return 1 if scalar(@reqs) > $requested;
my ($ping_backend)
= grep {
$_->{command} eq 'ping_backend'
} @reqs ;
if (!$ping_backend) {
return 0 if $requested;
my @now = localtime(time);
my $seconds = $now[0];
warn $seconds;
Ravada::Request->ping_backend() if $seconds < 5;
return 1;
}
return 0 if !scalar(@requests2) && grep { $_->{command} eq 'ping_backend'} @$requests;
return 0 if $ping_backend->{status} eq 'requested';
warn "***".Dumper(map { [ $_->{command} => $_->{status} ] } @requests2);
return scalar (@requests2);
return 1;
}
sub _different_list($list1, $list2) {
......
......@@ -116,9 +116,6 @@ ravadaApp.directive("solShowMachine", swMach)
};
function machinesPageC($scope, $http, $interval, $timeout, request, listMach) {
$http.get('/pingbackend.json').then(function(response) {
$scope.pingbe_fail = !response.data;
});
if( $scope.check_netdata && $scope.check_netdata != "0" ) {
var url = $scope.check_netdata;
$scope.check_netdata = 0;
......@@ -140,6 +137,7 @@ ravadaApp.directive("solShowMachine", swMach)
$scope.subscribe_all=function(url) {
subscribe_list_machines(url);
subscribe_list_requests(url);
subscribe_ping_backend(url);
};
subscribe_list_machines= function(url) {
......@@ -219,6 +217,17 @@ ravadaApp.directive("solShowMachine", swMach)
}
}
subscribe_ping_backend= function(url) {
var ws = new WebSocket(url);
ws.onopen = function(event) { ws.send('ping_backend') };
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
$scope.$apply(function () {
$scope.pingbe_fail = !data;
});
}
};
$scope.orderParam = ['name'];
$scope.auto_hide_clones = true;
$scope.orderMachineList = function(type1,type2){
......@@ -299,9 +308,6 @@ ravadaApp.directive("solShowMachine", swMach)
};
function usersPageC($scope, $http, $interval, request) {
$http.get('/pingbackend.json').then(function(response) {
$scope.pingbe_fail = !response.data;
});
$scope.action = function(target,action,machineId){
$http.get('/'+target+'/'+action+'/'+machineId+'.json');
};
......@@ -309,9 +315,6 @@ ravadaApp.directive("solShowMachine", swMach)
};
function messagesPageC($scope, $http, $interval, request) {
$http.get('/pingbackend.json').then(function(response) {
$scope.pingbe_fail = !response.data;
});
$scope.getMessages = function() {
$http.get('/messages.json').then(function(response) {
$scope.list_message= response.data;
......
......@@ -112,7 +112,7 @@
if (!ws_connected) {
$scope.ws_fail = true;
}
}, 5 * 1000 );
}, 60 * 1000 );
$scope.subscribe_list_machines_user = function(url) {
var channel = 'list_machines_user';
......@@ -157,9 +157,6 @@
subscribe_ping_backend(url);
};
$http.get('/pingbackend.json').then(function(response) {
$scope.pingbe_fail = !response.data;
});
$scope.only_public = false;
$scope.toggle_only_public=function() {
$scope.only_public = !$scope.only_public;
......@@ -246,9 +243,6 @@
};
$scope.domain_remove = 0;
$scope.new_name_invalid = false;
$http.get('/pingbackend.json').then(function(response) {
$scope.pingbe_fail = !response.data;
});
$scope.machine_info = function(id) {
$http.get('/machine/info/'+$scope.showmachineId+'.json')
.then(function(response) {
......
......@@ -442,12 +442,6 @@ get '/list_lxc_templates.json' => sub {
$c->render(json => $RAVADA->list_lxc_templates);
};
get '/pingbackend.json' => sub {
my $c = shift;
$c->render(json => $RAVADA->ping_backend);
};
# machine commands
get '/machine/info/(:id).(:type)' => sub {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment