return $LoopTimeout = $_[1] + 0;
}
-=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef ) >>
+=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef, $arg) >>
Add a timer to occur $seconds from now. $seconds may be fractional, but timers
are not guaranteed to fire at the exact time you ask for.
=cut
-sub add_timer ($$) {
- my ($secs, $coderef) = @_;
+sub add_timer ($$;$) {
+ my ($secs, $coderef, $arg) = @_;
my $fire_time = now() + $secs;
- my $timer = [$fire_time, $coderef];
+ my $timer = [$fire_time, $coderef, $arg];
if (!@Timers || $fire_time >= $Timers[-1][0]) {
push @Timers, $timer;
# Run expired timers
while (@Timers && $Timers[0][0] <= $now) {
my $to_run = shift(@Timers);
- $to_run->[1]->($now) if $to_run->[1];
+ $to_run->[1]->($to_run->[2]);
}
# timers may enqueue into nextq:
sub poll_once {
my ($self) = @_;
- sub {
- eval { $self->poll };
- warn "E: FakeInotify->poll: $@\n" if $@;
- PublicInbox::DS::add_timer($poll_intvl, poll_once($self));
- };
+ eval { $self->poll };
+ warn "E: FakeInotify->poll: $@\n" if $@;
+ PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $self);
}
sub new {
my $self = bless { watch => {} }, __PACKAGE__;
- PublicInbox::DS::add_timer($poll_intvl, poll_once($self));
+ PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $self);
$self;
}
}
}
-sub imap_fetch_fork ($$$) {
- my ($self, $intvl, $uris) = @_;
+sub imap_fetch_fork ($) { # DS::add_timer callback
+ my ($self, $intvl, $uris) = @{$_[0]};
return if $self->{quit};
watch_atfork_parent($self);
defined(my $pid = fork) or die "fork: $!";
PublicInbox::DS::dwaitpid($pid, \&imap_fetch_reap, $self);
}
-sub imap_fetch_cb ($$$) {
- my ($self, $intvl, $uris) = @_;
- sub { imap_fetch_fork($self, $intvl, $uris) };
-}
-
sub imap_fetch_reap { # PublicInbox::DS::dwaitpid callback
my ($self, $pid) = @_;
my $intvl_uris = delete $self->{poll_pids}->{$pid} or
map { $_->as_string."\n" } @$uris;
}
warn('I: will check ', $_->as_string, " in ${intvl}s\n") for @$uris;
- PublicInbox::DS::add_timer($intvl, imap_fetch_cb($self, $intvl, $uris));
+ PublicInbox::DS::add_timer($intvl, \&imap_fetch_fork,
+ [$self, $intvl, $uris]);
}
sub watch_imap_init ($) {
# poll all URIs for a given interval sequentially
while (my ($intvl, $uris) = each %$poll) {
- PublicInbox::DS::requeue(imap_fetch_cb($self, $intvl, $uris));
+ PublicInbox::DS::add_timer(0, \&imap_fetch_fork,
+ [$self, $intvl, $uris]);
}
}