错误时重新启动Perl脚本? (Perl新手)

时间:2011-10-10 22:00:22

标签: perl error-handling twitter-streaming-api

我正在使用从CPAN获得的AnyEvent :: Twitter :: Stream模块。我也是Perl的新手,我从示例中修改过的这个脚本触发了一个事件,一旦根据某些条件从Twitter流API中读取了一条推文,就会将一些Twitter数据插入到数据库中。我已经编写了一些代码,但一般信息如下:

#!/usr/bin/perl
    use strict;
    use AnyEvent::Twitter::Stream;

    use DBI;
    use DBD::mysql;



    my $done = AnyEvent->condvar;

    my($user, $password, $method, $args) = @ARGV;
    # my twitter login
    my %args;
    if ($method eq 'follow') {
        $args{follow} = $args;
        $method = 'filter';
    } elsif ($method eq 'track') {
        $args{track}  = $args;
        $method = 'filter';
    }

    binmode STDOUT, ":utf8";

    my $streamer = AnyEvent::Twitter::Stream->new(
        username => $user,
        password => $password,
        timeout => 0,
        method   => $method || "sample",
        %args,
        on_keepalive => sub {
              warn "ping\n";
          },
        on_tweet => sub {

            my $DBIconnect;
            #db connection info

            my $dsn = "dbi:mysql:$database:$host:$dbport";

            $DBIconnect = DBI->connect($dsn, $db_user, $db_pass);

            my $tweet = shift;

            if($tweet->{id}) {
               # occasionally twitter 
               # insert some data here

                my $save_tweet = $query;
                #print $query;
                my $save_handle = $DBIconnect->prepare($save_tweet);

                if($save_handle->execute()) { 
                    #print "Tweet #" . $tweet->{id} . " Stored\n";
                } else { 
                    #print "Error Saving Tweet #" . $tweet->{id} . "\n";
                }
                $save_handle->finish();
                $DBIconnect->disconnect();
            } else { 
                #print "Tweet did not contain Tweet ID.\n";
            }

        },
        on_error => sub {
            my $error = shift;
            print "Error! Error!";
            warn "ERROR: $error";


            $done->send;
            sleep 2;

            $done->recv;
        },
        on_eof   => sub {
            $done->send;
        },
    );


    $done->recv;

在此过程中,流或应用程序偶尔会因为仍然让我神秘的原因而退出。我想等待3秒钟,然后重新启动整个perl脚本(或简称AnyEvent实例)继续读取数据。

你可能会注意到我尝试了这个:

    on_error => sub {
        my $error = shift;
        print "Error! Error!";
        warn "ERROR: $error";


        $done->send;
        sleep 2;

        $done->recv;
    },

这似乎没有重启过程。任何人都可以向我提供任何有关这将采取什么的见解?

1 个答案:

答案 0 :(得分:2)

我不熟悉这个模块,但是你需要更清楚“流还是应用程序因为仍然让我神秘的原因而退出”的意思。脚本会死吗?

如果是这样,捕获错误并重新启动的一种方法是将整个代码包装在eval中,如下所示:

my $continue = 1;
while ($continue) {
   eval {  # Catch "die" event
       my $streamer = xxx
       # some other call to start the streamer???
   };
   if ($@ =~ /specific error message substring you wish to restart on/) {
       # Do some prep work if needed
   } else {
       $continue = 0; # quit on next loop iteration
       # As alternative to using $continue flag, just do:
       # last; 
   }
}