diff --git a/gorgone/gorgone/modules/core/cron/class.pm b/gorgone/gorgone/modules/core/cron/class.pm index 21c7078faac..275760b88f7 100644 --- a/gorgone/gorgone/modules/core/cron/class.pm +++ b/gorgone/gorgone/modules/core/cron/class.pm @@ -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 { @@ -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 { diff --git a/gorgone/gorgone/modules/core/httpserver/class.pm b/gorgone/gorgone/modules/core/httpserver/class.pm index 74461ead10a..132218339a4 100644 --- a/gorgone/gorgone/modules/core/httpserver/class.pm +++ b/gorgone/gorgone/modules/core/httpserver/class.pm @@ -67,7 +67,9 @@ sub new { $connector->{allowed_hosts_enabled} = 0; } - $connector->set_signal_handlers; + $connector->{tokens} = {}; + + $connector->set_signal_handlers(); return $connector; } @@ -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) = @_; @@ -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(); @@ -208,7 +238,7 @@ sub run { } if (!defined($connection)) { - gorgone::standard::api::event(httpserver => $self); + $self->event(); next; } diff --git a/gorgone/gorgone/standard/api.pm b/gorgone/gorgone/standard/api.pm index 79c1499016d..a8ec6166271 100644 --- a/gorgone/gorgone/standard/api.pm +++ b/gorgone/gorgone/standard/api.pm @@ -28,7 +28,6 @@ use JSON::XS; my $module; my $socket; -my $results = {}; my $action_token; sub set_module { @@ -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\/(.*)$/) { @@ -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"}'; @@ -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"}'; @@ -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;