Reputation: 61
I'm trying to run mojolicious as a Windows Service using Win32::Daemon, but I don't know how to return from the start callback after starting the mojo app. The mojo app begins to listen but the Windows Service Controller assumes the start failed because you never reach the return statement.
sub Callback_Start
{
my( $Event, $Context ) = @_;
app->start; # <-- code hangs here
$Context->{last_state} = SERVICE_RUNNING;
Win32::Daemon::State( SERVICE_RUNNING );
return();
}
Is it possible to start the Mojo server in a non-blocking way?
Upvotes: 3
Views: 1411
Reputation: 61
This is what I have finally done:
my $daemon = Mojo::Server::Daemon->new( app => app, listen => ['http://*:3000' ] );
$daemon->prepare_ioloop;
Win32::Daemon::StartService( \%context, 100 );
Win32::Daemon::RegisterCallbacks({
start => \&_start,
running => \&_running,
stop => \&_stop,
pause => \&_pause,
continue => \&_continue,
});
# ...
sub _running {
my( $Event, $context ) = @_;
if( SERVICE_RUNNING == Win32::Daemon::State() ) {
$daemon->ioloop->one_tick;
}
}
sub _start {
my ($event, $context ) = @_;
$context->{last_state} = SERVICE_RUNNING;
$context->{last_event} = $event;
Win32::Daemon::State( SERVICE_RUNNING );
return();
}
# ...
Calling the one_tick method repeteadly allows you to embed the Mojo server (see the doc). With the code above Windows will call the _running sub every 100 milliseconds (second StartService parameter).
Upvotes: 2
Reputation: 902
What if you'd for a process, run the web app in the child and in the parent let the service controller know everything's running fine. I'm curios about how you'd stop the service in this case :)
Upvotes: 0