Perl 如何捕获读取事件上的File::Tail异常
我正在尝试编写一个小进程,不断扫描日志路径。 所有旋转的文件都是.csv,而当前仍在写入的文件的扩展名是.csv.cur。 该过程应该几乎实时地查找.csv.cur文件,并对其内容执行操作。 简言之,它应该像尾巴一样工作——f |替换某些东西 这是主要代码:Perl 如何捕获读取事件上的File::Tail异常,perl,file,error-handling,fork,tail,Perl,File,Error Handling,Fork,Tail,我正在尝试编写一个小进程,不断扫描日志路径。 所有旋转的文件都是.csv,而当前仍在写入的文件的扩展名是.csv.cur。 该过程应该几乎实时地查找.csv.cur文件,并对其内容执行操作。 简言之,它应该像尾巴一样工作——f |替换某些东西 这是主要代码: for (;;) { opendir(my $cdrs, $path) || die $!; my @current = grep { /^$host.+\.cur$/ && -f "$path/$_" }
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() {
$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.
为了生成它,我要做的是在扫描过程中删除.csv.cur文件。
当然,为了测试代码,我做了如此愚蠢的事情,但我真的无法想出一个解决方案来处理这个错误
如果你需要更多信息,请告诉我 据我所知,这个错误正在按照您可能希望的方式工作。子级不再能够处理其分配的文件,因此它会自然地被清除,而父级则会继续查找新文件 如果您想更优雅地处理这些类型的错误条件,可以将代码包装在一个文件中,并在发生错误时进行一些清理和更详细的日志记录。您可以使用类似的模块来获取稍微语义化的错误捕获方式
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。非常感谢您的回答。我可能会尝试eval解决方案,因为由于可移植性的需要,我不能使用外部模块。我希望找到一个更优雅的解决方法来处理这个问题,因为eval在我看来是最后的解决方案,但到目前为止,这是我所知道的解决这个问题的唯一方法。我同意,这不是最漂亮的错误检查形式,这就是为什么有相当多的cpan模块试图模仿其他语言的行为。然而,这正是perl所拥有的,所以不妨接受它。如果这是您决定的解决方案,请随意将其标记为此问题的答案。祝你好运
eval {
my $file = File::Tail->new(...);
while (defined(my $line = $file->read)) {
...
}
};
if ($@) {
warn "Error processing $filename: $@";
# Any additional cleanup here
}