Skip to content

Instantly share code, notes, and snippets.

@Likk
Last active March 30, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Likk/6717ff092fb2e2cd418c5316b2e2c4ac to your computer and use it in GitHub Desktop.
Save Likk/6717ff092fb2e2cd418c5316b2e2c4ac to your computer and use it in GitHub Desktop.
子プロセス無いで死ぬとその後継続しない
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';
}
}
@Likk
Copy link
Author

Likk commented Mar 27, 2017

callback 書いた側の責任だとはおもうけど、
こんな感じで簡単に継続しなくなるので、
https://github.com/duck8823/Slack-RTM-Bot/blob/master/lib/Slack/RTM/Bot/Client.pm#L151

        eval {
            $action->{routine}->($response);
        };
        if($@){ warn '$@' #die を握りつぶすのもうどうなのという気持ちちょっとある }

いれると安全そう

@Likk
Copy link
Author

Likk commented Mar 27, 2017

ちなみに sleep 60; を短くしすぎると slack に 'You are sending too many requests. Please relax.' と言われる。

@Likk
Copy link
Author

Likk commented Mar 30, 2017

別案

        eval {
            $action->{routine}->($response);
        };
        if($@){
            warn '$@';
            exit(1); 
        }

@Likk
Copy link
Author

Likk commented Mar 30, 2017

ちなみにサンプルコードだと、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