package Coro::ProcessPool;

=head1 NAME

Coro::ProcessPool - an asynchronous process pool

=head1 SYNOPSIS

    use Coro::ProcessPool;

    my $pool = Coro::ProcessPool->new(
        max_procs => 4,
        max_reqs  => 100,
    );

    my $double = sub { $_[0] * 2 };

    # Process in sequence
    my %result;
    foreach my $i (1 .. 1000) {
        $result{$i} = $pool->process($double, [$i]);
    }

    # Process as a batch
    my @results = $pool->map($double, 1 .. 1000);

    # Defer waiting for result
    my %deferred = map { $_ => $pool->defer($double, [$_]) } 1 .. 1000);
    foreach my $i (keys %deferred) {
        print "$i = " . $deferred{$i}->() . "\n";
    }

    # Use a "task class", implementing 'new' and 'run'
    my $result = $pool->process('Task::Doubler', 21);

    $pool->shutdown;

=head1 DESCRIPTION

Processes tasks using a pool of external Perl processes.

=cut

use Moo;
use Types::Standard qw(-types);
use Carp;
use AnyEvent;
use Guard;
use Coro;
use Coro::AnyEvent qw(sleep);
use Coro::Channel;
use Coro::ProcessPool::Process;
use Coro::ProcessPool::Util;
use Coro::Semaphore;

our $VERSION = '0.20';

if ($^O eq 'MSWin32') {
    die 'MSWin32 is not supported';
}

=head1 ATTRIBUTES

=head2 max_procs

The maximum number of processes to run within the process pool. Defaults
to the number of CPUs on the ssytem.

=cut

has max_procs => (
    is      => 'ro',
    isa     => Int,
    default => sub { Coro::ProcessPool::Util::cpu_count() },
);

=head2 max_reqs

The maximum number of tasks a worker process may run before being terminated
and replaced with a fresh process. This is useful for tasks that might leak
memory over time.

=cut

has max_reqs => (
    is      => 'ro',
    isa     => Int,
    default => 0,
);

=head1 PRIVATE ATTRIBUTES

=head2 procs_lock

Semaphore used to control access to the worker processes. Starts incremented
to the number of processes (C<max_procs>).

=cut

has procs_lock => (
    is  => 'lazy',
    isa => InstanceOf['Coro::Semaphore'],
);

sub _build_procs_lock {
    my $self = shift;
    return Coro::Semaphore->new($self->max_procs);
}

=head2 num_procs

Running total of processes that are currently running.

=cut

has num_procs => (
    is      => 'rw',
    isa     => Int,
    default => 0,
);

=head2 procs

Array holding the L<Coro::Process> objects.

=cut

has procs => (
    is      => 'ro',
    isa     => ArrayRef[InstanceOf['Coro::Process']],
    default => sub { [] },
);

=head2 is_running

Boolean which signals to the instance that the C<shutdown> method has been
called.

=cut

has is_running => (
    is      => 'rw',
    isa     => Bool,
    default => 1,
);

=head1 METHODS

=cut

sub DEMOLISH {
    my $self = shift;
    $self->shutdown;
}

sub BUILD {
    my $self = shift;
    for (1 .. $self->max_procs) {
      $self->checkin_proc($self->start_proc);
    }
}

sub start_proc {
    my $self = shift;
    my $proc = Coro::ProcessPool::Process->new();
    ++$self->{num_procs};
    return $proc;
}

sub kill_proc {
    my ($self, $proc) = @_;
    $proc->shutdown;
    --$self->{num_procs};
}

sub checkin_proc {
    my ($self, $proc) = @_;

    if (!$self->is_running || ($self->max_reqs && $proc->messages_sent >= $self->max_reqs)) {
        $self->kill_proc($proc);
        unshift @{$self->procs}, $self->start_proc;
    } else {
        unshift @{$self->procs}, $proc;
    }
}

sub checkout_proc {
    my $self = shift;
    croak 'not running' unless $self->is_running;

    my $proc;

    # Start a new process if none are available and there are worker slots open
    if ($self->capacity == 0 && $self->num_procs < $self->max_procs) {
        $proc = $self->start_proc;
    } else {
        $proc = shift @{$self->procs};
    }

    return $proc;
}

=head2 capacity

Returns the number of free worker processes.

=cut

sub capacity {
    my $self = shift;
    return scalar(@{$self->procs});
}

=head2 shutdown

Shuts down all processes and resets state on the process pool. After calling
this method, the pool is effectively in a new state and may be used normally.

=cut

sub shutdown {
    my $self = shift;

    $self->is_running(0);

    my $count = $self->num_procs or return;
    for (1 .. $count) {
        my $proc = shift @{$self->procs};
        $proc->shutdown(5);
        --$self->{num_procs};
    }
}

