NAME

UniEvent::FsPoll - periodicall poll for changes in a file metadata

SYNOPSIS

use UniEvent::Fs; # import constants

my $loop = UniEvent::Loop->default_loop;
my $h = UniEvent::FsPoll->new($loop);
my $file = '/path/to/file';
$h->start($file, 5.5); # check every 5.5 seconds
$h->poll_callback(sub { # periodical check
    my ($handle, $prev, $curr, $error_code) = @_;
    say "The contents of $file has been modified"
         if (!$error_code && $curr->[STAT_MTIME] != $prev->[STAT_MTIME]);
}));
$h->start_callback(sub {
    my ($handle, $init, $error_code) = @_;
    # do something on the initial call with $init file stats
});
$l->run;

DESCRIPTION

This handle is used to periodically poll filesystem for the specified file metadata (i.e. change time, file size etc.) and invoke the user-specified callback with the current and previous metadata (file stats).

The UniEvent::FsPoll is inherited from UniEvent::Handle.

METHODS

new([$loop = UniEvent::Loop->default_loop])

Constructs new FsPoll handle and binds it to the specified event loop.

start($path [, $interval = 1.0 [, $callback]])

Stars monitoring the specified $path for changes. Each filesystem poll would occur every $interval second(s).

The optional start $callback can be set to invoke it upon the first invocation of the handle.

stop()

Stops monitoring.

poll_event()

event()

Returns XS::Framework::CallbackDispatcher instance, where callback(s) can be assigned with. The callback will be invoked upon timer event. The C++ interface is:

void(const FsPollSP&, const opt<Fs::FStat>& prev, const opt<Fs::FStat>& cur, const std::error_code&);

i.e. the callback returns nothing and as an argument takes FsPoll handle itself, optionally previous and current file stats and the error code.

Multiple callbacks can be added via the XS::Framework::CallbackDispatcher interface.

poll_callback($code)

callback($code)

Sets the callback, which will be invoked periodically on timer. All previously set event listeners or callbacks will be discarded. For the interface see event function.

start_event()

Returns XS::Framework::CallbackDispatcher instance, where callback(s) can be assigned with. The callback will be invoked once on handle start. The C++ interface is:

void(const FsPollSP&, const opt<Fs::FStat>& stat, const std::error_code&);

i.e. the callback returns nothing and as an argument takes FsPoll handle itself, optionally the current file stats and the error code.

Multiple callbacks can be added via the XS::Framework::CallbackDispatcher interface.

start_callback($code)

Sets the callback $code, which will be invoked once upon handle start. All previously set event listeners or callbacks will be discarded. For the interface see start_event function.

event_listener($delegate [, $weak = false])

Delegates callback invocation to the $delegate object of arbitrary class having on_fs_start and on_fs_poll methods. The object can optionally be weakened.

The method will be invoked as

$obj->on_fs_start($init, $error_code);
$obj->on_fs_poll($prev, $curr, $error_code);

path()

Returns the currently monitored path.

REFERENCES

UniEvent::Handle

UniEvent::Fs

UniEvent::FsEvent

UniEvent::Handle

XS::Framework::CallbackDispatcher