Skip to content

Commit

Permalink
Merge pull request os-autoinst#2194 from kalikiana/backend_class
Browse files Browse the repository at this point in the history
Move backend process logic into new class
  • Loading branch information
mergify[bot] authored Nov 2, 2022
2 parents 89e7afa + f73f10d commit 1bb28ec
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set(PERL_FILES
OpenQA/Benchmark/Stopwatch.pm
OpenQA/Commands.pm
OpenQA/Exceptions.pm
OpenQA/Isotovideo/Backend.pm
OpenQA/Isotovideo/CommandHandler.pm
OpenQA/Isotovideo/Interface.pm
OpenQA/Isotovideo/NeedleDownloader.pm
Expand Down
33 changes: 33 additions & 0 deletions OpenQA/Isotovideo/Backend.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

package OpenQA::Isotovideo::Backend;
use Mojo::Base -base, -signatures;

use Mojo::File qw(path);
use backend::driver;
use bmwqemu;
use log qw(diag);

sub new ($class, @args) {
$bmwqemu::vars{BACKEND} ||= "qemu";
$bmwqemu::backend = backend::driver->new($bmwqemu::vars{BACKEND});

path('os-autoinst.pid')->spurt("$$");
# might throw an exception
$bmwqemu::backend->start_vm;
$class->SUPER::new(@args);
}

sub process ($self) {
$bmwqemu::backend->{backend_process};
}

sub stop ($self) {
return undef unless my $process = $self->process;
diag('stopping backend process ' . $process->pid);
$process->stop if $process->is_running;
diag('done with backend process');
}

1;
2 changes: 1 addition & 1 deletion OpenQA/Isotovideo/NeedleDownloader.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 SUSE LLC
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

package OpenQA::Isotovideo::NeedleDownloader;
Expand Down
40 changes: 10 additions & 30 deletions isotovideo
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use JSON::PP;

my $installprefix; # $bmwqemu::scriptdir
my $fatal_error; # the last error message caught by the die handler
my $backend;

BEGIN {
# the following line is modified during make install
Expand All @@ -65,7 +66,6 @@ BEGIN {
unshift @INC, "$installprefix";
}

use backend::driver;
use log qw(diag fctwarn);
use needle;
use autotest ();
Expand All @@ -78,11 +78,12 @@ use POSIX qw(:sys_wait_h _exit);
use Time::HiRes qw(gettimeofday tv_interval sleep time);
use Try::Tiny;
use IO::Select;
use Mojo::File qw(curfile path);
use Mojo::File qw(curfile);
use Mojo::UserAgent;
use Mojo::IOLoop::ReadWriteProcess 'process';
use Mojo::IOLoop::ReadWriteProcess::Session 'session';
Getopt::Long::Configure("no_ignore_case");
use OpenQA::Isotovideo::Backend;
use OpenQA::Isotovideo::CommandHandler;
use OpenQA::Isotovideo::Interface;
use OpenQA::Isotovideo::Utils qw(git_rev_parse checkout_git_repo_and_branch
Expand Down Expand Up @@ -155,7 +156,6 @@ my $command_handler;
my $testprocess;
my $cmd_srv_fd;
my $cmd_srv_port;
my $backend_process;
my $loop = 1;

# note: The subsequently defined stop_* functions are used to tear down the process tree.
Expand Down Expand Up @@ -200,30 +200,15 @@ sub stop_autotest () {
diag('done with autotest process');
}

sub stop_backend () {
return unless defined $bmwqemu::backend && $backend_process;

diag('stopping backend process ' . $backend_process->pid);
$backend_process->stop if $backend_process->is_running;
$backend_process = undef;
diag('done with backend process');
}

sub signalhandler ($sig) {
bmwqemu::serialize_state(component => 'isotovideo', msg => "isotovideo received signal $sig", log => 1);
return $loop = 0 if $loop;
stop_backend;
$backend->stop if defined $backend;
stop_commands("received signal $sig");
stop_autotest;
_exit(1);
}

sub init_backend () {
$bmwqemu::vars{BACKEND} ||= "qemu";
$bmwqemu::backend = backend::driver->new($bmwqemu::vars{BACKEND});
return $bmwqemu::backend;
}

$SIG{TERM} = \&signalhandler;
$SIG{INT} = \&signalhandler;
$SIG{HUP} = \&signalhandler;
Expand Down Expand Up @@ -273,32 +258,27 @@ bmwqemu::save_vars();
my $testfd;
($testprocess, $testfd) = autotest::start_process();

init_backend();
path('os-autoinst.pid')->spurt("$$");

# might throw an exception
$bmwqemu::backend->start_vm;
$backend = OpenQA::Isotovideo::Backend->new;

spawn_debuggers;

$backend_process = $bmwqemu::backend->{backend_process};
my $io_select = IO::Select->new();
$io_select->add($testfd);
$io_select->add($cmd_srv_fd);
$io_select->add($backend_process->channel_out);
$io_select->add($backend->process->channel_out);

# stop main loop as soon as one of the child processes terminates
my $stop_loop = sub (@) { $loop = 0 if $loop; };
$testprocess->once(collected => $stop_loop);
$backend_process->once(collected => $stop_loop);
$backend->process->once(collected => $stop_loop);
$cmd_srv_process->once(collected => $stop_loop);

# now we have everything, give the tests a go
$testfd->write("GO\n");

$command_handler = OpenQA::Isotovideo::CommandHandler->new(
cmd_srv_fd => $cmd_srv_fd,
backend_fd => $backend_process->channel_in,
backend_fd => $backend->process->channel_in,
);
$command_handler->on(tests_done => sub (@) {
CORE::close($testfd);
Expand Down Expand Up @@ -353,7 +333,7 @@ while ($loop) {
$loop = 0;
last;
}
if ($readable == $backend_process->channel_out) {
if ($readable == $backend->process->channel_out) {
$command_handler->send_to_backend_requester({ret => $rsp->{rsp}});
next;
}
Expand Down Expand Up @@ -404,7 +384,7 @@ $return_code = handle_generated_assets($command_handler, $clean_shutdown) unless
$fatal_error = undef;

END {
stop_backend;
$backend->stop if defined $backend;
stop_commands('test execution ended through exception');
stop_autotest;

Expand Down

0 comments on commit 1bb28ec

Please sign in to comment.