| 1 | package ETLp::Execute::Serial; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ETLp::Execute::Serial - Execute a Serial Pipeline | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Takes the array ref of Pipeline items and executes each item in turn | ||
| 12 | |||
| 13 | =head1 METHODS | ||
| 14 | |||
| 15 | =head2 new | ||
| 16 | |||
| 17 | =head3 parameters | ||
| 18 | |||
| 19 | All of the following parameters are mandatory | ||
| 20 | |||
| 21 | * pipeline. an arrayref pipeline items | ||
| 22 | * config. The application configuration | ||
| 23 | |||
| 24 | =head3 retuns | ||
| 25 | |||
| 26 | * void | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | class ETLp::Execute::Serial with ETLp::Role::Config { | ||
| 31 | use Try::Tiny; | ||
| 32 | use Carp; | ||
| 33 | use ETLp::Exception; | ||
| 34 | use Data::Dumper; | ||
| 35 | |||
| 36 | has 'pipeline' => (is => 'ro', isa => 'ArrayRef', required => 1); | ||
| 37 | has 'config' => (is => 'ro', isa => 'HashRef', required => 1); | ||
| 38 | |||
| 39 | method run { | ||
| 40 | my $warning = 0; | ||
| 41 | |||
| 42 | try { | ||
| 43 | ITEM_LOOP: foreach my $item (@{$self->pipeline}) { | ||
| 44 | $self->logger->debug("Item: " . Dumper($item)); | ||
| 45 | |||
| 46 | # Create an audit entry for the item | ||
| 47 | my $audit_item = $self->audit->create_item( | ||
| 48 | name => $item->{name}, | ||
| 49 | type => $item->{type}, | ||
| 50 | phase => $item->{phase}, | ||
| 51 | ); | ||
| 52 | |||
| 53 | # The action to be taken when an error is encountered | ||
| 54 | my $on_error = (defined $item->{item}->{on_error}) ? | ||
| 55 | $item->{item}->{on_error} : | ||
| 56 | $self->config->{config}->{on_error} || 'die'; | ||
| 57 | |||
| 58 | my $run_ok = 1; | ||
| 59 | # Execute the item | ||
| 60 | try { | ||
| 61 | my $res = $item->{runner}(); | ||
| 62 | } | ||
| 63 | catch { | ||
| 64 | # Yes, we encountered an error. Set the item status to | ||
| 65 | # failed | ||
| 66 | my $error = $_; | ||
| 67 | |||
| 68 | $audit_item->update_message(''.$error); | ||
| 69 | $audit_item->update_status('failed'); | ||
| 70 | |||
| 71 | if ($on_error ne 'ignore') { | ||
| 72 | $error->rethrow if ref $error; | ||
| 73 | ETLpException->throw(error => $error); | ||
| 74 | } | ||
| 75 | |||
| 76 | $self->logger->error(''.$error); | ||
| 77 | $run_ok = 0; | ||
| 78 | $warning = 1; | ||
| 79 | }; | ||
| 80 | |||
| 81 | $audit_item->update_status('succeeded') if $run_ok; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | catch { | ||
| 85 | my $error = $_; | ||
| 86 | $self->audit->update_message(''.$error); | ||
| 87 | $self->audit->update_status('failed'); | ||
| 88 | ETLpException->throw(error => ''.$error); | ||
| 89 | }; | ||
| 90 | |||
| 91 | # We may have a warning is we encountered an error, but the on_error | ||
| 92 | # flag was set to ignore. | ||
| 93 | if ($warning) { | ||
| 94 | $self->audit->update_status('warning'); | ||
| 95 | } else { | ||
| 96 | $self->audit->update_status('succeeded'); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | =head1 LICENSE AND COPYRIGHT | ||
| 102 | |||
| 103 | Copyright 2010 Redbone Systems Ltd | ||
| 104 | |||
| 105 | This program is free software; you can redistribute it and/or modify it | ||
| 106 | under the terms of either: the GNU General Public License as published | ||
| 107 | by the Free Software Foundation; or the Artistic License. | ||
| 108 | |||
| 109 | The terms are in the LICENSE file that accompanies this application | ||
| 110 |