| 1 | package POE::Component::Metabase::Relay::Server::Queue; | ||
| 2 | $POE::Component::Metabase::Relay::Server::Queue::VERSION = '0.42'; | ||
| 3 | # ABSTRACT: Submission queue for the metabase relay | ||
| 4 | |||
| 5 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use POE qw[Component::EasyDBI]; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use POE::Component::Client::HTTP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use POE::Component::Resolver; | ||
| 10 | use POE::Component::Metabase::Client::Submit; | ||
| 11 | use CPAN::Testers::Report (); | ||
| 12 | use Metabase::User::Profile (); | ||
| 13 | use Metabase::User::Secret (); | ||
| 14 | use Module::Load::Conditional qw[can_load]; | ||
| 15 | use JSON::MaybeXS; | ||
| 16 | use Params::Util qw[_HASH]; | ||
| 17 | use Time::HiRes (); | ||
| 18 | use Data::UUID; | ||
| 19 | |||
| 20 | use constant DELAY => 150; | ||
| 21 | |||
| 22 | |||
| 23 | my $sql = { | ||
| 24 | 'create' => 'CREATE TABLE IF NOT EXISTS queue ( id varchar(150), submitted varchar(32), attempts INTEGER, data BLOB )', | ||
| 25 | 'insert' => 'INSERT INTO queue values(?,?,?,?)', | ||
| 26 | 'delete' => 'DELETE FROM queue where id = ?', | ||
| 27 | 'queue' => 'SELECT * FROM queue ORDER BY attempts ASC, submitted ASC limit ', # the limit is appended via "submissions" | ||
| 28 | 'update' => 'UPDATE queue SET attempts = ? WHERE id = ?', | ||
| 29 | 'addidx' => [ | ||
| 30 | 'CREATE INDEX IF NOT EXISTS queue_id ON queue ( id )', | ||
| 31 | 'CREATE INDEX IF NOT EXISTS queue_att_sub ON queue ( attempts, submitted )', | ||
| 32 | ], | ||
| 33 | }; | ||
| 34 | |||
| 35 | use MooseX::POE; | ||
| 36 | use MooseX::Types::URI qw[Uri]; | ||
| 37 | |||
| 38 | { | ||
| 39 | use Moose::Util::TypeConstraints; | ||
| 40 | |||
| 41 | my $ps = subtype as 'Str', where { $poe_kernel->alias_resolve( $_ ) }; | ||
| 42 | coerce $ps, from 'Str', via { $poe_kernel->alias_resolve( $_ )->ID }; | ||
| 43 | |||
| 44 | has 'session' => ( | ||
| 45 | is => 'ro', | ||
| 46 | isa => $ps, | ||
| 47 | coerce => 1, | ||
| 48 | ); | ||
| 49 | |||
| 50 | no Moose::Util::TypeConstraints; | ||
| 51 | } | ||
| 52 | |||
| 53 | has 'event' => ( | ||
| 54 | is => 'ro', | ||
| 55 | isa => 'Str', | ||
| 56 | ); | ||
| 57 | |||
| 58 | has 'profile' => ( | ||
| 59 | is => 'ro', | ||
| 60 | isa => 'Metabase::User::Profile', | ||
| 61 | required => 1, | ||
| 62 | ); | ||
| 63 | |||
| 64 | has 'secret' => ( | ||
| 65 | is => 'ro', | ||
| 66 | isa => 'Metabase::User::Secret', | ||
| 67 | required => 1, | ||
| 68 | ); | ||
| 69 | |||
| 70 | has 'dsn' => ( | ||
| 71 | is => 'ro', | ||
| 72 | isa => 'Str', | ||
| 73 | required => 1, | ||
| 74 | ); | ||
| 75 | |||
| 76 | has 'uri' => ( | ||
| 77 | is => 'ro', | ||
| 78 | isa => Uri, | ||
| 79 | coerce => 1, | ||
| 80 | required => 1, | ||
| 81 | ); | ||
| 82 | |||
| 83 | has 'username' => ( | ||
| 84 | is => 'ro', | ||
| 85 | isa => 'Str', | ||
| 86 | default => '', | ||
| 87 | ); | ||
| 88 | |||
| 89 | has 'password' => ( | ||
| 90 | is => 'ro', | ||
| 91 | isa => 'Str', | ||
| 92 | default => '', | ||
| 93 | ); | ||
| 94 | |||
| 95 | has 'debug' => ( | ||
| 96 | is => 'rw', | ||
| 97 | isa => 'Bool', | ||
| 98 | default => 0, | ||
| 99 | ); | ||
| 100 | |||
| 101 | has 'multiple' => ( | ||
| 102 | is => 'ro', | ||
| 103 | isa => 'Bool', | ||
| 104 | default => 0, | ||
| 105 | writer => '_set_multiple', | ||
| 106 | ); | ||
| 107 | |||
| 108 | has 'db_opts' => ( | ||
| 109 | is => 'ro', | ||
| 110 | isa => 'HashRef', | ||
| 111 | default => sub {{}}, | ||
| 112 | ); | ||
| 113 | |||
| 114 | has 'no_relay' => ( | ||
| 115 | is => 'rw', | ||
| 116 | isa => 'Bool', | ||
| 117 | default => 0, | ||
| 118 | trigger => sub { | ||
| 119 | my( $self, $new, $old ) = @_; | ||
| 120 | return if ! $self->_has_easydbi; | ||
| 121 | $self->yield( '_process_queue' ) if ! $new; | ||
| 122 | }, | ||
| 123 | ); | ||
| 124 | |||
| 125 | has 'no_curl' => ( | ||
| 126 | is => 'ro', | ||
| 127 | isa => 'Bool', | ||
| 128 | default => 0, | ||
| 129 | ); | ||
| 130 | |||
| 131 | has 'submissions' => ( | ||
| 132 | is => 'rw', | ||
| 133 | isa => 'Int', | ||
| 134 | default => 10, | ||
| 135 | ); | ||
| 136 | |||
| 137 | has '_uuid' => ( | ||
| 138 | is => 'ro', | ||
| 139 | isa => 'Data::UUID', | ||
| 140 | lazy_build => 1, | ||
| 141 | init_arg => undef, | ||
| 142 | ); | ||
| 143 | |||
| 144 | has '_easydbi' => ( | ||
| 145 | is => 'ro', | ||
| 146 | isa => 'POE::Component::EasyDBI', | ||
| 147 | lazy_build => 1, | ||
| 148 | init_arg => undef, | ||
| 149 | ); | ||
| 150 | |||
| 151 | has _http_alias => ( | ||
| 152 | is => 'ro', | ||
| 153 | isa => 'Str', | ||
| 154 | init_arg => undef, | ||
| 155 | writer => '_set_http_alias', | ||
| 156 | ); | ||
| 157 | |||
| 158 | has _resolver => ( | ||
| 159 | is => 'ro', | ||
| 160 | isa => 'POE::Component::Resolver', | ||
| 161 | init_arg => undef, | ||
| 162 | writer => '_set_resolver', | ||
| 163 | ); | ||
| 164 | |||
| 165 | has '_processing' => ( | ||
| 166 | is => 'ro', | ||
| 167 | isa => 'HashRef', | ||
| 168 | default => sub {{}}, | ||
| 169 | ); | ||
| 170 | |||
| 171 | sub _build__easydbi { | ||
| 172 | my $self = shift; | ||
| 173 | POE::Component::EasyDBI->new( | ||
| 174 | alias => '', | ||
| 175 | dsn => $self->dsn, | ||
| 176 | username => $self->username, | ||
| 177 | password => $self->password, | ||
| 178 | ( _HASH( $self->db_opts ) ? ( options => $self->db_opts ) : () ), | ||
| 179 | ); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub _build__uuid { | ||
| 183 | Data::UUID->new(); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub spawn { | ||
| 187 | shift->new(@_); | ||
| 188 | } | ||
| 189 | |||
| 190 | sub START { | ||
| 191 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 192 | $self->_build_table; | ||
| 193 | $kernel->yield( 'do_vacuum', 'process' ); | ||
| 194 | if ( !$self->no_curl && can_load( modules => { 'POE::Component::Curl::Multi' => '0.08' } ) ) { | ||
| 195 | $self->_set_multiple( 0 ); | ||
| 196 | $self->_set_http_alias( join '-', __PACKAGE__, $self->get_session_id ); | ||
| 197 | POE::Component::Curl::Multi->spawn( | ||
| 198 | Alias => $self->_http_alias, | ||
| 199 | FollowRedirects => 2, | ||
| 200 | ); | ||
| 201 | } | ||
| 202 | elsif ( $self->multiple ) { | ||
| 203 | $self->_set_resolver( BINGOS::POE::Component::Resolver->new() ); | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | $self->_set_http_alias( join '-', __PACKAGE__, $self->get_session_id ); | ||
| 207 | POE::Component::Client::HTTP->spawn( | ||
| 208 | Alias => $self->_http_alias, | ||
| 209 | FollowRedirects => 2, | ||
| 210 | ); | ||
| 211 | } | ||
| 212 | $kernel->yield( '_process_queue' ) if ! $self->no_relay; | ||
| 213 | return; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | sub _build_table { | ||
| 218 | my $self = shift; | ||
| 219 | |||
| 220 | $self->_easydbi; | ||
| 221 | |||
| 222 | if ( $self->dsn =~ /^dbi\:SQLite/i ) { | ||
| 223 | $self->_easydbi->do( | ||
| 224 | sql => 'PRAGMA synchronous = OFF', | ||
| 225 | event => '_generic_db_result', | ||
| 226 | _ts => $self->_time, | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | |||
| 230 | $self->_easydbi->do( | ||
| 231 | sql => $sql->{create}, | ||
| 232 | event => '_generic_db_result', | ||
| 233 | _ts => $self->_time, | ||
| 234 | ); | ||
| 235 | |||
| 236 | foreach my $idx ( @{ $sql->{addidx} } ) { | ||
| 237 | $self->_easydbi->do( | ||
| 238 | sql => $idx, | ||
| 239 | event => '_generic_db_result', | ||
| 240 | _ts => $self->_time, | ||
| 241 | ); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | event 'do_vacuum' => sub { | ||
| 246 | my ($kernel,$self,$process) = @_[KERNEL,OBJECT,ARG0]; | ||
| 247 | $self->_easydbi->do( | ||
| 248 | sql => 'VACUUM', | ||
| 249 | event => '_generic_db_result', | ||
| 250 | _ts => $self->_time, | ||
| 251 | ( $process ? ( _process => 1 ) : () ), | ||
| 252 | ); | ||
| 253 | |||
| 254 | $kernel->delay( 'do_vacuum' => DELAY * 60 ); | ||
| 255 | return; | ||
| 256 | }; | ||
| 257 | |||
| 258 | event 'shutdown' => sub { | ||
| 259 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 260 | $kernel->alarm_remove_all(); | ||
| 261 | $kernel->post( $self->_easydbi->ID, 'shutdown' ); | ||
| 262 | $kernel->post( | ||
| 263 | $self->_http_alias, | ||
| 264 | 'shutdown', | ||
| 265 | ); | ||
| 266 | $self->_resolver->_really_shutdown | ||
| 267 | if $self->multiple && $self->_resolver; | ||
| 268 | return; | ||
| 269 | }; | ||
| 270 | |||
| 271 | event '_generic_db_result' => sub { | ||
| 272 | my ($kernel,$self,$result) = @_[KERNEL,OBJECT,ARG0]; | ||
| 273 | $result->{dsn} = $self->dsn; | ||
| 274 | if ( $result->{error} ) { | ||
| 275 | warn "DB error (" . ( $self->_time - $result->{_ts} ) . "s): " . JSON->new->pretty(1)->encode( $result ) . "\n" if $self->debug; | ||
| 276 | } | ||
| 277 | $kernel->yield( '_process_queue' ) if $result->{_process}; | ||
| 278 | return; | ||
| 279 | }; | ||
| 280 | |||
| 281 | event 'submit' => sub { | ||
| 282 | my ($kernel,$self,$fact) = @_[KERNEL,OBJECT,ARG0]; | ||
| 283 | return unless $fact and $fact->isa('Metabase::Fact'); | ||
| 284 | my $timestamp = $self->_time; | ||
| 285 | $kernel->yield( '_dispatch_event', 'enqueued', $fact ); | ||
| 286 | $self->_easydbi->do( | ||
| 287 | sql => $sql->{insert}, | ||
| 288 | event => '_generic_db_result', | ||
| 289 | placeholders => [ $self->_uuid->create_b64(), $timestamp, 0, $self->_encode_fact( $fact ) ], | ||
| 290 | ( $self->no_relay ? () : ( _process => 1 ) ), | ||
| 291 | _ts => $timestamp, | ||
| 292 | ); | ||
| 293 | return; | ||
| 294 | }; | ||
| 295 | |||
| 296 | event '_process_queue' => sub { | ||
| 297 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 298 | return if $self->no_relay; | ||
| 299 | # Processing event | ||
| 300 | $kernel->delay( '_process_queue', DELAY ); | ||
| 301 | $self->_easydbi->arrayhash( | ||
| 302 | sql => $sql->{queue} . ( $self->multiple ? $self->submissions : 1 ), | ||
| 303 | event => '_queue_db_result', | ||
| 304 | _ts => $self->_time, | ||
| 305 | ); | ||
| 306 | return; | ||
| 307 | }; | ||
| 308 | |||
| 309 | event '_queue_db_result' => sub { | ||
| 310 | my ($kernel,$self,$result) = @_[KERNEL,OBJECT,ARG0]; | ||
| 311 | if ( $result->{error} and $self->debug ) { | ||
| 312 | warn $result->{error}, "\n"; | ||
| 313 | return; | ||
| 314 | } | ||
| 315 | foreach my $row ( @{ $result->{result} } ) { | ||
| 316 | # Have we seen this report before? | ||
| 317 | if ( exists $self->_processing->{ $row->{id} } ) { | ||
| 318 | next; | ||
| 319 | } else { | ||
| 320 | $self->_processing->{ $row->{id} }++; | ||
| 321 | } | ||
| 322 | |||
| 323 | # Submit event ? | ||
| 324 | my $report = $self->_decode_fact( $row->{data} ); | ||
| 325 | POE::Component::Metabase::Client::Submit->submit( | ||
| 326 | event => '_submit_status', | ||
| 327 | profile => $self->profile, | ||
| 328 | secret => $self->secret, | ||
| 329 | fact => $report, | ||
| 330 | uri => $self->uri->as_string, | ||
| 331 | context => [ $row->{id}, $row->{attempts}, $self->_time ], | ||
| 332 | ( $self->multiple ? ( resolver => $self->_resolver ) : ( http_alias => $self->_http_alias ) ), | ||
| 333 | ); | ||
| 334 | |||
| 335 | } | ||
| 336 | return; | ||
| 337 | }; | ||
| 338 | |||
| 339 | event '_dispatch_event' => sub { | ||
| 340 | my ($kernel,$self,@args) = @_[KERNEL,OBJECT,ARG0..$#_]; | ||
| 341 | return unless $self->session and $self->event; | ||
| 342 | $kernel->post( $self->session, $self->event, @args ); | ||
| 343 | return; | ||
| 344 | }; | ||
| 345 | |||
| 346 | event '_clear_processing' => sub { | ||
| 347 | my($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0]; | ||
| 348 | delete $self->_processing->{ $id } if exists | ||
| 349 | $self->_processing->{ $id }; | ||
| 350 | |||
| 351 | return; | ||
| 352 | }; | ||
| 353 | |||
| 354 | event '_submit_status' => sub { | ||
| 355 | my ($kernel,$self,$res) = @_[KERNEL,OBJECT,ARG0]; | ||
| 356 | my ($id,$attempts,$starttime) = @{ $res->{context} }; | ||
| 357 | my $timestamp = $self->_time; | ||
| 358 | $kernel->delay_set( '_clear_processing' => DELAY, $id ); | ||
| 359 | if ( $res->{success} ) { | ||
| 360 | # Success event | ||
| 361 | warn "Submit '$id' (" . ( $timestamp - $starttime ) . "s) success\n" if $self->debug; | ||
| 362 | $self->_easydbi->do( | ||
| 363 | sql => $sql->{delete}, | ||
| 364 | event => '_generic_db_result', | ||
| 365 | placeholders => [ $id ], | ||
| 366 | ( $self->no_relay ? () : ( _process => 1 ) ), | ||
| 367 | _ts => $timestamp, | ||
| 368 | ); | ||
| 369 | } | ||
| 370 | else { | ||
| 371 | warn "Submit '$id' (" . ( $timestamp - $starttime ) . "s) error: $res->{error}\n" . ( defined $res->{content} ? "$res->{content}\n" : '' ) if $self->debug; | ||
| 372 | if ( defined $res->{content} and $res->{content} =~ /GUID conflicts with an existing object/i ) { | ||
| 373 | # Duplicate event | ||
| 374 | $self->_easydbi->do( | ||
| 375 | sql => $sql->{delete}, | ||
| 376 | event => '_generic_db_result', | ||
| 377 | placeholders => [ $id ], | ||
| 378 | _ts => $timestamp, | ||
| 379 | ); | ||
| 380 | } | ||
| 381 | else { | ||
| 382 | # Re-enqueue event | ||
| 383 | $attempts++; | ||
| 384 | $self->_easydbi->do( | ||
| 385 | sql => $sql->{update}, | ||
| 386 | event => '_generic_db_result', | ||
| 387 | placeholders => [ $attempts, $id ], | ||
| 388 | _ts => $timestamp, | ||
| 389 | ); | ||
| 390 | } | ||
| 391 | } | ||
| 392 | return; | ||
| 393 | }; | ||
| 394 | |||
| 395 | sub _time { | ||
| 396 | return Time::HiRes::time; | ||
| 397 | } | ||
| 398 | |||
| 399 | sub _encode_fact { | ||
| 400 | my $self = shift; | ||
| 401 | return JSON->new->encode( shift->as_struct ); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub _decode_fact { | ||
| 405 | my $self = shift; | ||
| 406 | return CPAN::Testers::Report->from_struct( JSON->new->decode( shift ) ); | ||
| 407 | } | ||
| 408 | |||
| 409 | no MooseX::POE; | ||
| 410 | |||
| 411 | __PACKAGE__->meta->make_immutable; | ||
| 412 | |||
| 413 | package | ||
| 414 | BINGOS::POE::Component::Resolver; | ||
| 415 | |||
| 416 | use base qw[POE::Component::Resolver]; | ||
| 417 | |||
| 418 | #noop | ||
| 419 | sub shutdown { | ||
| 420 | return; | ||
| 421 | } | ||
| 422 | |||
| 423 | sub _really_shutdown { | ||
| 424 | shift->SUPER::shutdown; | ||
| 425 | } | ||
| 426 | |||
| 427 | 1; | ||
| 428 | |||
| 429 | __END__ | ||