Unverified Commit 616903fc authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

fix(backend): clenup process as requests (#833)

Speed up the backend making the cleanup process
regular requests

issue #828
parent 7257a7c0
......@@ -155,8 +155,8 @@ sub do_start {
$ravada->process_requests();
$ravada->process_long_requests(0,$NOFORK) if $NOFORK;
if ( time - $t_refresh > 60 ) {
$ravada->_cmd_refresh_vms() if rand(5)<3;
$ravada->enforce_limits() if rand(5)<2;
Ravada::Request->refresh_vms() if rand(5)<3;
Ravada::Request->enforce_limits() if rand(5)<2;
$t_refresh = time;
}
sleep 1 if time - $t0 <1;
......
......@@ -79,7 +79,7 @@ our $DIR_SQL = "sql/mysql";
$DIR_SQL = "/usr/share/doc/ravada/sql/mysql" if ! -e $DIR_SQL;
# LONG commands take long
our %HUGE_COMMAND = map { $_ => 1 } qw(download prepare_base remove_base);
our %HUGE_COMMAND = map { $_ => 1 } qw(download prepare_base remove_base enforce_limits refresh_vms);
our %LONG_COMMAND = map { $_ => 1 } (qw(screenshot shutdown force_shutdown ), keys %HUGE_COMMAND);
our $USER_DAEMON;
......@@ -2565,11 +2565,13 @@ sub _req_method {
,screenshot => \&_cmd_screenshot
,copy_screenshot => \&_cmd_copy_screenshot
,remove_base => \&_cmd_remove_base
,refresh_vms => \&_cmd_refresh_vms
,ping_backend => \&_cmd_ping_backend
,prepare_base => \&_cmd_prepare_base
,rename_domain => \&_cmd_rename_domain
,open_iptables => \&_cmd_open_iptables
,list_vm_types => \&_cmd_list_vm_types
,enforce_limits => \&_cmd_enforce_limits
,force_shutdown => \&_cmd_force_shutdown
,refresh_storage => \&_cmd_refresh_storage
,domain_autostart=> \&_cmd_domain_autostart
......@@ -2662,29 +2664,13 @@ sub import_domain {
return $vm->import_domain($name, $user, $spinoff_disks);
}
=head2 enforce_limits
Check no user has passed the limits and take action.
Some limits:
- More than 1 domain running at a time ( older get shut down )
=cut
sub enforce_limits {
_enforce_limits_active(@_);
sub _cmd_enforce_limits($self, $request=undef) {
_enforce_limits_active($self, $request);
}
sub _enforce_limits_active {
my $self = shift;
my %args = @_;
sub _enforce_limits_active($self, $request) {
my $timeout = (delete $args{timeout} or 10);
confess "ERROR: Unknown arguments ".join(",",sort keys %args)
if keys %args;
my $timeout = ($request->defined_arg('timeout') or 10);
my %domains;
for my $domain ($self->list_domains( active => 1 )) {
......
......@@ -749,6 +749,8 @@ sub list_requests($self, $id_domain_req=undef, $seconds=60) {
|| $command eq 'screenshot'
|| $command eq 'hibernate'
|| $command eq 'ping_backend'
|| $command eq 'enforce_limits'
|| $command eq 'refresh_vms'
|| $command eq 'refresh_storage';
next if $id_domain_req && defined $id_domain && $id_domain != $id_domain_req;
my $args;
......
......@@ -70,6 +70,8 @@ our %VALID_ARG = (
,add_hardware => {uid => 1, id_domain => 1, name => 1, number => 1}
,remove_hardware => {uid => 1, id_domain => 1, name => 1, index => 1}
,change_max_memory => {uid => 1, id_domain => 1, ram => 1}
,refresh_vms => { }
,enforce_limits => { timeout => 60 }
);
our %CMD_SEND_MESSAGE = map { $_ => 1 }
......@@ -1055,6 +1057,39 @@ sub domain_autostart {
, args => $args
);
}
sub refresh_vms {
my $proto = shift;
my $class = ref($proto) || $proto;
my $args = _check_args('refresh_vms', @_ );
my $self = {};
bless($self, $class);
return _new_request($self
, command => 'refresh_vms'
, args => $args
);
}
sub enforce_limits {
my $proto = shift;
my $class = ref($proto) || $proto;
my $args = _check_args('refresh_vms', @_ );
my $self = {};
bless($self, $class);
return _new_request($self
, command => 'enforce_limits'
, args => $args
);
}
sub AUTOLOAD {
my $self = shift;
......
......@@ -443,7 +443,7 @@ sub test_domain_limit {
is(rvd_back->list_domains(user => user_admin , active => 1),1);
$domain2->start( user_admin );
rvd_back->enforce_limits(timeout => 2);
rvd_back->_cmd_enforce_limits(timeout => 3);
sleep 2;
rvd_back->_process_requests_dont_fork();
my @list = rvd_back->list_domains(user => user_admin , active => 1);
......@@ -481,7 +481,7 @@ sub test_domain_limit_already_requested {
my @list_requests = $domain->list_requests;
is(scalar @list_requests,0,"Expecting 0 requests ".Dumper(\@list_requests));
rvd_back->enforce_limits(timeout => 3);
rvd_back->_cmd_enforce_limits(timeout => 3);
if (!$domain->can_hybernate && $domain->is_active) {
......
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