| 1 | package Test2::Harness::UI::Importer; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Carp qw/croak/; | ||
| 8 | |||
| 9 | use Test2::Harness::UI::RunProcessor; | ||
| 10 | |||
| 11 | use Test2::Harness::UI::Util::HashBase qw/-config -worker_id/; | ||
| 12 | |||
| 13 | use Test2::Harness::UI::UUID qw/gen_uuid/; | ||
| 14 | use Test2::Harness::Util::JSON qw/decode_json/; | ||
| 15 | |||
| 16 | use IO::Uncompress::Bunzip2 qw($Bunzip2Error); | ||
| 17 | use IO::Uncompress::Gunzip qw($GunzipError); | ||
| 18 | |||
| 19 | sub init { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | croak "'config' is a required attribute" | ||
| 23 | unless $self->{+CONFIG}; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub run { | ||
| 27 | my $self = shift; | ||
| 28 | my ($max) = @_; | ||
| 29 | |||
| 30 | my $schema = $self->{+CONFIG}->schema; | ||
| 31 | |||
| 32 | my $worker_id = $self->{+WORKER_ID} //= gen_uuid(); | ||
| 33 | |||
| 34 | while (!defined($max) || $max--) { | ||
| 35 | $schema->resultset('Run')->search( | ||
| 36 | {status => 'pending', log_file_id => {'is not' => undef}}, | ||
| 37 | {order_by => {-asc => 'added'}, rows => 1}, | ||
| 38 | )->update({status => 'running', worker_id => $worker_id->{string}}); | ||
| 39 | |||
| 40 | my $run = $schema->resultset('Run')->search( | ||
| 41 | {status => 'running', worker_id => $worker_id->{string}}, | ||
| 42 | {order_by => {-asc => 'added'}, rows => 1}, | ||
| 43 | )->first; | ||
| 44 | |||
| 45 | unless ($run) { | ||
| 46 | sleep 1; | ||
| 47 | next; | ||
| 48 | } | ||
| 49 | |||
| 50 | $self->process($run); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub process { | ||
| 55 | my $self = shift; | ||
| 56 | my ($run) = @_; | ||
| 57 | |||
| 58 | my $start = time; | ||
| 59 | syswrite(\*STDOUT, '[' . $run->worker_id . "] Starting run " . $run->run_id . " (" . $run->log_file->name . ")\n"); | ||
| 60 | |||
| 61 | my $status; | ||
| 62 | my $ok = eval { $status = $self->process_log($run); 1 }; | ||
| 63 | my $err = $@; | ||
| 64 | |||
| 65 | my $total = time - $start; | ||
| 66 | |||
| 67 | if ($ok && !$status->{errors}) { | ||
| 68 | syswrite(\*STDOUT, "Completed run " . $run->run_id . " (" . $run->log_file->name . ") in $total seconds.\n"); | ||
| 69 | $run->update({status => 'complete', passed => $status->{passed}, failed => $status->{failed}, retried => $status->{retried}}); | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | my $error = $ok ? join("\n" => @{$status->{errors}}) : $err; | ||
| 73 | syswrite(\*STDOUT, "Failed feed " . $run->run_id . " (" . $run->log_file->name . ") in $total seconds.\n$error\n"); | ||
| 74 | $run->update({status => 'broken', error => $error}); | ||
| 75 | } | ||
| 76 | |||
| 77 | return; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub process_log { | ||
| 81 | my $self = shift; | ||
| 82 | my ($run, $fh) = @_; | ||
| 83 | |||
| 84 | unless ($fh) { | ||
| 85 | my $log = $run->log_file or die "No log file"; | ||
| 86 | if ($log->name =~ m/\.bz2$/) { | ||
| 87 | $fh = IO::Uncompress::Bunzip2->new($log->local_file || \($log->data)) or die "Could not open bz2 data: $Bunzip2Error"; | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | $fh = IO::Uncompress::Gunzip->new($log->local_file || \($log->data)) or die "Could not open gz data: $GunzipError"; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | my $processor = Test2::Harness::UI::RunProcessor->new( | ||
| 95 | run => $run, | ||
| 96 | config => $self->{+CONFIG}, | ||
| 97 | buffer => 1, | ||
| 98 | ); | ||
| 99 | |||
| 100 | $processor->start(); | ||
| 101 | |||
| 102 | my $schema = $self->{+CONFIG}->schema; | ||
| 103 | |||
| 104 | local $| = 1; | ||
| 105 | while (my $line = <$fh>) { | ||
| 106 | next if $line =~ m/^null$/ims; | ||
| 107 | my $ln = $.; | ||
| 108 | |||
| 109 | my $error = $self->process_event_json($processor, $ln => $line); | ||
| 110 | |||
| 111 | return {errors => ["error processing line number $ln: $error"]} if $error; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $status = $processor->finish(); | ||
| 115 | return $status; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub process_event_json { | ||
| 119 | my $self = shift; | ||
| 120 | my ($processor, $ln, $json) = @_; | ||
| 121 | |||
| 122 | my $ok = eval { | ||
| 123 | my $event = decode_json($json); | ||
| 124 | $processor->process_event($event, undef, line => $ln); | ||
| 125 | 1; | ||
| 126 | }; | ||
| 127 | my $err = $@; | ||
| 128 | return $ok ? undef : $err; | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; | ||
| 132 | |||
| 133 | __END__ |