Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sub get_enquiry_events {
- my $self = shift;
- my $enq_id = $self->stash->{enq_id};
- # Increase inactivity timeout for connection a bit
- $self->inactivity_timeout( 60 * 20 ); # seconds * minutes
- # Change content type and finalize response headers
- $self->res->headers->content_type('text/event-stream');
- $self->write;
- $self->write("event:enquiry\ndata: {\"status\":\"Start\",\"component\":\"trim\",\"action\":\"update\"}\n\n");
- my $loop_id = Mojo::IOLoop->recurring(
- 15 => sub {
- my $loop = shift;
- $self->write("event:enquiry\ndata: {\"status\":\"Ping\",\"component\":\"trim\"}\n\n");
- }
- );
- my $ua = Mojo::UserAgent->new( max_response_size => 0 ) # new UA
- ->request_timeout( 60 * 180 ); # seconds * minutes
- my $uri = sprintf('%s/%s', MyApp->config->{firewalled_url}, $enq_id);
- # Build a normal transaction
- my $tx = $ua->build_tx( GET => $uri );
- $tx->res->content->unsubscribe('read')->on(
- read => sub {
- my ( $content, $bytes ) = @_;
- MyApp->logger->info( "get_enquiry_events-bytes: " . np($bytes));
- $self->write($bytes);
- }
- );
- $tx->on(
- finish => sub {
- my $tx = shift;
- Mojo::IOLoop->remove($loop_id);
- $self->finish();
- }
- );
- $ua->start($tx => sub{
- my ($ua, $tx) = @_;
- MetaData::Trim->logger->info( "get_enquiry_events-callback for start");
- $self->finish();
- });
- # Start event loop if necessary
- Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement