HTTPS SSH
NAME
    Zug::Monitor - run a coderef and monitor process size

VERSION
    Version 0.1

SYNOPSIS
        use strict;
        use warnings;
    
        use Zug::Monitor;
        use Time::HiRes qw(sleep);
    
        my $test = sub {
            my $max_count = 100000;
            my $count     = 0;
            my @strings;
            while ( $count <= $max_count ) {
                sleep 0.01;
                $count++;
                push @strings,
                  ( 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' x
                      1000 );
            }
        };
    
        my $soft_limit = 50;
        my $hard_limit = 60;
        Zug::Monitor::watch(
            $test,
            $soft_limit,
            $hard_limit,
            {
                on_hard_limit_exceeded => \&on_hard_limit,
                on_soft_limit_exceeded => \&on_soft_limit,
                on_error => \&debug,
                on_success => \&debug,
            },
            10
        );

        sub debug {
            my $message = join( ' ', @_ );
            print STDERR $message, "\n";
        }
    
        sub on_soft_limit {
            print STDERR "process size greater than the soft limit\n";
        }
    
        sub on_hard_limit {
            print STDERR "process size greater than the hard limit\n";
        }

DESCRIPTION
    Zug::Monitor::watch will take a coderef and it will execute it in a
    forked process and watch the size of the forked process. A soft limit
    and a hard limit should be provided: when each of them is crossed the
    corresponding callback is executed. You can provide a grace_count, that
    is how many iterations of the watcher can the child fork survive after
    crossing the hard limit.

SUBROUTINES/METHODS
  watch
    Arguments:

    job a coderef with the code to execute

    soft_limit
        in MB

    hard_limit
        in MB

    callbacks
        a hashref of coderefs; optional, some reasonable but basic defaults
        are provided

        on_soft_limit_exceeded
            a coderef to be executed when the soft limit is exceeded;
            arguments : pid, soft limit, process size

        on_hard_limit_exceeded
            a coderef to be executed when the hard limit is exceeded;
            arguments accepted: pid, hard limit, process size

        on_error
            a coderef to be executed if the job throws an exception

        on_success
            a coderef to be executed when the forked process finishes on its
            own and returns 0

        debug
            a coderef wich will do something with debug messages

    grace_count
        how many iterations can the process be allowed to go over the hard
        limit; when the grace count is exceeded a kill 9 $pid is done

AUTHOR
    Emil Perhinschi, "<emilper at gmail.com>"

BUGS
    currently using "ps" to get the process size; considering replacing it
    with BSD::Resource, Memory::Usage or Unix::Getrusage

    Please report any bugs or feature requests to
    <https://bitbucket.org/emilper/zug-monitor>

SUPPORT
    You can find documentation for this module with the perldoc command.

        perldoc Zug::Monitor

    You can also look for information at
    <https://bitbucket.org/emilper/zug-monitor>

SEE ALSO
    A similar project you might want to look at
    <https://github.com/pshved/timeout>

LICENSE AND COPYRIGHT
    Copyright 2015 Emil Perhinschi.

    This program is free software; you can redistribute it and/or modify it
    under the terms of either: the GNU General Public License as published
    by the Free Software Foundation; or the Artistic License.

    See <http://dev.perl.org/licenses/> for more information.