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'; | |
} | |
} |
ちなみに sleep 60; を短くしすぎると slack に 'You are sending too many requests. Please relax.' と言われる。
別案
eval {
$action->{routine}->($response);
};
if($@){
warn '$@';
exit(1);
}
ちなみにサンプルコードだと、34行目のdie をはずしてcallbackが正常終了し、
$bot->stop_RTM();
が呼ばれた場合もプロセスがゾンビになって残り続ける。
while ループでインスタンスを作り直す度にゾンビが無限に増える。
Slack::RTM::Bot#stop_RTM の
kill 9, $child;
しているところを
kill 'TERM', $child;
waitpid($child, 0);
にすると安全に子プロセスを終了を親が回収できそう。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
callback 書いた側の責任だとはおもうけど、
こんな感じで簡単に継続しなくなるので、
https://github.com/duck8823/Slack-RTM-Bot/blob/master/lib/Slack/RTM/Bot/Client.pm#L151
に
いれると安全そう