| 1 | package Cikl::Smrt::Broker; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Mouse; | ||
| 5 | use namespace::autoclean; | ||
| 6 | use Try::Tiny; | ||
| 7 | |||
| 8 | has 'builder' => ( | ||
| 9 | is => 'bare', | ||
| 10 | isa => 'Cikl::EventBuilder', | ||
| 11 | reader => '_builder', | ||
| 12 | required => 1 | ||
| 13 | ); | ||
| 14 | |||
| 15 | has 'count' => ( | ||
| 16 | is => 'ro', | ||
| 17 | isa => 'Num', | ||
| 18 | writer => '_set_count', | ||
| 19 | required => 1, | ||
| 20 | default => 0, | ||
| 21 | init_arg => undef | ||
| 22 | ); | ||
| 23 | |||
| 24 | has 'count_failed' => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'Num', | ||
| 27 | writer => '_set_count_failed', | ||
| 28 | required => 1, | ||
| 29 | default => 0, | ||
| 30 | init_arg => undef | ||
| 31 | ); | ||
| 32 | |||
| 33 | has 'count_too_old' => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => 'Num', | ||
| 36 | writer => '_set_count_too_old', | ||
| 37 | required => 1, | ||
| 38 | default => 0, | ||
| 39 | init_arg => undef | ||
| 40 | ); | ||
| 41 | |||
| 42 | sub emit { | ||
| 43 | my $self = shift; | ||
| 44 | my $event_hash = shift; | ||
| 45 | my $err; | ||
| 46 | my $event; | ||
| 47 | try { | ||
| 48 | $event = $self->_builder->build_event($event_hash); | ||
| 49 | } catch { | ||
| 50 | $err = shift; | ||
| 51 | }; | ||
| 52 | if ($err) { | ||
| 53 | $self->_set_count_failed($self->count_failed() + 1); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | if (defined($event)) { | ||
| 57 | $self->_emit($event); | ||
| 58 | $self->_set_count($self->count() + 1); | ||
| 59 | } else { | ||
| 60 | $self->_set_count_too_old($self->count_too_old() + 1); | ||
| 61 | # It was too old. | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _emit { | ||
| 66 | my $self = shift; | ||
| 67 | die("_emit not implemented!"); | ||
| 68 | } | ||
| 69 | |||
| 70 | __PACKAGE__->meta->make_immutable; | ||
| 71 | |||
| 72 | 1; |