Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Polling a file for changes - Best Practices?

by Tommy (Chaplain)
on Mar 04, 2020 at 21:36 UTC ( #11113786=perlquestion: print w/replies, xml ) Need Help??

Tommy has asked for the wisdom of the Perl Monks concerning the following question:

Looking for advice on how to best accomplish this in 2020. The last node I could find mentioning polling files was from 2004.

Rationale: I have a log file I want to tail out to the browser via Mojolicious websockets. I have a rudimentary solution in place where an XHR request is sent on a 10 second interval to the server (client pull) which then goes and checks for updates a la qx(tail -n50 $file) ... basically.

Having seen how awesome websockets can be, I want to use a sever-push model instead. The websockets logic is already in place... I just need to know how best to approach the file polling. Something like d/inotify maybe? Or stat() wrapped in a while loop (yuck)?

Thoughts??

Tommy
A mistake can be valuable or costly, depending on how faithfully you pursue correction

Replies are listed 'Best First'.
Re: Polling a file for changes - Best Practices?
by choroba (Cardinal) on Mar 04, 2020 at 22:31 UTC
    I've used inotifywait for file polling several times. Usually without any module, just
    open my $inotify, '-|', do { no warnings 'qw'; qw( inotifywait -m -r -e close_write -e moved_to --exclude /\ +.git/|~$|/#[^/]+# . ) } or die $!; while (<$inotify>) { my ($path, $event, $file) = split; $thread_queue->enqueue("$path$file") if $event =~ /$expected/; }

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Was there any reason why to depend on an external module/program? Did something like File::Tail not provide what you want?

        I don't think File::Tail can monitor a whole directory tree.
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      What linux package provides `inotifywait`. Looks like inotify-tools from sourceforge, but I can't find it in any typical repo?? I'm looking at a github repo right now, but this doesn't feel mainstream...

      Tommy
      A mistake can be valuable or costly, depending on how faithfully you pursue correction
        Yes, it comes from sourceforge, but openSUSE provides it as a rpm package.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        but this doesn't feel mainstream...

        There's a whole world of really good stuff which isn't "mainstream". Think for a moment how much of CPAN isn't in core. Does that stop you using those modules? Think how many Perl modules exist which are not provided as RedHat packages (that situation was really dire with RHEL5 and earlier and is only slightly better today). If you reach beyond the mainstream you will find a panoply of wondrous experiences.

        If you wanted something with a RedHat official seal of approval then this should have been stated up front in the OP. But there you already say you are using Mojolicious, websockets, etc. and your task doesn't sound particularly mainstream to me. Why the sudden aversion to downloading and installing inotifywait?

Re: Polling a file for changes - Best Practices?
by Your Mother (Archbishop) on Mar 05, 2020 at 00:12 UTC

    Caveat: I am not adept at this despite doing some lately. The main reason I succeeded was haukex’s help. That said, my first impression would be what you say, a websocket doing tail. I would read the the process from tail -F through a pipe—with one of the IPC:: modules probably but I’d have to read a few nodes to remember which is best here—and just loop through $x lines or bytes, throwing them away if there is no client, and sending the pipe stream output to any connected clients in close to real time.

    I am too lazy / pretend-busy tonight to try to hack something up for you, despite wanting a log tailer just like this for a couple of years, but here are some nice code jumping off points: Mixing asynchronous data feed with synchronous program flow control (replies) and tail file and websocket stream and Re: Architecture advice, proxy or rebroadcast websocket.

Re: Polling a file for changes - Best Practices?
by Anonymous Monk on Mar 04, 2020 at 23:01 UTC

      I thought about this approach, (hooking into log events), but it doesn't capture the output of warn() or die() ... only what gets sent to app->log->debug()

      Tommy
      A mistake can be valuable or costly, depending on how faithfully you pursue correction

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11113786]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2022-12-03 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?