gpt4 book ai didi

perl - 如何在读取事件中捕获 File::Tail 异常

转载 作者:行者123 更新时间:2023-12-03 08:00:19 25 4
gpt4 key购买 nike

我正在尝试编写一个持续扫描日志路径的小进程。
所有旋转的文件都是“.csv”,而当前仍在写入的文件的扩展名是“.csv.cur”。
该过程应继续“几乎”实时查找“.csv.cur”文件并对其内容执行操作。
简而言之,它应该像“tail -f | replace something”一样工作。

所以这是主要代码:

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);
}

和子 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

为了生成它,我所做的是在进程扫描它时删除 .csv.cur 文件。
当然,为了测试代码,我正在做这种愚蠢的事情,但我真的想不出一个解决方案来处理这个错误。

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

最佳答案

据我所知,该错误正在按您可能希望的方式工作。 child 不再能够处理其分配的文件,所以它自然 dies 并且在 parent 继续寻找新文件时被清理。

如果你想更优雅地处理这些类型的错误情况,你可以包装你的 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]的飘来飘去。

关于perl - 如何在读取事件中捕获 File::Tail 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22582719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com