4

我正在开发一个基于简单Mojolicious::Lite的服务器,其中包括一个 websocket 端点。

我想处理一些终止信号以优雅地终止 websocket 连接并避免客户端(java 应用程序)中的异常。

我试图定义我的信号处理程序,就像我以前的服务器使用HTTP::Daemon. 问题是它们似乎被忽略了。也许在 Mojolicious 层中重新定义了,我还没有找到任何关于它的参考。

我期待看到我的终止消息,但它没有发生

[Mon Mar 23 14:01:28 2020] [info] Listening at "http://*:3000"
Server available at http://127.0.0.1:3000
^C  # <-- i want to see my signal received message here if type Ctrl-c

当服务器在终端中处于前台时,我SIGINT通过输入直接发送Ctrl-C,并且我可以优雅地终止服务器(例如,当由 cron 或其他无显示方式启动时)带有kill <pid>.

在以前的一些服务器中,我试图通过处理来非常详尽:

  • HUP现在用来重新加载配置的劫持信号
  • SIGINTCtrl-C
  • SIGQUITCtrl-\
  • SIGABRT例如异常库终止
  • SIGTERM外部终止请求 - “友好” kill(通过野蛮的反对kill -9
  • TSTP使用 Ctrl-Z 暂停
  • CONT从 Ctrl-Z 恢复时使用fgorbg

所有这些处理程序都允许在清理资源的情况下优雅地退出,确保数据一致性或在外部更改后重新加载配置或数据模型,具体取决于程序和需求。

我找到了Mojo::IOLoop::Signal“非阻塞信号处理程序”包,但它似乎是另一回事。错误的?

这是我的简化代码(使用 simple 运行perl ws_store_test.pl daemon):

文件 ws_store_test.pl

# Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
use Mojolicious::Lite;

my $store = {};
my $ws_clients = {};

sub terminate_clients {
    for my $peer (keys %$ws_clients){
        $ws_clients->{$peer}->finish;
    }
}

$SIG{INT} = sub {
    say "SIGINT";  # to be sure to display something
    app->log->info("SIGINT / CTRL-C received. Leaving...");
    terminate_clients;
};
$SIG{TERM} = sub {
    say "SIGTERM"; # to be sure to display something
    app->log->info("SIGTERM - External termination request. Leaving...");
    terminate_clients;
};

# this simulates a change on datamodel and notifies the clients
sub update_store {
    my $t = localtime time;
    $store->{last_time} = $t;
    for my $peer (keys %$ws_clients){
        app->log->debug(sprintf 'notify %s', $peer);
        $ws_clients->{$peer}->send({ json => $store
                                       });
    }
}

# Route with placeholder - to test datamodel contents
get '/:foo' => sub {
  my $c   = shift;
  my $foo = $c->param('foo');
  $store->{$foo}++;
  $c->render(text => "Hello from $foo." . (scalar keys %$store ? " already received " . join ', ', sort keys %$store : "") );
};

# websocket service with optional parameter
websocket '/ws/tickets/*id' => { id => undef } => sub {
    my $ws = shift;
    my $id = $ws->param('id');

    my $peer = sprintf '%s', $ws->tx;
    app->log->debug(sprintf 'Client connected: %s, id=%s', $peer, $id);
    $ws_clients->{$peer} = $ws->tx;
    $store->{$id} = {};

    $ws->on( message => sub {
        my ($c, $message) = @_;
        app->log->debug(sprintf 'WS received %s from a client', $message);
             });

    $ws->on( finish => sub {
        my ($c, $code, $reason) = @_;
        app->log->debug(sprintf 'WS client disconnected: %s - %d - %s', $peer, $code, $reason);
        delete $ws_clients->{$peer};
             });
};

plugin Cron => ( '* * * * *' => \&update_store );

# Start the Mojolicious command system
app->start;
4

1 回答 1

4

SIGINT 和 SIGTERM 处理程序在服务器启动时重新定义。在morbo 中,这是:

local $SIG{INT} = local $SIG{TERM} = sub {
  $self->{finished} = 1;
  kill 'TERM', $self->{worker} if $self->{worker};
};

Mojo::Server::Daemon中,这是:

local $SIG{INT} = local $SIG{TERM} = sub { $loop->stop };

如果您自己在顶层重新定义 SIGINT/SIGTERM 的处理程序,这些处理程序local将覆盖它们。相反,我建议将它们重新定义一次before_dispatch。例如:

sub add_sigint_handler {
    my $old_int = $SIG{INT};
    $SIG{INT} = sub {
        say "SIGINT";  # to be sure to display something
        app->log->info("SIGINT / CTRL-C received. Leaving...");
        terminate_clients;
        $old_int->(); # Calling the old handler to cleanly exit the server
    }
}

app->hook(before_dispatch => sub {
    state $unused = add_sigint_handler();
});

在这里,我state用来确保add_sigint_handler只评估一次(因为如果多次评估,$old_int第一次之后不会有正确的值)。另一种写作方式可能是:

my $flag = 0;
app->hook(before_dispatch => sub {
    if ($flag == 0) {
        add_sigint_handler();
        $flag = 1;
    }
});

或者,

app->hook(before_dispatch => sub {
    state $flag = 0;
    if ($flag == 0) {
        add_sigint_handler();
        $flag = 1;
    }
});
于 2020-03-24T11:13:41.283 回答