=head2 process($f, $args)

Processes code ref C<$f> in a child process from the pool. If C<$args> is
provided, it is an array ref of arguments that will be passed to C<$f>. Returns
the result of calling $f->(@$args).

Alternately, C<$f> may be the name of a class implementing the methods C<new>
and C<run>, in which case the result is equivalent to calling
$f->new(@$args)->run(). Note that the include path for worker processes is
identical to that of the calling process.

This call will yield until the results become available. If all processes are
busy, this method will block until one becomes available. Processes are spawned
as needed, up to C<max_procs>, from this method. Also note that the use of
C<max_reqs> can cause this method to yield while a new process is spawned.

=cut

sub process {
    my ($self, $f, $args) = @_;
    my $guard = $self->procs_lock->guard;
    my $proc  = $self->checkout_proc;
    scope_guard { $self->checkin_proc($proc) };

    my $msgid = $proc->send($f, $args);
    return $proc->recv($msgid);
}

=head2 map($f, @args)

Applies C<$f> to each value in C<@args> in turn and returns a list of the
results. Although the order in which each argument is processed is not
guaranteed, the results are guaranteed to be in the same order as C<@args>,
even if the result of calling C<$f> returns a list itself (in which case, the
results of that calcuation is flattened into the list returned by C<map>.

=cut

sub map {
    my ($self, $f, @args) = @_;
    my @deferred = map { $self->defer($f, [$_]) } @args;
    return map { $_->() } @deferred;
}

=head2 defer($f, $args)

Similar to L<./process>, but returns immediately. The return value is a code
reference that, when called, returns the results of calling C<$f->(@$args)>.

    my $deferred = $pool->defer($coderef, [ $x, $y, $z ]);
    my $result   = $deferred->();

=cut

sub defer {
    my $self = shift;
    my $cv   = AnyEvent->condvar;

    async_pool {
        my ($self, $cv, @args) = @_;
        my $result = eval { $self->process(@args) };
        $cv->croak($@) if $@;
        $cv->send($result);
    } $self, $cv, @_;

    return sub { $cv->recv };
}

=head1 A NOTE ABOUT IMPORTS AND CLOSURES

Code refs are serialized using L<Storable> to pass them to the worker
processes. Once deserialized in the pool process, these functions can no
longer see the stack as it is in the parent process. Therefore, imports and
variables external to the function are unavailable.

Something like this will not work:

    use Foo;
    my $foo = Foo->new();

    my $result = $pool->process(sub {
        return $foo->bar; # $foo not found
    });

Nor will this:

    use Foo;
    my $result = $pool->process(sub {
        my $foo = Foo->new; # Foo not found
        return $foo->bar;
    });

The correct way to do this is to import from within the function:

    my $result = $pool->process(sub {
        require Foo;
        my $foo = Foo->new();
        return $foo->bar;
    });

...or to pass in external variables that are needed by the function:

    use Foo;
    my $foo = Foo->new();

    my $result = $pool->process(sub { $_[0]->bar }, [ $foo ]);

=head2 Use versus require

The C<use> pragma is run a compile time, whereas C<require> is evaluated at
runtime. Because of this, the use of C<use> in code passed directly to the
C<process> method can fail because the C<use> statement has already been
evaluated when the calling code was compiled.

This will not work:

    $pool->process(sub {
        use Foo;
        my $foo = Foo->new();
    });

This will work:

    $pool->process(sub {
        require Foo;
        my $foo = Foo->new();
    });

If C<use> is necessary (for example, to import a method or transform the
calling code via import), it is recommended to move the code into its own
module, which can then be called in the anonymous routine:

    package Bar;

    use Foo;

    sub dostuff {
        ...
    }

Then, in your caller:

    $pool->process(sub {
        require Bar;
        Bar::dostuff();
    });

=head2 If it's a problem...

Use the task class method if the loading requirements are causing headaches:

    my $result = $pool->process('Task::Class', [@args]);

=head1 COMPATIBILITY

C<Coro::ProcessPool> will likely break on Win32 due to missing support for
non-blocking file descriptors (Win32 can only call C<select> and C<poll> on
actual network sockets). Without rewriting this as a network server, which
would impact performance and be really annoying, it is likely this module will
not support Win32 in the near future.

The following modules will get you started if you wish to explore a synchronous
process pool on Windows:

=over

=item L<Win32::Process>

=item L<Win32::IPC>

=item L<Win32::Pipe>

=back

=head1 AUTHOR

Jeff Ober <jeffober@gmail.com>

=head1 LICENSE

BSD License

=cut

1;
