簡體   English   中英

如何在讀取事件時捕獲File :: Tail異常

[英]How to catch File::Tail exception on read event

我正在嘗試編寫一個不斷掃描日志路徑的小進程。 所有旋轉的文件均為“ .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文件。 當然,為了測試代碼,我正在做這種愚蠢的事情,但是我真的無法提出解決此錯誤的解決方案。

如果您需要更多信息,請與我們聯系。

據我所知,該錯誤正在按您的期望進行。 孩子不再能夠處理分配給它的文件,所以它自然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]浮動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM