如何在读取事件上捕获File :: Tail异常

时间:2014-03-22 19:50:43

标签: perl file error-handling fork tail

我正在尝试编写一个不断扫描日志路径的小进程。 所有旋转的文件都是" .csv",而当前仍然被写入的文件有" .csv.cur"作为其延伸。 这个过程应该继续关注"几乎"实时" .csv.cur"文件并对其内容执行操作。 为了缩短它,它应该像" tail -f |一样工作替换某些东西"。

所以这是主要代码:

for (;;) {

    opendir(my $cdrs, $path) || die $!;
    my @current = grep { /^$host.+\.cur$/ && -f "$path/$_" } readdir($cdrs); 
    closedir($cdrs);

    if (! $current[0]) {
        &debug(DEBUG_MODE, "[PARENT] No CURRENT file found in: $path/. Retrying in $current_lookup_retry second(s)...\n");
        sleep $current_lookup_retry;
        next;
    }

    if ((! $last_file) || ($last_file ne $current[0])) {
        $last_file = $current[0];
        my $c_pid = &spawn_guardian("$path/$current[0]");
        &debug(DEBUG_MODE, "[PARENT] Guardian spawned (Child PID $c_pid)\n");
        &debug(DEBUG_MODE, "[PARENT] CURRENT set to: $current[0]\n");
    }

    select(undef, undef, undef, $ms);
}

sub spawn_guardian:

sub spawn_guardian() {
    $SIG{CHLD} = "IGNORE"; # prevent defunct processes
    my $child_pid = fork();    

    if (!defined $child_pid) {
        die "[CHILD] Cannot fork: $!";
    } elsif ($child_pid == 0) {
        # no need to verify if the parent dies

        &debug(DEBUG_MODE, "[CHILD][$$] Spawning guardian for $_[0]...\n");
        my $file = File::Tail->new(name          => $_[0],
                                   #name_changes => \&lcount($line_count),
                                   interval      => 1, 
                                   maxinterval   => 5,
                                   adjustafter   => 1,
                                   #errmode      => 'die',
                                   resetafter    => 15,
                                   debug         => DEBUG_MODE);

        while (defined(my $line = $file->read)) {
            if ($line) { # Try to evaluate the content of $line 
                print "[CHILD][$$] $line";
            } else {  # Try to gracefully exit from this Child process  
                &grace($$, $_[0]);
            }
        }

        &debug(DEBUG_MODE, "[CHILD][$$] Exiting guardian for $_[0]...\n"); # this should never be executed anyway
        exit 0; # same as previous line
    }
    return $child_pid; # return PID to Parent process
} 

这是我从这段代码得到的输出:

[DEBUG][PARENT] No CURRENT file found in: /root/OLOShaper/. Retrying in 2 second(s)...
[DEBUG][PARENT] No CURRENT file found in: /root/OLOShaper/. Retrying in 2 second(s)...
[DEBUG][CHILD][8346] Spawning guardian for /root/OLOShaper/mcitti21-KCZsOUrfmlFNRDXk.csv.cur...
[DEBUG][PARENT] Guardian spawned (Child PID 8346)
[DEBUG][PARENT] CURRENT set to: mcitti21-KCZsOUrfmlFNRDXk.csv.cur
[CHILD][8346] <omitted content of the .csv.cur>
[CHILD][8346] <omitted content of the .csv.cur>
[CHILD][8346] <omitted content of the .csv.cur>
[DEBUG][PARENT] No CURRENT file found in: /root/OLOShaper/. Retrying in 2 second(s)...
Error opening /root/OLOShaper/mcitti21-KCZsOUrfmlFNRDXk.csv.cur: No such file or directory at ./OLOShaper line 91
[DEBUG][PARENT] No CURRENT file found in: /root/OLOShaper/. Retrying in 2 second(s)...
[DEBUG]Ctrl-C detected!
Graceful shutdown in progress for PID 8344...
[DEBUG]Resetting PID file...
[DEBUG]Removing PID file...
Service ./OLOShaper has been halted.

因此,您可以看到我收到此错误:

Error opening /root/OLOShaper/mcitti21-KCZsOUrfmlFNRDXk.csv.cur: No such file or directory at ./OLOShaper line 91

我在oder中做了什么来生成它,在进程扫描时删除.csv.cur文件。 当然,为了测试代码,我做了这样的愚蠢的事情,但我真的无法提出解决方案来处理这个错误。

如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

据我所知,这个错误正如你所希望的那样有效。孩子不再能够处理其指定的文件,因此它自然dies并在父母继续寻找新文件时被清理。

如果您想更优雅地处理这些类型的错误情况,可以将File::Tail代码包装在eval中,并在发生错误时进行一些清理和更详细的日志记录。您可以使用像Try::Tiny之类的模块来获取更多语义捕获错误的方法。

eval {
    my $file = File::Tail->new(...);

    while (defined(my $line = $file->read)) {
       ...
    }
};
if ($@) {
    warn "Error processing $filename: $@";
    # Any additional cleanup here
}

无论哪种方式,看起来你的代码都可以正常工作,但肯定会有更多完整的错误处理。

旁注

您的spawn_guardian传递了一个参数文件名。接下来,将其放在一个描述性变量中:my ($filename) = @_;这样,您的代码就会更加自我记录,而不是随机移动$_[0]