Startup/Shutdown hook for worker processes

30 views
Skip to first unread message

Frank Brendel

unread,
Mar 27, 2020, 8:31:38 AM3/27/20
to Mojolicious
Hi,

my application needs to load Selenium::Chrome for every single worker process.
I made this via
Mojo::IOLoop->next_tick(sub {
      $Self->{Chrome} = Selenium::Chrome->new(
         extra_capabilities => {
            chromeOptions => {
               args => [
                  'headless', 'no-sandbox', 'window-size=1400,900',
                  'disk-cache-size=0', 'disable-gpu', 'v8-cache-options=off'
               ]
            }
         },
      );

But I have no idea where to shutdown the chrome binary.

I've tried it via the 
Mojo::IOLoop->on(finish

event but that stops the chromedriver to early so that outstanding Selenium requests fail.

Thanks
Frank

Dan Book

unread,
Mar 27, 2020, 11:41:32 AM3/27/20
to mojol...@googlegroups.com
You could try shutting it down in an END {} block. To do this you would need to store the instance to shut down by process ID so you know which one to shut down.

-Dan

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/58314c80-14ee-4cbe-8e2f-c7a5bebc7c0a%40googlegroups.com.

Frank Brendel

unread,
Mar 31, 2020, 3:57:57 AM3/31/20
to Mojolicious
Hi Dan,

thanks for the reply.
Unfortunately, the END block doesn't work.

But I've solved the problem as follows.
The application creates the Selenium::Chrome object via 'has' as described in the wiki.
has Chrome => sub {
   
my $Self = shift;
   
my $Chrome = Selenium::Chrome->new(

      extra_capabilities
=> {
         chromeOptions
=> {
            args
=> [
               
'headless', 'no-sandbox', 'window-size=1400,900',
               
'disk-cache-size=0', 'disable-gpu', 'v8-cache-options=off'
           
]
         
}
     
},

      error_handler
=> sub { $Self->Log("W", $_[1]); }
   
);
   
return $Chrome;
};

When the server shuts down it sets a flag and stops the Chrome webdriver.
$Self->hook(before_server_start => sub {
     
my ($Server, $App) = @_;
      $Self
->{Shutdown} = 0;
      $Server
->ioloop->on(finish => sub {
         $Self
->Log("I", "Shutdown Chrome");
         $Self
->{Shutdown} = 1;
         $Self
->Chrome->shutdown_binary;
     
});
   
});

and the controller returns immediately if $App->{Shutdown} is 1.

Nevertheless, this leads to minor synchronization problems when the server stops Chrome while the application controller is processing a request, but I can handle it.


Thanks,
Frank


Am Freitag, 27. März 2020 16:41:32 UTC+1 schrieb Dan Book:
You could try shutting it down in an END {} block. To do this you would need to store the instance to shut down by process ID so you know which one to shut down.

-Dan

On Fri, Mar 27, 2020 at 8:31 AM 'Frank Brendel' via Mojolicious <mojol...@googlegroups.com> wrote:
Hi,

my application needs to load Selenium::Chrome for every single worker process.
I made this via
Mojo::IOLoop->next_tick(sub {
      $Self->{Chrome} = Selenium::Chrome->new(
         extra_capabilities => {
            chromeOptions => {
               args => [
                  'headless', 'no-sandbox', 'window-size=1400,900',
                  'disk-cache-size=0', 'disable-gpu', 'v8-cache-options=off'
               ]
            }
         },
      );

But I have no idea where to shutdown the chrome binary.

I've tried it via the 
Mojo::IOLoop->on(finish

event but that stops the chromedriver to early so that outstanding Selenium requests fail.

Thanks
Frank

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojol...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages