Last active
March 30, 2017 12:05
-
-
Save Likk/6717ff092fb2e2cd418c5316b2e2c4ac to your computer and use it in GitHub Desktop.
子プロセス無いで死ぬとその後継続しない
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use utf8; | |
use Encode; | |
use Slack::RTM::Bot; | |
while(1){ | |
my $bot = Slack::RTM::Bot->new( | |
token => 'XXXX', | |
options => +{ max_message_size => 20480 } | |
); | |
$bot->on( | |
+{ }, | |
\&main::display_tl, | |
); | |
warn 'start'; | |
$bot->start_RTM(); | |
sleep 60; | |
$bot->stop_RTM(); | |
warn 'stop'; | |
} | |
sub display_tl { | |
my ($response) = @_; | |
if($response->{text} && $response->{channel} && $response->{user}){ | |
printf("[%s] <%s%s> :%-10s\n", | |
$response->{ts}, | |
$response->{user}, | |
$response->{channel}, | |
Encode::encode_utf8($response->{text}), | |
); | |
die 'hoge'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ちなみにサンプルコードだと、34行目のdie をはずしてcallbackが正常終了し、
$bot->stop_RTM();
が呼ばれた場合もプロセスがゾンビになって残り続ける。
while ループでインスタンスを作り直す度にゾンビが無限に増える。
Slack::RTM::Bot#stop_RTM の
しているところを
にすると安全に子プロセスを終了を親が回収できそう。