| 1 | use utf8; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | package DR::TarantoolQueue::Worker; | ||
| 5 | use Carp; | ||
| 6 | use Mouse; | ||
| 7 | use Coro; | ||
| 8 | use Data::Dumper; | ||
| 9 | use Encode qw(encode_utf8); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | DR::TarantoolQueue::Worker - template for workers | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | my $worker = DR::TarantoolQueue::Worker->new( | ||
| 18 | count => 10, # defaults 1 | ||
| 19 | queue => $queue | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub process { | ||
| 23 | my ($task) = @_; | ||
| 24 | |||
| 25 | |||
| 26 | ... do something with task | ||
| 27 | |||
| 28 | |||
| 29 | } | ||
| 30 | |||
| 31 | $worker->run(\&process) | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | =over | ||
| 36 | |||
| 37 | =item * | ||
| 38 | |||
| 39 | Process function can throw exception. The task will be buried (if process | ||
| 40 | function didn't change task status yet. | ||
| 41 | |||
| 42 | =item * | ||
| 43 | |||
| 44 | If process function didn't change task status (didn't call B<ack>, or | ||
| 45 | L<DR::TarantoolQueue::Task/release>) worker calls | ||
| 46 | L<DR::TarantoolQueue::Task/ack>. | ||
| 47 | |||
| 48 | =item * | ||
| 49 | |||
| 50 | L<run> method catches B<SIGTERM> and B<SIGINT> and waits for all process | ||
| 51 | functions are done and then do return. | ||
| 52 | |||
| 53 | =item * | ||
| 54 | |||
| 55 | Worker uses default B<tube> and B<space> in queue. So You have to define | ||
| 56 | them in Your queue or here. | ||
| 57 | |||
| 58 | =back | ||
| 59 | |||
| 60 | =head1 ATTRIBUTES | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | =head2 count | ||
| 65 | |||
| 66 | Count of process functions that can do something at the same time. | ||
| 67 | Default value is B<1>. The attribute means something if Your B<process> | ||
| 68 | function uses L<Coro> and Your queue uses L<Coro>, too. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | has count => isa => 'Num', is => 'rw', default => 1; | ||
| 73 | |||
| 74 | |||
| 75 | =head2 queue | ||
| 76 | |||
| 77 | Ref to Your queue. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | has queue => isa => 'DR::TarantoolQueue', is => 'ro', required => 1; | ||
| 82 | |||
| 83 | =head2 space & tube | ||
| 84 | |||
| 85 | Space and tube for processing queue. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | has space => isa => 'Str|Undef', is => 'ro'; | ||
| 90 | has tube => isa => 'Str|Undef', is => 'ro'; | ||
| 91 | |||
| 92 | =head2 restart | ||
| 93 | |||
| 94 | The function will be called if L<restart_limit> is reached. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | has restart => isa => 'CodeRef|Undef', is => 'rw'; | ||
| 99 | |||
| 100 | =head2 restart_limit | ||
| 101 | |||
| 102 | How many tasks can be processed before restart worker. | ||
| 103 | |||
| 104 | If B<restart_limit> is 0, restart mechanizm will be disabled. | ||
| 105 | |||
| 106 | If L<restart> callback isn't defined, restart mechanizm will be disabled. | ||
| 107 | |||
| 108 | Each processed task increments common taskcounter. When B<restart_limit> is | ||
| 109 | reached by the counter, worker don't take new task and call L<restart> | ||
| 110 | function. After L<restart> worker will continue to process tasks. | ||
| 111 | |||
| 112 | In L<restart> callback user can do L<perlfunc/exec> or L<perlfunc/exit> | ||
| 113 | to avoid memory leaks. | ||
| 114 | |||
| 115 | DR::TarantoolQueue::Worker->new( | ||
| 116 | restart_limit => 100, | ||
| 117 | restart => sub { exec perl => $0 }, | ||
| 118 | queue => $q, | ||
| 119 | count => 10 | ||
| 120 | )->run(sub { ... }); | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | has restart_limit => isa => 'Num', is => 'rw', default => 0; | ||
| 125 | |||
| 126 | =head1 PRIVATE ATTRIBUTES | ||
| 127 | |||
| 128 | =head2 timeout | ||
| 129 | |||
| 130 | timeout for queue.take | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | has timeout => isa => 'Num', is => 'ro', default => 2; | ||
| 135 | |||
| 136 | =head2 is_run | ||
| 137 | |||
| 138 | B<True> means that workers are run | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | has is_run => isa => 'Bool', is => 'rw', default => 0; | ||
| 143 | |||
| 144 | =head2 is_stopping | ||
| 145 | |||
| 146 | B<True> means that workers are stopping (by B<SIGTERM>/B<SIGINT>/L<stop>) | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | has is_stopping => isa => 'Bool', is => 'rw', default => 0; | ||
| 151 | |||
| 152 | |||
| 153 | has stop_waiters => isa => 'ArrayRef', is => 'ro', default => sub {[]}; | ||
| 154 | |||
| 155 | |||
| 156 | has mailto => isa => 'Maybe[Str]', is => 'ro'; | ||
| 157 | has mailfrom => isa => 'Maybe[Str]', is => 'ro'; | ||
| 158 | has mailsublect => isa => 'Str', is => 'ro', default => 'Worker died'; | ||
| 159 | has mailheaders => isa => 'HashRef[Str]', is => 'ro', default => sub {{}}; | ||
| 160 | |||
| 161 | |||
| 162 | =head1 METHODS | ||
| 163 | |||
| 164 | =head2 run(CODEREF[, CODEREF]) | ||
| 165 | |||
| 166 | Run workers. Two arguments: | ||
| 167 | |||
| 168 | =over | ||
| 169 | |||
| 170 | =item process function | ||
| 171 | |||
| 172 | Function will receive three arguments: | ||
| 173 | |||
| 174 | =over | ||
| 175 | |||
| 176 | =item task | ||
| 177 | |||
| 178 | =item queue | ||
| 179 | |||
| 180 | =item task number | ||
| 181 | |||
| 182 | =back | ||
| 183 | |||
| 184 | =item debug function | ||
| 185 | |||
| 186 | The function can be used to show internal debug messages. | ||
| 187 | |||
| 188 | =over | ||
| 189 | |||
| 190 | =item * | ||
| 191 | |||
| 192 | Debug messages aren't finished by B<EOL> (C<\n>). | ||
| 193 | |||
| 194 | =item * | ||
| 195 | |||
| 196 | The function will be called as L<perlfunc/sprintf>. | ||
| 197 | |||
| 198 | =back | ||
| 199 | |||
| 200 | =back | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub run { | ||
| 205 | my ($self, $cb, $debugf) = @_; | ||
| 206 | croak 'process subroutine is not CODEREF' unless 'CODE' eq ref $cb; | ||
| 207 | $debugf //= sub { }; | ||
| 208 | croak 'debugf subroutine is not CODEREF' unless 'CODE' eq ref $debugf; | ||
| 209 | |||
| 210 | croak 'worker is already run' if $self->is_run; | ||
| 211 | |||
| 212 | local $SIG{TERM} = sub { | ||
| 213 | $debugf->('SIGTERM was received, stopping...'); | ||
| 214 | $self->is_stopping( 1 ) | ||
| 215 | }; | ||
| 216 | local $SIG{INT} = sub { | ||
| 217 | $debugf->('SIGINT was received, stopping...'); | ||
| 218 | $self->is_stopping( 1 ) | ||
| 219 | }; | ||
| 220 | |||
| 221 | |||
| 222 | $self->is_run( 1 ); | ||
| 223 | $self->is_stopping( 0 ); | ||
| 224 | |||
| 225 | my $no; | ||
| 226 | my @f; | ||
| 227 | while(1) { | ||
| 228 | ($no, @f) = (0); | ||
| 229 | |||
| 230 | for (1 .. $self->count) { | ||
| 231 | push @f => async { | ||
| 232 | while($self->is_run and !$self->is_stopping) { | ||
| 233 | last if $self->restart and $no >= $self->restart_limit; | ||
| 234 | my $task = $self->queue->take( | ||
| 235 | defined($self->space) ? (space => $self->space) : (), | ||
| 236 | defined($self->tube) ? (tube => $self->tube) : (), | ||
| 237 | timeout => $self->timeout, | ||
| 238 | ); | ||
| 239 | next unless $task; | ||
| 240 | |||
| 241 | $no++; | ||
| 242 | eval { | ||
| 243 | $cb->( $task, $self->queue, $no ); | ||
| 244 | }; | ||
| 245 | |||
| 246 | if ($@) { | ||
| 247 | $debugf->('Worker was died (%s)', $@); | ||
| 248 | eval { | ||
| 249 | $self->sendmail( | ||
| 250 | $task, | ||
| 251 | sprintf "Worker was died: %s", $@ | ||
| 252 | ); | ||
| 253 | }; | ||
| 254 | if ($@) { | ||
| 255 | $debugf->("Can't send mail (%s)", $@); | ||
| 256 | } | ||
| 257 | if ($task->status eq 'taken') { | ||
| 258 | eval { $task->bury }; | ||
| 259 | if ($@) { | ||
| 260 | $debugf->("Can't bury task %s: %s", | ||
| 261 | $task->id, $@); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | next; | ||
| 265 | } | ||
| 266 | if ($task->status eq 'taken') { | ||
| 267 | eval { $task->ack }; | ||
| 268 | if ($@) { | ||
| 269 | $debugf->("Can't ack task %s: %s", $task->id, $@); | ||
| 270 | } | ||
| 271 | next; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | $_->join for @f; | ||
| 278 | |||
| 279 | last unless $self->is_run; | ||
| 280 | last if $self->is_stopping; | ||
| 281 | last unless $self->restart; | ||
| 282 | last unless $no >= $self->restart_limit; | ||
| 283 | $self->restart->( ); | ||
| 284 | } | ||
| 285 | |||
| 286 | $self->is_run( 0 ); | ||
| 287 | $self->is_stopping( 0 ); | ||
| 288 | while(@{ $self->stop_waiters }) { | ||
| 289 | my $w = shift @{ $self->stop_waiters }; | ||
| 290 | $w->ready; | ||
| 291 | } | ||
| 292 | return $self->count; | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | =head2 sendmail | ||
| 297 | |||
| 298 | Send mail about worker crash | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub sendmail { | ||
| 303 | my ($self, $task, $error) = @_; | ||
| 304 | return unless $self->mailto; | ||
| 305 | return unless $self->mailfrom; | ||
| 306 | |||
| 307 | my $subject = encode_utf8 $self->mailsublect; | ||
| 308 | |||
| 309 | require MIME::Lite; | ||
| 310 | require MIME::Words; | ||
| 311 | |||
| 312 | $subject .= sprintf' (space: %s, tube: %s)', $task->space, $task->tube; | ||
| 313 | $subject = MIME::Words::encode_mimeword($subject, 'B', 'utf-8'); | ||
| 314 | |||
| 315 | my $mail = MIME::Lite->new( | ||
| 316 | From => $self->mailfrom || 'dimka@uvw.ru', | ||
| 317 | To => $self->mailto || 'dimka@uvw.ru', | ||
| 318 | Subject => $subject, | ||
| 319 | Type => 'multipart/fixed', | ||
| 320 | ); | ||
| 321 | |||
| 322 | local $Data::Dumper::Indent = 1; | ||
| 323 | local $Data::Dumper::Terse = 1; | ||
| 324 | local $Data::Dumper::Useqq = 1; | ||
| 325 | local $Data::Dumper::Deepcopy = 1; | ||
| 326 | local $Data::Dumper::Maxdepth = 0; | ||
| 327 | |||
| 328 | |||
| 329 | $mail->attach( | ||
| 330 | Type => 'text/plain; charset=utf-8', | ||
| 331 | Disposition => 'inline', | ||
| 332 | Data => encode_utf8($error), | ||
| 333 | ); | ||
| 334 | $mail->attach( | ||
| 335 | Type => 'text/plain; charset=utf-8', | ||
| 336 | Filename => 'task.dump.txt', | ||
| 337 | Disposition => 'inline', | ||
| 338 | Data => Dumper($task), | ||
| 339 | ); | ||
| 340 | |||
| 341 | $mail->add($_ => $self->mailheaders->{$_}) for keys %{ $self->mailheaders }; | ||
| 342 | $mail->send; | ||
| 343 | } | ||
| 344 | |||
| 345 | =head2 stop | ||
| 346 | |||
| 347 | Stop worker cycle | ||
| 348 | |||
| 349 | =cut | ||
| 350 | |||
| 351 | sub stop { | ||
| 352 | my ($self) = @_; | ||
| 353 | return 0 unless $self->is_run; | ||
| 354 | $self->is_stopping( 1 ); | ||
| 355 | push @{ $self->stop_waiters } => $Coro::current; | ||
| 356 | Coro::schedule; | ||
| 357 | return $self->is_run; | ||
| 358 | } | ||
| 359 | |||
| 360 | __PACKAGE__->meta->make_immutable(); | ||
| 361 |