Commit 5c98b062 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

list of requests in json

parent f7005693
......@@ -5,6 +5,7 @@ use strict;
use Data::Dumper;
use DBIx::Connector;
use JSON::XS;
use Moose;
use YAML;
......@@ -374,11 +375,40 @@ sub process_requests {
my $sth = $CONNECTOR->dbh->prepare("SELECT id FROM requests WHERE status='requested'");
$sth->execute;
while (my ($id)= $sth->fetchrow) {
$self->_execute(Ravada::Request->open($id));
warn "Processing request $id";
my $req = Ravada::Request->open($id);
$self->_execute($req);
warn $req->status();
}
$sth->finish;
}
=head2 list_requests
Returns a list of ruquests : ( id , domain_name, status, error )
=cut
sub list_requests {
my $self = shift;
my $sth = $CONNECTOR->dbh->prepare("SELECT id, args, status, error "
." FROM requests "
." WHERE status <> 'done' "
);
$sth->execute;
my @reqs;
my ($id, $j_args, $status, $error);
$sth->bind_columns(\($id, $j_args, $status, $error));
while ( $sth->fetch) {
my $args = decode_json($j_args) if $j_args;
push @reqs,{ id => $id, status => $status, error => $error , name => $args->{name}};
}
$sth->finish;
return \@reqs;
}
sub _execute {
my $self = shift;
my $request = shift;
......
......@@ -263,6 +263,7 @@ sub status {
my $sth = $$CONNECTOR->dbh->prepare("UPDATE requests set status=? "
." WHERE id=?");
warn "$status";
$sth->execute($status, $self->{id});
$sth->finish;
return $status;
......
......@@ -127,6 +127,16 @@ get '/machine/shutdown/*.html' => sub {
return shutdown_machine($c);
};
get '/machine/prepare/*.html' => sub {
my $c = shift;
return prepare_machine($c);
};
get '/requests.json' => sub {
my $c = shift;
return list_requests($c);
};
###################################################
sub _logged_in {
......@@ -348,10 +358,12 @@ sub wait_request_done {
my ($c, $req) = @_;
for ( 1 .. $TIMEOUT ) {
warn $req->status;
warn "$_ ".$req->status;
last if $req->status eq 'done';
sleep 1;
}
$req->status("timeout")
if $req->status eq 'working';
return $req;
}
......@@ -431,6 +443,27 @@ sub shutdown_machine {
return quick_start($c);
}
sub prepare_machine {
my $c = shift;
return login($c) if !_logged_in($c);
my $domain = _search_requested_machine($c);
my $req = Ravada::Request->prepare_base(
$domain->name
);
$c->render(text => 'Base '.$domain->name." prepared.");
}
sub list_requests {
my $c = shift;
my $list_requests = $RAVADA->list_requests();
$c->render(json => $list_requests);
}
app->start;
__DATA__
......
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