Skip to content

Commit

Permalink
fix(gorgone-httpserver): no response from the server (#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
garnier-quentin authored and Evan-Adam committed Jul 16, 2024
1 parent 64801a1 commit ef068c6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 43 deletions.
19 changes: 15 additions & 4 deletions gorgone/gorgone/modules/core/cron/class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ sub action_getcron {
code => $options{data}->{parameters}->{code}
}
});
my $watcher_timer = $self->{loop}->timer(5, 0, \&stop_ev);
$self->{loop}->run();

my $timeout = 5;
my $ctime = time();
while (1) {
my $watcher_timer = $self->{loop}->timer(1, 0, \&stop_ev);
$self->{loop}->run();
last if (time() > ($ctime + $timeout));
}

$data = $connector->{ack}->{data}->{data}->{result};
} else {
Expand Down Expand Up @@ -436,8 +442,13 @@ sub dispatcher {
json_encode => 1
});

my $watcher_timer = $options->{connector}->{loop}->timer(5, 0, \&stop_ev);
$options->{connector}->{loop}->run();
my $timeout = 5;
my $ctime = time();
while (1) {
my $watcher_timer = $options->{connector}->{loop}->timer(1, 0, \&stop_ev);
$options->{connector}->{loop}->run();
last if (time() > ($ctime + $timeout));
}
}

sub run {
Expand Down
36 changes: 33 additions & 3 deletions gorgone/gorgone/modules/core/httpserver/class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ sub new {
$connector->{allowed_hosts_enabled} = 0;
}

$connector->set_signal_handlers;
$connector->{tokens} = {};

$connector->set_signal_handlers();
return $connector;
}

Expand Down Expand Up @@ -144,6 +146,34 @@ sub stop_ev {
$connector->{loop}->break();
}

sub event {
my ($self, %options) = @_;

while ($self->{internal_socket}->has_pollin()) {
my ($message) = $self->read_message();
next if (!defined($message));

if ($message =~ /^\[(.*?)\]\s+\[([a-zA-Z0-9:\-_]*?)\]\s+\[.*?\]\s+(.*)$/m ||
$message =~ /^\[(.*?)\]\s+\[([a-zA-Z0-9:\-_]*?)\]\s+(.*)$/m) {
my ($action, $token, $data) = ($1, $2, $3);
$self->{tokens}->{$token} = {
action => $action,
token => $token,
data => $data
};
if ((my $method = $self->can('action_' . lc($action)))) {
my ($rv, $decoded) = $self->json_decode(argument => $data, token => $token);
next if ($rv);
$method->($self, token => $token, data => $decoded);
}
}
}

if (defined($self->{break_token}) && defined($self->{tokens}->{ $self->{break_token} })) {
$self->{loop}->break();
}
}

sub run {
my ($self, %options) = @_;

Expand All @@ -166,7 +196,7 @@ sub run {
gorgone::standard::api::set_module($self);

my $watcher_timer = $self->{loop}->timer(4, 0, \&stop_ev);
my $watcher_io = $self->{loop}->io($connector->{internal_socket}->get_fd(), EV::READ, \&gorgone::standard::api::event);
my $watcher_io = $self->{loop}->io($connector->{internal_socket}->get_fd(), EV::READ, sub { $connector->event() });
$self->{loop}->run();

$self->init_dispatch();
Expand Down Expand Up @@ -208,7 +238,7 @@ sub run {
}

if (!defined($connection)) {
gorgone::standard::api::event(httpserver => $self);
$self->event();
next;
}

Expand Down
63 changes: 27 additions & 36 deletions gorgone/gorgone/standard/api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use JSON::XS;

my $module;
my $socket;
my $results = {};
my $action_token;

sub set_module {
Expand All @@ -40,10 +39,9 @@ sub root {

$options{logger}->writeLogInfo("[api] Requesting '" . $options{uri} . "' [" . $options{method} . "]");

$action_token = undef;
$options{module}->{tokens} = {};
$socket = $options{socket};
$module = $options{module};
$results = {};

my $response;
if ($options{method} eq 'GET' && $options{uri} =~ /^\/api\/(nodes\/(\w*)\/)?log\/(.*)$/) {
Expand Down Expand Up @@ -149,14 +147,23 @@ sub call_internal {
json_encode => 1
});

my $watcher_timer = $options{module}->{loop}->timer(5, 0, \&stop_ev);
$options{module}->{loop}->run();
$options{module}->{break_token} = $action_token;

my $timeout = 5;
my $ctime = time();
while (1) {
my $watcher_timer = $options{module}->{loop}->timer(1, 0, \&stop_ev);
$options{module}->{loop}->run();
last if (time() > ($ctime + $timeout) || defined($options{module}->{tokens}->{$action_token}));
}

$options{module}->{break_token} = undef;

my $response = '{"error":"no_result", "message":"No result found for action \'' . $options{action} . '\'"}';
if (defined($results->{$action_token}->{data})) {
if (defined($options{module}->{tokens}->{$action_token}->{data})) {
my $content;
eval {
$content = JSON::XS->new->decode($results->{$action_token}->{data});
$content = JSON::XS->new->decode($options{module}->{tokens}->{$action_token}->{data});
};
if ($@) {
$response = '{"error":"decode_error","message":"Cannot decode response"}';
Expand Down Expand Up @@ -204,14 +211,23 @@ sub get_log {
json_encode => 1
});

my $watcher_timer = $options{module}->{loop}->timer(5, 0, \&stop_ev);
$options{module}->{loop}->run();
$options{module}->{break_token} = $token_log;

my $timeout = 5;
my $ctime = time();
while (1) {
my $watcher_timer = $options{module}->{loop}->timer(1, 0, \&stop_ev);
$options{module}->{loop}->run();
last if (time() > ($ctime + $timeout) || defined($options{module}->{tokens}->{$token_log}));
}

$options{module}->{break_token} = undef;

my $response = '{"error":"no_log","message":"No log found for token","data":[],"token":"' . $options{token} . '"}';
if (defined($results->{ $token_log }) && defined($results->{ $token_log }->{data})) {
if (defined($options{module}->{tokens}->{$token_log}) && defined($options{module}->{tokens}->{ $token_log }->{data})) {
my $content;
eval {
$content = JSON::XS->new->decode($results->{ $token_log }->{data});
$content = JSON::XS->new->decode($options{module}->{tokens}->{ $token_log }->{data});
};
if ($@) {
$response = '{"error":"decode_error","message":"Cannot decode response"}';
Expand All @@ -234,29 +250,4 @@ sub get_log {
return $response;
}

sub event {
my (%options) = @_;

my $httpserver = defined($options{httpserver}) ? $options{httpserver} : $module;
while ($httpserver->{internal_socket}->has_pollin()) {
my ($message) = $httpserver->read_message();
next if (!defined($message));

if ($message =~ /^\[(.*?)\]\s+\[([a-zA-Z0-9:\-_]*?)\]\s+\[.*?\]\s+(.*)$/m ||
$message =~ /^\[(.*?)\]\s+\[([a-zA-Z0-9:\-_]*?)\]\s+(.*)$/m) {
my ($action, $token, $data) = ($1, $2, $3);
$results->{$token} = {
action => $action,
token => $token,
data => $data
};
if ((my $method = $httpserver->can('action_' . lc($action)))) {
my ($rv, $decoded) = $httpserver->json_decode(argument => $data, token => $token);
next if ($rv);
$method->($httpserver, token => $token, data => $decoded);
}
}
}
}

1;

0 comments on commit ef068c6

Please sign in to comment.