| 1 | package ETLp; | ||
| 2 | |||
| 3 | 0 | ▶ | use MooseX::Declare; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ETLp - A framework for managing and auditing ETL processing | ||
| 8 | |||
| 9 | =head1 END-USER DOCUMENTATION | ||
| 10 | |||
| 11 | For end-user documentation on how to use ETLp refer to | ||
| 12 | L<http://trac.assembla.com/etlp/wiki> or: | ||
| 13 | |||
| 14 | perldoc ETLp::Manual::Intro | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | Version 0.04 | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | This module manages the processing of ETL tasks as a series of pipelines. | ||
| 23 | The tasks are defined ina one or more configuration files. ETLp is invoked | ||
| 24 | by providing the details of the pipeline top be executed. | ||
| 25 | |||
| 26 | use ETLp; | ||
| 27 | |||
| 28 | my $etlp = ETLp->new( | ||
| 29 | config_directory => "$ENV{'HOME'}/conf", | ||
| 30 | app_config_file => 'sales', | ||
| 31 | section => 'monthly_sales', | ||
| 32 | env_config_file => 'env', | ||
| 33 | ); | ||
| 34 | |||
| 35 | $etlp->run(); | ||
| 36 | ... | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | =head2 new | ||
| 41 | |||
| 42 | =head3 parameters | ||
| 43 | |||
| 44 | * app_config_file. The name of the application configuration file | ||
| 45 | * env_config_file. The name of the environment configuration file | ||
| 46 | * config_directrory. Optional. The name of the configuration file | ||
| 47 | that contains the configuration files | ||
| 48 | * section. The name of the section in the application configuration | ||
| 49 | that defined the job being executed | ||
| 50 | * localize. Optional. Whether to modify the configuration files to | ||
| 51 | use the operating system eol markers. Deafults to 0. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | class ETLp with ETLp::Role::Config { | ||
| 56 | use FindBin qw($Bin); | ||
| 57 | use Config::General qw(ParseConfig); | ||
| 58 | use ETLp::Exception; | ||
| 59 | use Try::Tiny; | ||
| 60 | use Modern::Perl; | ||
| 61 | use ETLp::Config; | ||
| 62 | use ETLp::Schema; | ||
| 63 | use ETLp::Audit::Job; | ||
| 64 | use ETLp::ItemBuilder; | ||
| 65 | use ETLp::Execute::Iteration; | ||
| 66 | use ETLp::Execute::Serial; | ||
| 67 | use Cwd 'abs_path'; | ||
| 68 | use DBI; | ||
| 69 | use Log::Log4perl qw(); | ||
| 70 | use Data::Dumper; | ||
| 71 | use UNIVERSAL::require; | ||
| 72 | use File::LocalizeNewlines; | ||
| 73 | use DBI::Const::GetInfoType; | ||
| 74 | |||
| 75 | our $VERSION = '0.04'; | ||
| 76 | |||
| 77 | has 'app_config_file' => (is => 'ro', isa => 'Str', required => 1); | ||
| 78 | has 'env_config_file' => (is => 'ro', isa => 'Str', required => 1); | ||
| 79 | has 'section' => (is => 'ro', isa => 'Str', required => 1); | ||
| 80 | has 'config_directory' => (is => 'ro', isa => 'Str', required => 0); | ||
| 81 | has 'localize' => (is => 'ro', isa => 'Bool', required => 0, default => 0); | ||
| 82 | |||
| 83 | has 'registered_plugins' => ( | ||
| 84 | is => 'rw', | ||
| 85 | isa => 'HashRef', | ||
| 86 | required => 0, | ||
| 87 | lazy => 1, | ||
| 88 | default => sub { {} } | ||
| 89 | ); | ||
| 90 | |||
| 91 | has 'env_conf' => ( | ||
| 92 | is => 'rw', | ||
| 93 | isa => 'HashRef', | ||
| 94 | required => 0, | ||
| 95 | lazy => 1, | ||
| 96 | default => sub { {} } | ||
| 97 | ); | ||
| 98 | |||
| 99 | has 'config' => ( | ||
| 100 | is => 'rw', | ||
| 101 | isa => 'HashRef', | ||
| 102 | required => 0, | ||
| 103 | lazy => 1, | ||
| 104 | default => sub { {} } | ||
| 105 | ); | ||
| 106 | |||
| 107 | has 'app_root' => ( | ||
| 108 | is => 'rw', | ||
| 109 | isa => 'Str', | ||
| 110 | required => 0, | ||
| 111 | default => abs_path("$Bin/..") | ||
| 112 | ); | ||
| 113 | has 'log_dir' => ( | ||
| 114 | is => 'ro', | ||
| 115 | isa => 'Str', | ||
| 116 | required => 0, | ||
| 117 | default => abs_path("$Bin/../log") | ||
| 118 | ); | ||
| 119 | has 'config_directory' => ( | ||
| 120 | is => 'ro', | ||
| 121 | isa => 'Str', | ||
| 122 | required => 0, | ||
| 123 | default => abs_path("$Bin/../conf") | ||
| 124 | ); | ||
| 125 | |||
| 126 | # Build the application pipeline | ||
| 127 | method _build_pipeline { | ||
| 128 | # Only include the environment configuration if allowed to - otherwise | ||
| 129 | # we may leak sensitive information to plugin writers | ||
| 130 | my $env_conf = ($self->env_conf->{allow_env_vars}) ? | ||
| 131 | $self->env_conf : | ||
| 132 | {}; | ||
| 133 | |||
| 134 | my $item_builder = ETLp::ItemBuilder->new( | ||
| 135 | plugins => $self->registered_plugins, | ||
| 136 | pipeline_type => $self->config->{type}, | ||
| 137 | allow_env_vars => $self->env_conf->{allow_env_vars}, | ||
| 138 | env_conf => $env_conf, | ||
| 139 | app_root => $self->app_root, | ||
| 140 | config => $self->config, | ||
| 141 | ); | ||
| 142 | |||
| 143 | my $pipeline = $item_builder->build_pipeline; | ||
| 144 | |||
| 145 | return $pipeline; | ||
| 146 | } | ||
| 147 | |||
| 148 | # parse the application configuration file | ||
| 149 | method _build_app_config(Str $app_config_file, Str $section) { | ||
| 150 | ETLpException->throw(error => | ||
| 151 | "No such application configuration file $app_config_file") | ||
| 152 | unless -f $app_config_file; | ||
| 153 | |||
| 154 | if ($self->localize) { | ||
| 155 | my $localize = File::LocalizeNewlines->new; | ||
| 156 | my $num_localized = $localize->localize($app_config_file); | ||
| 157 | |||
| 158 | my $label; | ||
| 159 | } | ||
| 160 | |||
| 161 | my %config; | ||
| 162 | |||
| 163 | try { | ||
| 164 | %config = ParseConfig(-ConfigFile => $app_config_file); | ||
| 165 | } | ||
| 166 | catch { | ||
| 167 | ETLpException->throw(error => "Cannot parse $app_config_file: $_"); | ||
| 168 | }; | ||
| 169 | |||
| 170 | my $job = $config{$section}; | ||
| 171 | |||
| 172 | # Make sure that the section exists in the configuration | ||
| 173 | ETLpException->throw(error => "No section " . $section . | ||
| 174 | " in " . $app_config_file) unless ref($job) eq 'HASH'; | ||
| 175 | |||
| 176 | # Each job must have a type | ||
| 177 | ETLpException->throw(error => "No type for " . $self->section) | ||
| 178 | unless $job->{type}; | ||
| 179 | |||
| 180 | # The types must be 'iteration' or 'serial' | ||
| 181 | ETLpException->throw(error => "Invalid type " . $job->{type}) | ||
| 182 | unless (($job->{type} eq 'iterative') || ($job->{type} eq 'serial')); | ||
| 183 | |||
| 184 | # Update relative directories if required. | ||
| 185 | if (defined $job->{config}) { | ||
| 186 | foreach my $dir (qw/incoming_dir fail_dir archive_dir | ||
| 187 | controlfile_dir/) { | ||
| 188 | if (defined $job->{config}->{$dir}) { | ||
| 189 | if (substr($job->{config}->{$dir}, 0, 1) ne '/') { | ||
| 190 | $job->{config}->{$dir} = $self->app_root . '/' . | ||
| 191 | $job->{config}->{$dir} | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | # All the items within a phase should be an arrayref of hashrefs. This | ||
| 198 | # won't be the case if there is only a single item, so make sure | ||
| 199 | # items are within an arrayref | ||
| 200 | foreach my $phase (qw/pre_process process post_process/) { | ||
| 201 | if (exists $job->{$phase}) { | ||
| 202 | if (defined $job->{$phase}->{item}) { | ||
| 203 | if (ref $job->{$phase}->{item} eq 'HASH') { | ||
| 204 | $job->{$phase}->{item} = [$job->{$phase}->{item}]; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | # Store the configuration | ||
| 211 | return $job; | ||
| 212 | } | ||
| 213 | |||
| 214 | # parse the environment configuration file | ||
| 215 | method _parse_env_conf(Str $env_config) { | ||
| 216 | my %conf; | ||
| 217 | try { | ||
| 218 | if ($self->localize) { | ||
| 219 | my $localize = File::LocalizeNewlines->new; | ||
| 220 | my $num_localized = $localize->localize($env_config); | ||
| 221 | } | ||
| 222 | |||
| 223 | %conf = ParseConfig(-ConfigFile => $env_config,); | ||
| 224 | } | ||
| 225 | catch { | ||
| 226 | ETLpException->throw(error => "Cannot parse $env_config: $_"); | ||
| 227 | }; | ||
| 228 | |||
| 229 | return \%conf; | ||
| 230 | } | ||
| 231 | |||
| 232 | # Create a connection to the database | ||
| 233 | method _create_dbh { | ||
| 234 | my $env = $self->env_conf; | ||
| 235 | my $dbh; | ||
| 236 | |||
| 237 | try { | ||
| 238 | $dbh = | ||
| 239 | DBI->connect($env->{'dsn'}, $env->{user}, $env->{password}, | ||
| 240 | {RaiseError => 1, AutoCommit => 1, PrintError => 0}); | ||
| 241 | } | ||
| 242 | catch { | ||
| 243 | ETLpException->throw(error => "Unable to connect to database: $_"); | ||
| 244 | }; | ||
| 245 | |||
| 246 | return $dbh; | ||
| 247 | } | ||
| 248 | |||
| 249 | # Create a Log4perl logger. | ||
| 250 | method _create_logger() { | ||
| 251 | my $log_dir = $self->log_dir; | ||
| 252 | my $name = $self->app_config_file . '_' . $self->section; | ||
| 253 | unless (-d $log_dir) { | ||
| 254 | ETLpException->throw(error => "No such log directory $log_dir"); | ||
| 255 | } | ||
| 256 | |||
| 257 | my $layout_pattern = $self->env_conf->{logger_layout_pattern} | ||
| 258 | || '%d %l %p> %m%n'; | ||
| 259 | my $logger_level = $self->env_conf->{logger_level} || 'DEBUG'; | ||
| 260 | my $admin_email = $self->env_conf->{admin_email}; | ||
| 261 | my $email_sender = $self->env_conf->{email_sender}; | ||
| 262 | my $environment = $self->env_conf->{environment} | ||
| 263 | || '<unkown environment>'; | ||
| 264 | my $log_conf; | ||
| 265 | |||
| 266 | $admin_email =~ s/@/\@/ if $admin_email; | ||
| 267 | $email_sender =~ s/@/\@/ if $email_sender; | ||
| 268 | |||
| 269 | if ($admin_email && $email_sender) { | ||
| 270 | $log_conf = qq{ | ||
| 271 | log4perl.rootLogger=$logger_level,LOGFILE,Mailer | ||
| 272 | }; | ||
| 273 | } else { | ||
| 274 | $log_conf = qq{ | ||
| 275 | log4perl.rootLogger=$logger_level,LOGFILE | ||
| 276 | }; | ||
| 277 | } | ||
| 278 | |||
| 279 | $log_conf .= qq! | ||
| 280 | log4perl.appender.LOGFILE=Log::Dispatch::FileRotate | ||
| 281 | log4perl.appender.LOGFILE.filename = $log_dir/${name}.log | ||
| 282 | log4perl.appender.LOGFILE.mode=append | ||
| 283 | log4perl.appender.LOGFILE.max = 5 | ||
| 284 | log4perl.appender.LOGFILE.size = 10000000 | ||
| 285 | |||
| 286 | log4perl.appender.LOGFILE.layout = PatternLayout | ||
| 287 | log4perl.appender.LOGFILE.layout.ConversionPattern = $layout_pattern | ||
| 288 | !; | ||
| 289 | |||
| 290 | if ($admin_email && $email_sender) { | ||
| 291 | $log_conf .= qq{ | ||
| 292 | log4perl.appender.Mailer = Log::Dispatch::Email::MailSendmail | ||
| 293 | log4perl.appender.Mailer.from = $email_sender | ||
| 294 | log4perl.appender.Mailer.to = $admin_email | ||
| 295 | log4perl.appender.Mailer.subject = [$environment] ETLp Error: $name | ||
| 296 | log4perl.appender.Mailer.layout = SimpleLayout | ||
| 297 | log4perl.appender.Mailer.Threshold = WARN | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | Log::Log4perl::init(\$log_conf); | ||
| 302 | my $logger = Log::Log4perl::get_logger("DW_${name}"); | ||
| 303 | |||
| 304 | return $logger; | ||
| 305 | |||
| 306 | } | ||
| 307 | |||
| 308 | # Register plugins for processing items | ||
| 309 | method _register_plugins(Str $job_type) { | ||
| 310 | my $plugin = 'Module::Pluggable'; | ||
| 311 | my @plugin_ns; | ||
| 312 | |||
| 313 | # A job must be either serial or iterative (and is validated earlier | ||
| 314 | # during the build process) | ||
| 315 | if ($job_type eq 'serial') { | ||
| 316 | push @plugin_ns, 'ETLp::Plugin::Serial'; | ||
| 317 | push @plugin_ns, $self->env_conf->{'serial_plugin_ns'} | ||
| 318 | if defined $self->env_conf->{'serial_plugin_ns'}; | ||
| 319 | } else { | ||
| 320 | push @plugin_ns, 'ETLp::Plugin::Iterative'; | ||
| 321 | push @plugin_ns, $self->env_conf->{'iterative_plugin_ns'} | ||
| 322 | if defined $self->env_conf->{'iterative_plugin_ns'}; | ||
| 323 | } | ||
| 324 | |||
| 325 | # import all of the plugins | ||
| 326 | my $module = "Module::Pluggable"; | ||
| 327 | $module->require; | ||
| 328 | $module->import(search_path => \@plugin_ns, require => 1); | ||
| 329 | |||
| 330 | my %plugin_type; | ||
| 331 | |||
| 332 | # register each plugin | ||
| 333 | foreach my $plugin ($self->plugins) { | ||
| 334 | my $class = $plugin->meta->name; | ||
| 335 | my $type; | ||
| 336 | |||
| 337 | unless ($plugin->can('type')) { | ||
| 338 | ETLpException->throw(error => "plugin $class has no type"); | ||
| 339 | } | ||
| 340 | $type = $plugin->type; | ||
| 341 | |||
| 342 | if (exists $plugin_type{$type}) { | ||
| 343 | ETLpException->throw(error => | ||
| 344 | "cannot add plugin $class because $type ". | ||
| 345 | "is already managed by " . $plugin_type{$type}); | ||
| 346 | } | ||
| 347 | |||
| 348 | $plugin_type{$type} = $class; | ||
| 349 | } | ||
| 350 | |||
| 351 | return \%plugin_type; | ||
| 352 | } | ||
| 353 | |||
| 354 | =head2 run | ||
| 355 | |||
| 356 | Executes the pipeline | ||
| 357 | |||
| 358 | =head3 parameters | ||
| 359 | |||
| 360 | * pipeline - An arrayref of items to be executed | ||
| 361 | |||
| 362 | =head3 returns | ||
| 363 | |||
| 364 | * void | ||
| 365 | |||
| 366 | =cut | ||
| 367 | |||
| 368 | method run { | ||
| 369 | my $runner; | ||
| 370 | my $pipeline = $self->{pipeline}; | ||
| 371 | |||
| 372 | #ETLp::Config->schema->storage->dbh->{AutoCommit} = 1; | ||
| 373 | |||
| 374 | my $audit = ETLp::Audit::Job->new( | ||
| 375 | name => $self->app_config_file, | ||
| 376 | section => $self->section, | ||
| 377 | config => $self->config, | ||
| 378 | ); | ||
| 379 | |||
| 380 | # save the audit object to the coniguration | ||
| 381 | ETLp::Config->audit($audit); | ||
| 382 | |||
| 383 | my $audit_record = ETLp::Config->schema->resultset('EtlpJob')->find($audit->id); | ||
| 384 | |||
| 385 | ETLp::Config->logger->info("Config name: " .$audit_record->section->config->config_name); | ||
| 386 | #ETLp::Config->logger->info("AutoCommit: " .ETLp::Config->schema->storage->dbh->{AutoCommit}); | ||
| 387 | |||
| 388 | if ($self->config->{type} eq 'iterative') { | ||
| 389 | $runner = ETLp::Execute::Iteration->new( | ||
| 390 | pipeline => $pipeline, | ||
| 391 | config => $self->config, | ||
| 392 | ); | ||
| 393 | } else { | ||
| 394 | $runner = ETLp::Execute::Serial->new( | ||
| 395 | pipeline => $pipeline, | ||
| 396 | config => $self->config, | ||
| 397 | ); | ||
| 398 | } | ||
| 399 | |||
| 400 | ETLp::Config->logger->debug(Dumper $runner); | ||
| 401 | |||
| 402 | try { | ||
| 403 | $runner->run | ||
| 404 | } catch { | ||
| 405 | #return 0; | ||
| 406 | $_->rethrow if ref $_; | ||
| 407 | ETLpException->throw(error => $_); | ||
| 408 | }; | ||
| 409 | |||
| 410 | return 1; | ||
| 411 | } | ||
| 412 | |||
| 413 | method pipeline { | ||
| 414 | return $self->{pipeline}; | ||
| 415 | } | ||
| 416 | |||
| 417 | method BUILD { | ||
| 418 | my $app_config_file = $self->app_config_file; | ||
| 419 | my $env_config_file = $self->env_config_file; | ||
| 420 | |||
| 421 | $app_config_file .= '.conf' unless $app_config_file =~ /\.conf$/; | ||
| 422 | $env_config_file .= '.conf' unless $env_config_file =~ /\.conf$/; | ||
| 423 | |||
| 424 | if ($self->config_directory) { | ||
| 425 | $app_config_file = $self->config_directory . '/' . $app_config_file; | ||
| 426 | $env_config_file = $self->config_directory . '/' . $env_config_file; | ||
| 427 | } | ||
| 428 | |||
| 429 | my $app_config = | ||
| 430 | $self->_build_app_config($app_config_file, $self->section); | ||
| 431 | #$self->pipeline_type($app_config->{type}); | ||
| 432 | |||
| 433 | $self->env_conf($self->_parse_env_conf($env_config_file)); | ||
| 434 | |||
| 435 | my $logger = $self->_create_logger; | ||
| 436 | my $dbh = $self->_create_dbh; | ||
| 437 | my $schema_dbh = $self->_create_dbh; | ||
| 438 | |||
| 439 | if ($schema_dbh->get_info($GetInfoType{SQL_DBMS_NAME}) && | ||
| 440 | lc $schema_dbh->get_info($GetInfoType{SQL_DBMS_NAME}) eq 'oracle') { | ||
| 441 | $schema_dbh->{LongReadLen} = 1000000; | ||
| 442 | $schema_dbh->{LongTruncOk} = 1; | ||
| 443 | } | ||
| 444 | |||
| 445 | my $schema = ETLp::Schema->connect(sub { $schema_dbh }, | ||
| 446 | {on_connect_call => 'datetime_setup'}); | ||
| 447 | |||
| 448 | $logger->info("DBH's AutoCommit: " . $dbh->{AutoCommit}); | ||
| 449 | $schema->storage->dbh->{AutoCommit} = 1; | ||
| 450 | $dbh->{AutoCommit} = 0; | ||
| 451 | |||
| 452 | my $config = ETLp::Config->instance; | ||
| 453 | ETLp::Config->dbh($dbh); | ||
| 454 | ETLp::Config->logger($logger); | ||
| 455 | ETLp::Config->schema($schema); | ||
| 456 | $self->config($app_config); | ||
| 457 | |||
| 458 | $self->registered_plugins( | ||
| 459 | $self->_register_plugins($app_config->{type})); | ||
| 460 | |||
| 461 | $self->{pipeline} = $self->_build_pipeline(); | ||
| 462 | |||
| 463 | my $checkpoint; | ||
| 464 | |||
| 465 | }; | ||
| 466 | } | ||
| 467 | |||
| 468 | =head1 AUTHOR | ||
| 469 | |||
| 470 | Dan Horne, C<< <dan.horne at redbone.co.nz> >> | ||
| 471 | |||
| 472 | =head1 BUGS | ||
| 473 | |||
| 474 | Please report any bugs or feature requests through | ||
| 475 | the web interface at L<https://rt.cpan.org/Public/Dist/Display.html?Name=ETLp>. | ||
| 476 | |||
| 477 | =head1 SUPPORT | ||
| 478 | |||
| 479 | You can find documentation for this module with the perldoc command. | ||
| 480 | |||
| 481 | perldoc ETLp | ||
| 482 | |||
| 483 | You can also look for information at: | ||
| 484 | |||
| 485 | =over 4 | ||
| 486 | |||
| 487 | =item Project Home Page | ||
| 488 | |||
| 489 | L<http://trac.assembla.com/etlp/wiki/> | ||
| 490 | |||
| 491 | =item * ETLp Tickets | ||
| 492 | |||
| 493 | Please add bug reports, feature requests | ||
| 494 | |||
| 495 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=ETLp> | ||
| 496 | |||
| 497 | =item * Browse Source Code | ||
| 498 | |||
| 499 | L<http://trac.assembla.com/etlp/browser> | ||
| 500 | |||
| 501 | =item * Check out the source | ||
| 502 | |||
| 503 | svn checkout http://subversion.assembla.com/svn/etlp/ etlp | ||
| 504 | |||
| 505 | =back | ||
| 506 | |||
| 507 | |||
| 508 | =head1 LICENSE AND COPYRIGHT | ||
| 509 | |||
| 510 | Copyright 2010 Redbone Systems Ltd | ||
| 511 | |||
| 512 | This program is free software; you can redistribute it and/or modify it | ||
| 513 | under the terms of either: the GNU General Public License as published | ||
| 514 | by the Free Software Foundation; or the Artistic License. | ||
| 515 | |||
| 516 | The terms are in the LICENSE file that accompanies this application | ||
| 517 | |||
| 518 | =cut | ||
| 519 | |||
| 520 | 1; # End of ETLp | ||