Mojo::Server::DaemonControl - A Mojolicious daemon manager
# Start the manager
$ mojodctl -l 'http://*:8080' -P /tmp/myapp.pid -w 4 /path/to/myapp.pl;
# Reload the manager
$ mojodctl -R -P /tmp/myapp.pid /path/to/myapp.pl;
# For more options
$ mojodctl --help
use Mojo::Server::DaemonControl;
my $dctl = Mojo::Server::DaemonControl->new(listen => ['http://*:8080'], workers => 4);
$dctl->run('/path/to/my-mojo-app.pl');
It is possible to use the "before_server_start" in Mojolicious hook to change
server settings. The $app
is also available, meaning the values can be read
from a config file. See Mojo::Server::DaemonControl::Worker and
Mojo::Server::Daemon for more information about what to tweak.
use Mojolicious::Lite -signatures;
app->hook(before_server_start => sub ($server, $app) {
if ($sever->isa('Mojo::Server::DaemonControl::Worker')) {
$server->inactivity_timeout(60);
$server->max_clients(100);
$server->max_requests(10);
}
});
Mojo::Server::DaemonControl is not a web server. Instead it manages one or more Mojo::Server::Daemon processes that can handle web requests.
This server is an alternative to Mojo::Server::Hypnotoad where each of the workers handle long running (WebSocket) requests. The main difference is that a hot deploy will simply start new workers, instead of restarting the manager. This is useful if you need/want to deploy a new version of your server during the "graceful_timeout". Normally this is not something you would need, but in some cases where the graceful timeout and long running requests last for several hours or even days, then it might come in handy to let the old code run, while new processes are deployed.
Note that Mojo::Server::DaemonControl is currently EXPERIMENTAL and it has not been tested in production yet. Feedback is more than welcome.
Shut down server immediately.
Shut down server gracefully.
Increase worker pool by one.
Decrease worker pool by one.
Will prevent existing workers from accepting new connections and eventually stop them, and start new workers in a fresh environment that handles the new connections. The manager process will remain the same.
$ mojodctl
|- myapp.pl-1647405707
|- myapp.pl-1647405707
|- myapp.pl-1647405707
|- myapp.pl
|- myapp.pl
'- myapp.pl
EXPERIMENTAL: The workers that waits to be stopped will have a timestamp
appended to $0
to illustrate which is new and which is old.
Mojo::Server::DaemonControl inherits all attributes from Mojo::EventEmitter and implements the following ones.
$bool = $dctl->cleanup;
$dctl = $dctl->cleanup(1);
Set this to true and Mojo::Server::DaemonControl will remove "pid_file" when this object goes out of scope.
$timeout = $dctl->graceful_timeout;
$dctl = $dctl->graceful_timeout(120);
A worker will be forced stopped if it could not be gracefully stopped after this amount of time.
$num = $dctl->heartbeat_interval;
$dctl = $dctl->heartbeat_interval(5);
Heartbeat interval in seconds. This value is passed on to "heartbeat_interval" in Mojo::Server::DaemonControl::Worker.
$num = $dctl->heartbeat_timeout;
$dctl = $dctl->heartbeat_timeout(120);
A worker will be stopped gracefully if a heartbeat has not been seen within this amount of time.
$array_ref = $dctl->listen;
$dctl = $dctl->listen(['http://127.0.0.1:3000']);
Array reference with one or more locations to listen on. See "listen" in Mojo::Server::Daemon for more details.
$log = $dctl->log;
$dctl = $dctl->log(Mojo::Log->new);
A Mojo::Log object used for logging.
$file = $dctl->pid_file;
$dctl = $dctl->pid_file(Mojo::File->new);
A Mojo::File object with the path to the pid file.
Note that the PID file must end with ".pid"! Default path is "mojodctl.pid" in "tmpdir" in File::Spec.
$int = $dctl->workers;
$dctl = $dctl->workers(4);
Number of worker processes, defaults to 4. See "workers" in Mojo::Server::Prefork for more details.
Mojo::Server::DaemonControl inherits all methods from Mojo::EventEmitter and implements the following ones.
$int = $dctl->check_pid;
Returns the PID of the running process documented in "pid_file" or zero (0) if it is not running.
$dctl->ensure_pid_file($pid);
Makes sure "pid_file" exists and contains the current PID.
$int = $dctl->reload($app);
Tries to reload a running instance by sending "USR2" to "pid_file".
$int = $dctl->run($app);
Run the menager and wait for "SIGNALS". Note that $app
is not loaded in
the manager process, which means that each worker does not share any code or
memory.
$dctl->stop($signal);
Used to stop the running manager and any "workers" with the $signal
INT,
QUIT or TERM (default).
Jan Henning Thorsen
Copyright (C) Jan Henning Thorsen
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
Mojo::Server::Daemon, Mojo::Server::Hypnotoad, Mojo::Server::DaemonControl::Worker.