| 1 | package Cikl::Smrt::HandlerRole; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Cikl::Client::Factory; | ||
| 6 | use Cikl::Smrt::ClientBroker; | ||
| 7 | use Cikl::Smrt::Decoders::Null; | ||
| 8 | use Cikl::EventBuilder; | ||
| 9 | use Cikl::Smrt::Broker; | ||
| 10 | use Config::Simple; | ||
| 11 | use Try::Tiny; | ||
| 12 | use AnyEvent; | ||
| 13 | use Coro; | ||
| 14 | use DateTime; | ||
| 15 | |||
| 16 | use Mouse::Role; | ||
| 17 | use Cikl::Logging; | ||
| 18 | use Net::SSLeay; | ||
| 19 | Net::SSLeay::SSLeay_add_ssl_algorithms(); | ||
| 20 | |||
| 21 | use namespace::autoclean; | ||
| 22 | |||
| 23 | my $logger = get_logger(); | ||
| 24 | |||
| 25 | requires 'name'; | ||
| 26 | requires '_default_event_data'; | ||
| 27 | |||
| 28 | has 'global_config' => ( | ||
| 29 | is => 'ro', | ||
| 30 | isa => 'Config::Simple', | ||
| 31 | required => 1 | ||
| 32 | ); | ||
| 33 | |||
| 34 | has 'event_builder' => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => 'Cikl::EventBuilder', | ||
| 37 | lazy => 1, | ||
| 38 | builder => "_event_builder" | ||
| 39 | ); | ||
| 40 | |||
| 41 | has 'default_event_data' => ( | ||
| 42 | is => 'ro', | ||
| 43 | isa => 'HashRef', | ||
| 44 | lazy => 1, | ||
| 45 | builder => "_default_event_data" | ||
| 46 | ); | ||
| 47 | |||
| 48 | has 'refresh' => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'Bool', | ||
| 51 | lazy => 1, | ||
| 52 | builder => "_refresh" | ||
| 53 | ); | ||
| 54 | |||
| 55 | has 'not_before' => ( | ||
| 56 | is => 'ro', | ||
| 57 | isa => 'DateTime', | ||
| 58 | required => 1, | ||
| 59 | default => sub {return DateTime->now()->subtract(days => 3);} | ||
| 60 | ); | ||
| 61 | |||
| 62 | has 'proxy' => ( | ||
| 63 | is => 'ro', | ||
| 64 | required => 0 | ||
| 65 | ); | ||
| 66 | |||
| 67 | has 'fetcher' => ( | ||
| 68 | is => 'ro', | ||
| 69 | isa => 'Cikl::Smrt::Fetcher', | ||
| 70 | lazy => 1, | ||
| 71 | builder => "_fetcher" | ||
| 72 | ); | ||
| 73 | |||
| 74 | has 'parser' => ( | ||
| 75 | is => 'ro', | ||
| 76 | isa => 'Cikl::Smrt::Parser', | ||
| 77 | lazy => 1, | ||
| 78 | builder => "_parser" | ||
| 79 | ); | ||
| 80 | |||
| 81 | has 'decoder' => ( | ||
| 82 | is => 'ro', | ||
| 83 | isa => 'Cikl::Smrt::DecoderRole', | ||
| 84 | lazy => 1, | ||
| 85 | builder => "_decoder" | ||
| 86 | ); | ||
| 87 | |||
| 88 | has 'detecttime_format' => ( | ||
| 89 | is => 'ro', | ||
| 90 | isa => 'Maybe[Str]', | ||
| 91 | required => 0, | ||
| 92 | lazy => 1, | ||
| 93 | builder => '_build_detecttime_format' | ||
| 94 | ); | ||
| 95 | |||
| 96 | has 'detecttime_zone' => ( | ||
| 97 | is => 'ro', | ||
| 98 | isa => 'Maybe[Str]', | ||
| 99 | required => 0, | ||
| 100 | lazy => 1, | ||
| 101 | builder => '_build_detecttime_zone' | ||
| 102 | ); | ||
| 103 | |||
| 104 | |||
| 105 | sub _build_detecttime_format { | ||
| 106 | return undef; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _build_detecttime_zone { | ||
| 110 | return undef; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _event_builder { | ||
| 114 | my $self = shift; | ||
| 115 | return Cikl::EventBuilder->new( | ||
| 116 | not_before => $self->not_before()->epoch(), | ||
| 117 | default_event_data => $self->default_event_data(), | ||
| 118 | refresh => $self->refresh(), | ||
| 119 | detecttime_format => $self->detecttime_format(), | ||
| 120 | detecttime_zone => $self->detecttime_zone() | ||
| 121 | ) | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _refresh { | ||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub get_client { | ||
| 129 | my $self = shift; | ||
| 130 | my $client_config = $self->global_config()->get_block('client'); | ||
| 131 | return Cikl::Client::Factory->instantiate($client_config); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub process { | ||
| 135 | my $self = shift; | ||
| 136 | my ($err, $ret); | ||
| 137 | |||
| 138 | my $client = $self->get_client(); | ||
| 139 | |||
| 140 | my $broker = Cikl::Smrt::ClientBroker->new( | ||
| 141 | client => $client, | ||
| 142 | builder => $self->event_builder() | ||
| 143 | ); | ||
| 144 | try { | ||
| 145 | my ($err) = $self->parse($broker); | ||
| 146 | if ($err) { | ||
| 147 | die($err); | ||
| 148 | } | ||
| 149 | } catch { | ||
| 150 | $err = shift; | ||
| 151 | } finally { | ||
| 152 | if ($client) { | ||
| 153 | $client->shutdown(); | ||
| 154 | } | ||
| 155 | }; | ||
| 156 | if ($err) { | ||
| 157 | return($err); | ||
| 158 | } | ||
| 159 | |||
| 160 | $logger->debug('records to be processed: '.$broker->count() . ", too old: " . $broker->count_too_old()); | ||
| 161 | if ($broker->count_failed() > 0) { | ||
| 162 | $logger->debug('failed to create events: '. $broker->count_failed()); | ||
| 163 | } | ||
| 164 | |||
| 165 | if($broker->count() == 0){ | ||
| 166 | if ($broker->count_too_old() != 0) { | ||
| 167 | $logger->debug('your goback is too small, if you want records, increase the goback time'); | ||
| 168 | } | ||
| 169 | return (undef, 'no records'); | ||
| 170 | } | ||
| 171 | |||
| 172 | return(undef); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub fetch { | ||
| 176 | my $self = shift; | ||
| 177 | my $cv = AnyEvent->condvar; | ||
| 178 | my $fetcher = $self->fetcher(); | ||
| 179 | |||
| 180 | async { | ||
| 181 | try { | ||
| 182 | $cv->send($fetcher->fetch()); | ||
| 183 | } catch { | ||
| 184 | $cv->croak(shift); | ||
| 185 | }; | ||
| 186 | }; | ||
| 187 | while (!($cv->ready())) { | ||
| 188 | Coro::AnyEvent::sleep(1); | ||
| 189 | } | ||
| 190 | my $fh = $cv->recv(); | ||
| 191 | |||
| 192 | # auto-decode the content if need be | ||
| 193 | return $self->decode($fh); | ||
| 194 | |||
| 195 | ## TODO MPR : This looks like a hack for the utf8 and CR stuff below. | ||
| 196 | #return(undef,$ret) if($feedparser_config->{'cikl'} && $feedparser_config->{'cikl'} eq 'true'); | ||
| 197 | |||
| 198 | ## Commenting this out as I haven't run into any issues, yet. | ||
| 199 | # encode to utf8 | ||
| 200 | #$ret = encode_utf8($ret); | ||
| 201 | |||
| 202 | # remove any CR's | ||
| 203 | #$ret =~ s/\r//g; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub parse { | ||
| 207 | my $self = shift; | ||
| 208 | my $broker = shift; | ||
| 209 | |||
| 210 | my $fh = $self->fetch(); | ||
| 211 | |||
| 212 | my $return = $self->parser()->parse($fh, $broker); | ||
| 213 | |||
| 214 | $fh->close() or die($!); | ||
| 215 | undef $fh; | ||
| 216 | |||
| 217 | return(undef); | ||
| 218 | } | ||
| 219 | |||
| 220 | # Just pass things through. This can be overridden by subclasses. | ||
| 221 | sub decode { | ||
| 222 | my $self = shift; | ||
| 223 | my $content_ref = shift; | ||
| 224 | return $self->decoder->decode($content_ref); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub _decoder { | ||
| 228 | # Use a default decoder of 'Null', just because we're nice. | ||
| 229 | return Cikl::Smrt::Decoders::Null->new(); | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | 1; |