| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Win32::ProcFarm::PerpetualPool - manages a pool of child processes for perpetual jobs | ||
| 4 | # | ||
| 5 | # Author: Toby Everett | ||
| 6 | # Revision: 2.15 | ||
| 7 | # Last Change: Patched to fix problems when Win32::GetTickCount > 2**31 | ||
| 8 | ############################################################################# | ||
| 9 | # Copyright 1999, 2000 Toby Everett. All rights reserved. | ||
| 10 | # | ||
| 11 | # This file is distributed under the Artistic License. See | ||
| 12 | # http://www.ActiveState.com/corporate/artistic_license.htm or | ||
| 13 | # the license that comes with your perl distribution. | ||
| 14 | # | ||
| 15 | # For comments, questions, bugs or general interest, feel free to | ||
| 16 | # contact Toby Everett at teverett@alascom.att.com | ||
| 17 | ############################################################################# | ||
| 18 | |||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Win32::ProcFarm::PerpetualPool - manages a pool of child processes for perpetual jobs | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | use Win32::ProcFarm::PerpetualPool; | ||
| 27 | |||
| 28 | $Pool = Win32::ProcFarm::PerpetualPool->new($poolsize, $portnum, $scriptname, Win32::GetCwd, | ||
| 29 | command => 'whatever', | ||
| 30 | list_check_intvl => 30, | ||
| 31 | exit_check_intvl => 5, | ||
| 32 | list_sub => sub { return ('Fred', 'Julie', 'Joe') }, | ||
| 33 | exit_sub => sub { return -e 'killme'; }, | ||
| 34 | result_sub => sub { print join(', ' @_)."\n"; }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | $Pool->start_pool(0.1); | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | =head2 Installation instructions | ||
| 42 | |||
| 43 | This installs with MakeMaker as part of Win32::ProcFarm. | ||
| 44 | |||
| 45 | To install via MakeMaker, it's the usual procedure - download from CPAN, | ||
| 46 | extract, type "perl Makefile.PL", "nmake" then "nmake install". Don't | ||
| 47 | do an "nmake test" because the I haven't written a test suite yet. | ||
| 48 | |||
| 49 | =head2 More usage instructions | ||
| 50 | |||
| 51 | This is a version of Win32::ProcFarm::Pool designed for continuous operation. You supply a single | ||
| 52 | command name and a subroutine that returns a list of keys. The keys are passed as the sole | ||
| 53 | parameter to the command (it is presumed that the child process can do whatever needs to be done | ||
| 54 | based on that single key). The subroutine that returns the list of keys will be periodically | ||
| 55 | executed (every 120 seconds by default, but adjustable via list_intvl) and the running list | ||
| 56 | updated as needed. Whenever a job finishes, that key is added back onto the end of the waiting | ||
| 57 | pool. | ||
| 58 | |||
| 59 | =head1 METHODS | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | use Win32::ProcFarm::Pool; | ||
| 64 | use Win32::ProcFarm::TickCount; | ||
| 65 | |||
| 66 | package Win32::ProcFarm::PerpetualPool; | ||
| 67 | |||
| 68 | use strict; | ||
| 69 | use vars qw($VERSION @ISA); | ||
| 70 | |||
| 71 | $VERSION = '2.15'; | ||
| 72 | |||
| 73 | @ISA = qw(Win32::ProcFarm::Pool); | ||
| 74 | |||
| 75 | sub new { | ||
| 76 | my $class = shift; | ||
| 77 | |||
| 78 | my($num_threads, $port_num, $script, $curdir, %options) = @_; | ||
| 79 | my $self = $class->SUPER::new($num_threads, $port_num, $script, $curdir, %options); | ||
| 80 | |||
| 81 | foreach my $i (qw(command list_check_intvl list_sub exit_check_intvl exit_sub)) { | ||
| 82 | exists $options{$i} and $self->{$i} = $options{$i}; | ||
| 83 | } | ||
| 84 | |||
| 85 | $self->{current_hash} = {}; | ||
| 86 | $self->{death_hash} = {}; | ||
| 87 | $self->{next_list_check} = Win32::GetTickCount(); | ||
| 88 | $self->{next_exit_check} = Win32::GetTickCount(); | ||
| 89 | $self->{state} = 'running'; | ||
| 90 | defined $self->{list_check_intvl} or $self->{list_check_intvl} = 60; | ||
| 91 | defined $self->{exit_check_intvl} or $self->{exit_check_intvl} = 2; | ||
| 92 | |||
| 93 | bless $self, $class; | ||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub list_check { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | $self->{state} eq 'running' or return; | ||
| 101 | |||
| 102 | my %temp; | ||
| 103 | @temp{$self->{list_sub}->($self)} = (); | ||
| 104 | |||
| 105 | foreach my $i (keys %temp) { | ||
| 106 | if (!exists $self->{current_hash}->{$i}) { | ||
| 107 | $self->add_waiting_job($i, $self->{command}, $i); | ||
| 108 | $self->{current_hash}->{$i} = undef; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | foreach my $i (keys %{$self->{current_hash}}) { | ||
| 113 | if (!exists $temp{$i}) { | ||
| 114 | $self->{death_hash}->{$i} = undef; | ||
| 115 | delete $self->{current_hash}->{$i}; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | sub exit_check { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | $self->{state} eq 'running' or return; | ||
| 124 | |||
| 125 | ref($self->{exit_sub}) eq 'CODE' or return; | ||
| 126 | if ($self->{exit_sub}->($self)) { | ||
| 127 | $self->{waiting_pool} = []; | ||
| 128 | $self->{death_hash} = {}; | ||
| 129 | $self->{state} = 'stopping'; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | sub start_pool { | ||
| 134 | my $self = shift; | ||
| 135 | my($sleep) = @_; | ||
| 136 | |||
| 137 | while (1) { | ||
| 138 | $self->cleanse_and_dispatch(); | ||
| 139 | if ($self->{state} eq 'stopping' && ($self->count_waiting + $self->count_running) == 0 ) { | ||
| 140 | $self->{state} = 'stopped'; | ||
| 141 | last; | ||
| 142 | } | ||
| 143 | $sleep and Win32::Sleep($sleep*1000); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | sub cleanse_and_dispatch { | ||
| 148 | my $self = shift; | ||
| 149 | |||
| 150 | if (Win32::ProcFarm::TickCount::compare($self->{next_list_check}, Win32::GetTickCount()) == -1) { | ||
| 151 | $self->list_check(); | ||
| 152 | $self->{next_list_check} = Win32::GetTickCount() + $self->{list_check_intvl} * 1000; | ||
| 153 | } | ||
| 154 | |||
| 155 | if (Win32::ProcFarm::TickCount::compare($self->{next_exit_check}, Win32::GetTickCount()) == -1) { | ||
| 156 | $self->exit_check(); | ||
| 157 | $self->{next_exit_check} = Win32::GetTickCount() + $self->{exit_check_intvl} * 1000; | ||
| 158 | } | ||
| 159 | |||
| 160 | return $self->SUPER::cleanse_and_dispatch; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub cleanse_thread { | ||
| 164 | my $self = shift; | ||
| 165 | my($thread) = @_; | ||
| 166 | |||
| 167 | $thread->{Parent}->get_state eq 'fin' or return 0; | ||
| 168 | |||
| 169 | my @temp = $thread->{Parent}->get_retval; | ||
| 170 | if (ref($self->{result_sub}) eq 'CODE') { | ||
| 171 | $self->{result_sub}->($thread->{key}, @temp); | ||
| 172 | } | ||
| 173 | |||
| 174 | if ($self->{state} eq 'running') { | ||
| 175 | $self->add_waiting_job($thread->{key}, $self->{command}, $thread->{key}); | ||
| 176 | } | ||
| 177 | |||
| 178 | $thread->{key} = undef; | ||
| 179 | return 1; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub dispatch_job { | ||
| 183 | my $self = shift; | ||
| 184 | my($thread) = @_; | ||
| 185 | |||
| 186 | $thread->{Parent}->get_state eq 'idle' or return 0; | ||
| 187 | my $job = $self->get_next_job() or return 0; | ||
| 188 | $thread->{Parent}->execute($job->{command}, @{$job->{params}}); | ||
| 189 | $thread->{key} = $job->{key}; | ||
| 190 | return 1; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub get_next_job { | ||
| 194 | my $self = shift; | ||
| 195 | |||
| 196 | my $job; | ||
| 197 | while ($job = shift(@{$self->{waiting_pool}})) { | ||
| 198 | if (exists $self->{death_hash}->{$job->{key}}) { | ||
| 199 | delete $self->{death_hash}->{$job->{key}}; | ||
| 200 | next; | ||
| 201 | } | ||
| 202 | return $job; | ||
| 203 | } | ||
| 204 | return undef; | ||
| 205 | } | ||
| 206 | |||
| 207 | 1; |