| 1 | package #hide from metacpan | ||
| 2 | BackgroundJob; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use base qw(Tk::AppWindow::BaseClasses::Callback); | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my $class = shift; | ||
| 11 | my $interval = shift; | ||
| 12 | my $self = $class->SUPER::new(@_); | ||
| 13 | $self->{INTERVAL} = $interval; | ||
| 14 | $self->{COUNT} = 1; | ||
| 15 | $self->{PAUSED} = 0; | ||
| 16 | return $self; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub execute { | ||
| 20 | my $self = shift; | ||
| 21 | return if $self->paused; | ||
| 22 | my $interval = $self->{INTERVAL}; | ||
| 23 | my $count = $self->{COUNT}; | ||
| 24 | if ($count eq $interval) { | ||
| 25 | $self->SUPER::execute(@_); | ||
| 26 | $self->{COUNT} = 1; | ||
| 27 | } else { | ||
| 28 | $self->{COUNT} = $count + 1 | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | sub paused { | ||
| 33 | my $self = shift; | ||
| 34 | $self->{PAUSED} = shift if @_; | ||
| 35 | return $self->{PAUSED} | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | package Tk::AppWindow::Ext::Daemons; | ||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | Tk::AppWindow::Ext::Daemons - Run background processes easily | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | use strict; | ||
| 48 | use warnings; | ||
| 49 | use Carp; | ||
| 50 | use vars qw($VERSION); | ||
| 51 | $VERSION="0.08"; | ||
| 52 | |||
| 53 | use base qw( Tk::AppWindow::BaseClasses::Extension ); | ||
| 54 | |||
| 55 | =head1 SYNOPSIS | ||
| 56 | |||
| 57 | my $app = new Tk::AppWindow(@options, | ||
| 58 | -extensions => ['Daemons'], | ||
| 59 | ); | ||
| 60 | $app->MainLoop; | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | Easily run background jobs organized neatly application wide. | ||
| 65 | |||
| 66 | =head1 CONFIG VARIABLES | ||
| 67 | |||
| 68 | =over 4 | ||
| 69 | |||
| 70 | =item B<-cycleinterval> | ||
| 71 | |||
| 72 | Default value 10 miliseconds. This is the base interval for the Deamons system. | ||
| 73 | |||
| 74 | How often a job is executed is defined by their own interval parameter. The real interval time of a job is the base interval | ||
| 75 | multiplied by the job interval. | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub new { | ||
| 82 | my $class = shift; | ||
| 83 | my $self = $class->SUPER::new(@_); | ||
| 84 | $self->addPreConfig( | ||
| 85 | -cycleinterval => ['PASSIVE', undef, undef, 10], | ||
| 86 | ); | ||
| 87 | $self->{JOBS} = {}; | ||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head1 METHODS | ||
| 92 | |||
| 93 | =over 4 | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub cycle { | ||
| 98 | my $self = shift; | ||
| 99 | delete $self->{'cycle_id'}; | ||
| 100 | my $jobs = $self->_jobs; | ||
| 101 | for (keys %$jobs) { | ||
| 102 | my $call = $jobs->{$_}; | ||
| 103 | $call->execute; | ||
| 104 | } | ||
| 105 | $self->cycleStart; | ||
| 106 | } | ||
| 107 | |||
| 108 | =item B<cyleActive> | ||
| 109 | |||
| 110 | Returns true if one or more background jobs are running. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub cycleActive { | ||
| 115 | return exists $_[0]->{'cycle_id'} | ||
| 116 | } | ||
| 117 | |||
| 118 | =item B<cyleStart> | ||
| 119 | |||
| 120 | Starts the job cycle unless it is running already. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub cycleStart { | ||
| 125 | my $self = shift; | ||
| 126 | my $cycle = $self->configGet('-cycleinterval'); | ||
| 127 | $self->{'cycle_id'} = $self->after($cycle, ['cycle', $self]) unless $self->cycleActive; | ||
| 128 | } | ||
| 129 | |||
| 130 | =item B<cyleStop> | ||
| 131 | |||
| 132 | Stops the job cycle if it is running. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub cycleStop { | ||
| 137 | my $self = shift; | ||
| 138 | my $id = $self->{'cycle_id'}; | ||
| 139 | $self->afterCancel($id) if defined $id; | ||
| 140 | delete $self->{'cycle_id'}; | ||
| 141 | } | ||
| 142 | |||
| 143 | =item B<jobAdd>I<($name, $interval, @call)> | ||
| 144 | |||
| 145 | Add the job I<$name> unless it already exists. | ||
| 146 | I<$interval> specifies after how many cycles the job is executed. | ||
| 147 | For details about I<@call> see L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub jobAdd { | ||
| 152 | my ($self, $name, $interval, @call) = @_; | ||
| 153 | |||
| 154 | unless (defined $name) { | ||
| 155 | croak "You must specify a name"; | ||
| 156 | return | ||
| 157 | } | ||
| 158 | unless (defined $interval) { | ||
| 159 | croak "You must specify an interval"; | ||
| 160 | return | ||
| 161 | } | ||
| 162 | unless (@call) { | ||
| 163 | croak "No callback defined"; | ||
| 164 | return | ||
| 165 | } | ||
| 166 | |||
| 167 | my $new = new BackgroundJob($interval, @call); | ||
| 168 | my $jobs = $self->_jobs; | ||
| 169 | if (exists $jobs->{$name}) { | ||
| 170 | croak "Job $name already exists"; | ||
| 171 | return | ||
| 172 | } | ||
| 173 | $jobs->{$name} = $new; | ||
| 174 | $self->cycleStart; | ||
| 175 | } | ||
| 176 | |||
| 177 | =item B<jobExists>I<($name)> | ||
| 178 | |||
| 179 | Returns true if a job I<$name> is currently running. | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub jobExists { | ||
| 184 | my ($self, $name) = @_; | ||
| 185 | my $jobs = $self->_jobs; | ||
| 186 | return exists $jobs->{$name} | ||
| 187 | } | ||
| 188 | |||
| 189 | =item B<jobGet>I<($name)> | ||
| 190 | |||
| 191 | Returns the job object of I<$name>. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub jobGet { | ||
| 196 | my ($self, $name) = @_; | ||
| 197 | unless (defined $name) { | ||
| 198 | croak "You must specify a name"; | ||
| 199 | return | ||
| 200 | } | ||
| 201 | my $jobs = $self->_jobs; | ||
| 202 | unless (exists $jobs->{$name}) { | ||
| 203 | croak "Job $name does not exist"; | ||
| 204 | return | ||
| 205 | } | ||
| 206 | return $jobs->{$name}; | ||
| 207 | } | ||
| 208 | |||
| 209 | =item B<jobList> | ||
| 210 | |||
| 211 | Returns a list of running jobs. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub jobList { | ||
| 216 | my $self = shift; | ||
| 217 | my $jobs = $self->_jobs; | ||
| 218 | return keys %$jobs | ||
| 219 | } | ||
| 220 | |||
| 221 | =item B<jobPause>I<($name)> | ||
| 222 | |||
| 223 | Pauses job I<$name>. | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | sub jobPause { | ||
| 228 | my ($self, $name) = @_; | ||
| 229 | my $job = $self->jobGet($name); | ||
| 230 | $job->paused(1) if defined $job; | ||
| 231 | } | ||
| 232 | |||
| 233 | =item B<jobRemove>I<($name)> | ||
| 234 | |||
| 235 | Removes job I<$name>. | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | sub jobRemove { | ||
| 240 | my ($self, $name) = @_; | ||
| 241 | my $job = $self->jobGet($name); | ||
| 242 | my $jobs = $self->_jobs; | ||
| 243 | delete $jobs->{$name} if defined $job; | ||
| 244 | my @list = $self->jobList; | ||
| 245 | $self->cycleStop unless @list; | ||
| 246 | } | ||
| 247 | |||
| 248 | =item B<jobResume>I<($name)> | ||
| 249 | |||
| 250 | Resumes job I<$name> | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | sub jobResume { | ||
| 255 | my ($self, $name) = @_; | ||
| 256 | my $job = $self->jobGet($name); | ||
| 257 | $job->paused(0) if defined $job; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub _jobs { | ||
| 261 | return $_[0]->{JOBS} | ||
| 262 | } | ||
| 263 | |||
| 264 | =back | ||
| 265 | |||
| 266 | =head1 AUTHOR | ||
| 267 | |||
| 268 | Hans Jeuken (hanje at cpan dot org) | ||
| 269 | |||
| 270 | =head1 BUGS | ||
| 271 | |||
| 272 | Unknown. If you find any, please contact the author. | ||
| 273 | |||
| 274 | =head1 SEE ALSO | ||
| 275 | |||
| 276 | L<Tk::AppWindow> | ||
| 277 | |||
| 278 | L<Tk::AppWindow::BaseClasses::Callback> | ||
| 279 | |||
| 280 | L<Tk::AppWindow::BaseClasses::Extension> | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | 1; | ||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 |