]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ConfigIter.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / ConfigIter.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Intended for PublicInbox::DS->EventLoop in read-only daemons
5 # to avoid each_inbox() monopolizing the event loop when hundreds/thousands
6 # of inboxes are in play.
7 package PublicInbox::ConfigIter;
8 use strict;
9 use v5.10.1;
10
11 sub new {
12         my ($class, $pi_cfg, $cb, @args) = @_;
13         my $i = 0;
14         bless [ $pi_cfg, \$i, $cb, @args ], __PACKAGE__;
15 }
16
17 # for PublicInbox::DS::next_tick, we only call this is if
18 # PublicInbox::DS is already loaded
19 sub event_step {
20         my $self = shift;
21         my ($pi_cfg, $i, $cb, @arg) = @$self;
22         my $section = $pi_cfg->{-section_order}->[$$i++];
23         eval { $cb->($pi_cfg, $section, @arg) };
24         warn "E: $@ in ${self}::event_step" if $@;
25         PublicInbox::DS::requeue($self) if defined($section);
26 }
27
28 # for generic PSGI servers
29 sub each_section {
30         my $self = shift;
31         my ($pi_cfg, $i, $cb, @arg) = @$self;
32         while (defined(my $section = $pi_cfg->{-section_order}->[$$i++])) {
33                 eval { $cb->($pi_cfg, $section, @arg) };
34                 warn "E: $@ in ${self}::each_section" if $@;
35         }
36         eval { $cb->($pi_cfg, undef, @arg) };
37         warn "E: $@ in ${self}::each_section" if $@;
38 }
39
40 1;