| 1 | package ETLp::Audit::Job; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ETLp::Audit::Job - audit the execution of an ETLp job | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This class is used to manage the job's audit record | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use DateTime; | ||
| 16 | |||
| 17 | class ETLp::Audit::Job with (ETLp::Role::Config, ETLp::Role::Schema, | ||
| 18 | ETLp::Role::Audit) { | ||
| 19 | use Try::Tiny; | ||
| 20 | use DateTime; | ||
| 21 | use ETLp::Types; | ||
| 22 | use ETLp::Audit::Item; | ||
| 23 | |||
| 24 | =head1 ATTRIBUTES | ||
| 25 | |||
| 26 | The attributes are populated during instantiation, and should not be provided | ||
| 27 | at object creation time | ||
| 28 | |||
| 29 | =head2 id | ||
| 30 | |||
| 31 | The Job identifier | ||
| 32 | |||
| 33 | =head2 session_id | ||
| 34 | |||
| 35 | The database session associated with the job | ||
| 36 | |||
| 37 | =head2 item | ||
| 38 | |||
| 39 | The currently executing item | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | has 'session_id' => (is => 'rw', isa => 'Int', required => 0); | ||
| 44 | has 'id' => (is => 'rw', isa => 'Int', required => 0); | ||
| 45 | has 'name' => (is => 'ro', isa => 'Str', required => 1); | ||
| 46 | has 'section' => (is => 'ro', isa => 'Str', required => 1); | ||
| 47 | has 'config' => (is => 'ro', isa => 'HashRef', required => 1); | ||
| 48 | |||
| 49 | =head1 METHODS | ||
| 50 | |||
| 51 | =head2 create_item | ||
| 52 | |||
| 53 | Create an audit item | ||
| 54 | |||
| 55 | =head3 parameters (Hash) | ||
| 56 | |||
| 57 | * Str name: Name of the item | ||
| 58 | * Str type: The item type | ||
| 59 | * Str phase: The item phase | ||
| 60 | |||
| 61 | =head3 returns | ||
| 62 | |||
| 63 | * An ETLp::Audit::Item | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | method create_item(Str :$name, Str :$type, Str :$phase) { | ||
| 68 | my $item = ETLp::Audit::Item->new( | ||
| 69 | name => $name, | ||
| 70 | type => $type, | ||
| 71 | phase => $phase, | ||
| 72 | job => $self, | ||
| 73 | config => $self->config | ||
| 74 | ); | ||
| 75 | $self->{item} = $item; | ||
| 76 | return $item; | ||
| 77 | } | ||
| 78 | |||
| 79 | method item { | ||
| 80 | return $self->{item}; | ||
| 81 | } | ||
| 82 | |||
| 83 | method _start_audit { | ||
| 84 | my $now = $self->now; | ||
| 85 | try { | ||
| 86 | $self->schema->txn_do( | ||
| 87 | sub { | ||
| 88 | my $config = $self->EtlpConfiguration->find_or_create( | ||
| 89 | { | ||
| 90 | config_name => $self->name, | ||
| 91 | date_created => $now, | ||
| 92 | date_updated => $now | ||
| 93 | }, | ||
| 94 | {key => 'etlp_configuration_u1'} | ||
| 95 | ); | ||
| 96 | |||
| 97 | my $section = $self->EtlpSection->find_or_create( | ||
| 98 | { | ||
| 99 | config_id => $config->config_id, | ||
| 100 | section_name => $self->section, | ||
| 101 | date_created => $now, | ||
| 102 | date_updated => $now | ||
| 103 | }, | ||
| 104 | {key => 'etlp_section_u1'} | ||
| 105 | ); | ||
| 106 | |||
| 107 | my $job = $self->EtlpJob->create( | ||
| 108 | { | ||
| 109 | section_id => $section->section_id, | ||
| 110 | status_id => $self->get_status_id('running'), | ||
| 111 | session_id => $self->session_id, | ||
| 112 | process_id => $$, | ||
| 113 | date_created => $now, | ||
| 114 | date_updated => $now | ||
| 115 | } | ||
| 116 | ); | ||
| 117 | |||
| 118 | $self->id($job->job_id); | ||
| 119 | } | ||
| 120 | ) | ||
| 121 | |||
| 122 | } catch { | ||
| 123 | $self->logger->logdie("Cannot create job audit record: $_"); | ||
| 124 | }; | ||
| 125 | } | ||
| 126 | |||
| 127 | =head2 update_status | ||
| 128 | |||
| 129 | Update the job's status | ||
| 130 | |||
| 131 | =head3 parameters | ||
| 132 | |||
| 133 | * status_name: The name of the status | ||
| 134 | |||
| 135 | =head3 returns | ||
| 136 | |||
| 137 | * void | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | method update_status(Str $status_name) { | ||
| 142 | my $job = $self->EtlpJob->find($self->id); | ||
| 143 | $job->status_id($self->get_status_id($status_name)); | ||
| 144 | $self->update($job, $self->now); | ||
| 145 | } | ||
| 146 | |||
| 147 | =head2 update_message | ||
| 148 | |||
| 149 | Update the audit message | ||
| 150 | |||
| 151 | =head3 parameters | ||
| 152 | |||
| 153 | * message: The message associated with the Job | ||
| 154 | |||
| 155 | =head3 returns | ||
| 156 | |||
| 157 | * void | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | method update_message(Str $message) { | ||
| 162 | my $job = $self->EtlpJob->find($self->id); | ||
| 163 | $job->message($message); | ||
| 164 | $self->update($job, $self->now); | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 update | ||
| 168 | |||
| 169 | Update the audit record date | ||
| 170 | |||
| 171 | =head3 parameters | ||
| 172 | |||
| 173 | * message: The message associated with the Job | ||
| 174 | |||
| 175 | =head3 returns | ||
| 176 | |||
| 177 | * void | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | method update(ETLp::Schema::Result::EtlpJob $job, | ||
| 182 | Maybe[DateTime] $date) { | ||
| 183 | $date = $self->now unless $date; | ||
| 184 | try { | ||
| 185 | $self->schema->txn_do( | ||
| 186 | sub { | ||
| 187 | $job->date_updated($date); | ||
| 188 | $job->update; | ||
| 189 | } | ||
| 190 | ) | ||
| 191 | } | ||
| 192 | catch { | ||
| 193 | $self->logger->logdie("Cannot update job record: $_"); | ||
| 194 | }; | ||
| 195 | } | ||
| 196 | |||
| 197 | method _get_session_id { | ||
| 198 | my $driver = $self->get_driver; | ||
| 199 | my $session_id; | ||
| 200 | |||
| 201 | # The session should be that of the main database connection not | ||
| 202 | # that of the audit session | ||
| 203 | if ($driver eq 'Oracle') { | ||
| 204 | my $sql = q{ | ||
| 205 | select sid from v$mystat where rownum = 1 | ||
| 206 | }; | ||
| 207 | |||
| 208 | ($session_id) = | ||
| 209 | $self->dbh->selectrow_array($sql); | ||
| 210 | } else { | ||
| 211 | $session_id = 1; | ||
| 212 | } | ||
| 213 | |||
| 214 | return $session_id; | ||
| 215 | } | ||
| 216 | |||
| 217 | method BUILD { | ||
| 218 | $self->session_id($self->_get_session_id); | ||
| 219 | $self->_start_audit; | ||
| 220 | } | ||
| 221 | |||
| 222 | } | ||
| 223 | |||
| 224 | =head1 ROLES CONSUMED | ||
| 225 | |||
| 226 | * ETLp::Role::Config | ||
| 227 | * ETLp::Role::Schema | ||
| 228 | |||
| 229 | =head1 LICENSE AND COPYRIGHT | ||
| 230 | |||
| 231 | Copyright 2010 Redbone Systems Ltd | ||
| 232 | |||
| 233 | This program is free software; you can redistribute it and/or modify it | ||
| 234 | under the terms of either: the GNU General Public License as published | ||
| 235 | by the Free Software Foundation; or the Artistic License. | ||
| 236 | |||
| 237 | The terms are in the LICENSE file that accompanies this application | ||
| 238 | |||
| 239 | =cut |