| 1 | 1 | ▶ | use 5.008; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | package Data::Conveyor::App::Dispatch; | ||||||||||||
| 6 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | $Data::Conveyor::App::Dispatch::VERSION = '1.103130'; | |||||||||||
| 8 | } | ||||||||||||
| 9 | # ABSTRACT: Stage-based conveyor-belt-like ticket handling system | ||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use Time::HiRes 'usleep'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use Data::Conveyor::Lock::Dispatcher; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use parent 'Class::Scaffold::App::CommandLine'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | __PACKAGE__->mk_framework_object_accessors( | |||||||||||
| 15 | ticket_provider => 'ticket_provider', | ||||||||||||
| 16 | ticket_dispatcher => 'dispatcher', | ||||||||||||
| 17 | )->mk_scalar_accessors( | ||||||||||||
| 18 | qw( | ||||||||||||
| 19 | done stage_class dispatcher_sleep lockpath lockhandler | ||||||||||||
| 20 | ) | ||||||||||||
| 21 | )->mk_integer_accessors(qw(ticket_count)); | ||||||||||||
| 22 | 1 | ▶ | use constant GETOPT => qw/lockpath=s/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | |||||||||||||
| 24 | 0 | ▶ | sub app_init { | ||||||||||
|
Subroutine
app_init: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 25 | 0 | my $self = shift; | |||||||||||
| 26 | 0 | $self->SUPER::app_init(@_); | |||||||||||
| 27 | 0 | $self->delegate->make_obj('ticket_payload'); | |||||||||||
| 28 | |||||||||||||
| 29 | # If several dispatchers are running, we want to know which log message | ||||||||||||
| 30 | # came from which process. Can be done only now that | ||||||||||||
| 31 | # Class::Scaffold::App->app_init will have instantiated the log singleton. | ||||||||||||
| 32 | 0 | $self->log->set_pid; | |||||||||||
| 33 | 0 | ▶ | $self->ticket_count(0) unless defined $self->ticket_count; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 0 | ▶ | $self->dispatcher_sleep($self->delegate->dispatcher_sleep || 10); | ||||||||||
|
Condition0%0/2
$self->delegate->dispatcher_sleep || 10
MC/DC0%0/1
$self->delegate->dispatcher_sleep || 10
$self->delegate->dispatcher_sleep
Truth table0%0/2
A
A= $self->delegate->dispatcher_sleep
| |||||||||||||
| 35 | 0 | $self->lockpath($self->delegate->lockpath); | |||||||||||
| 36 | 0 | $self->delegate->control->filename($self->delegate->control_filename); | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | sub check_lockfile { | ||||||||||
|
Subroutine
check_lockfile: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||
| 41 | 0 | ▶ | return 1 if $self->delegate->ignore_locks; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 0 | ▶ | $self->lockhandler | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | || $self->lockhandler( | ||||||||||||
| 44 | Data::Conveyor::Lock::Dispatcher->new(lockpath => $self->lockpath)); | ||||||||||||
| 45 | 0 | $self->lockhandler->lockstate; | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub app_code { | ||||||||||
|
Subroutine
app_code: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 49 | 0 | my $self = shift; | |||||||||||
| 50 | 0 | $self->log->info("starting"); | |||||||||||
| 51 | 0 | $self->SUPER::app_code(@_); | |||||||||||
| 52 | |||||||||||||
| 53 | # keep EINTR from looping over into the next sleep call; | ||||||||||||
| 54 | # this also should rollback the interrupted transaction, | ||||||||||||
| 55 | # which is exactly what we want. -ac | ||||||||||||
| 56 | 0 | ▶ | local $SIG{INT} = sub { exit }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 57 | 0 | my $success; | |||||||||||
| 58 | 0 | ▶ | while (!$self->done) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | |||||||||||||
| 60 | # this could stay here | ||||||||||||
| 61 | 0 | ▶ | unless ($self->check_lockfile) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | $self->done(1); | |||||||||||
| 63 | 0 | last; | |||||||||||
| 64 | } | ||||||||||||
| 65 | 0 | $self->ticket_count_inc; | |||||||||||
| 66 | 0 | ▶ | $self->done(1) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | if $self->ticket_count >= $self->delegate->max_tickets_per_dispatcher; | ||||||||||||
| 68 | 0 | ▶ | unless ($self->delegate->control->read) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 0 | $self->log->info("control returned false, exiting."); | |||||||||||
| 70 | 0 | $self->done(1); | |||||||||||
| 71 | 0 | last; | |||||||||||
| 72 | } | ||||||||||||
| 73 | 0 | my $ticket; | |||||||||||
| 74 | |||||||||||||
| 75 | # If there aren't any tickets waiting to be processed, don't exit, | ||||||||||||
| 76 | # just sleep. We don't want to keep starting and stopping dispatcher | ||||||||||||
| 77 | # processes just because there are no more tickets for a few seconds. | ||||||||||||
| 78 | 0 | ▶ | unless ( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | defined( | ||||||||||||
| 80 | $ticket = $self->ticket_provider->get_next_ticket( | ||||||||||||
| 81 | [ $self->delegate->control->allowed_stages_keys ], $success | ||||||||||||
| 82 | ) | ||||||||||||
| 83 | ) | ||||||||||||
| 84 | ) { | ||||||||||||
| 85 | 0 | $self->log->info("sleep %ss", $self->dispatcher_sleep); | |||||||||||
| 86 | 0 | sleep($self->dispatcher_sleep); | |||||||||||
| 87 | 0 | next; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | # XXX: $ticket->stage should already be a ticket stage value object, | ||||||||||||
| 91 | # so we'd only need to do $ticket->stage->name. | ||||||||||||
| 92 | #my $stage = $self->delegate->make_obj('value_ticket_stage')->new( | ||||||||||||
| 93 | # value => $ticket->stage)->name; | ||||||||||||
| 94 | # Try to open the ticket - this can still fail if another dispatcher | ||||||||||||
| 95 | # process has already opened the ticket. | ||||||||||||
| 96 | # try_open sets the stage to aktiv_* and commits - we don't want that | ||||||||||||
| 97 | # any more in nic.at | ||||||||||||
| 98 | # we should try to get the db locks instead. | ||||||||||||
| 99 | 0 | $self->log_line($ticket, '>'); | |||||||||||
| 100 | 0 | ▶ | if ($self->open_ticket($ticket)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | 0 | $success = 1; | |||||||||||
| 102 | |||||||||||||
| 103 | # $self->log_line($ticket, $success); | ||||||||||||
| 104 | } else { | ||||||||||||
| 105 | 0 | $success = 0; | |||||||||||
| 106 | |||||||||||||
| 107 | # $self->log_line($ticket, $success); | ||||||||||||
| 108 | # cool it a little | ||||||||||||
| 109 | 0 | usleep(200_000); | |||||||||||
| 110 | 0 | next; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | # Now we have an opened ticket; process it. | ||||||||||||
| 114 | 0 | $self->process_ticket($ticket); | |||||||||||
| 115 | } | ||||||||||||
| 116 | 0 | $self->log->info("exiting"); | |||||||||||
| 117 | } | ||||||||||||
| 118 | |||||||||||||
| 119 | 0 | ▶ | sub log_line { | ||||||||||
|
Subroutine
log_line: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 120 | 0 | my ($self, $ticket, $success) = @_; | |||||||||||
| 121 | 0 | $self->log->info("%s [%s] [% 3s] %s", | |||||||||||
| 122 | $ticket->ticket_no, $success, $ticket->nice, $ticket->stage->name); | ||||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | 0 | ▶ | sub open_ticket { | ||||||||||
|
Subroutine
open_ticket: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 126 | 0 | my ($self, $ticket) = @_; | |||||||||||
| 127 | 0 | $ticket->try_open; | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 0 | ▶ | sub process_ticket { | ||||||||||
|
Subroutine
process_ticket: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 131 | 0 | my ($self, $ticket) = @_; | |||||||||||
| 132 | 0 | $self->dispatcher->dispatch($ticket); | |||||||||||
| 133 | } | ||||||||||||
| 134 | 1 | 1; | |||||||||||
| 135 | |||||||||||||
| 136 | |||||||||||||
| 137 | __END__ | ||||||||||||