App-Dochazka-REST
view release on metacpan or search on metacpan
lib/App/Dochazka/REST/ConnBank.pm view on Meta::CPAN
my $dbsslmode = $site->DOCHAZKA_DBSSLMODE;
my $data_source = "Dbi:Pg:dbname=\"$dbname\"";
$data_source .= ";host=$dbhost" if $dbhost;
$data_source .= ";port=$dbport" if $dbport;
$data_source .= ";sslmode=$dbsslmode" if $dbsslmode;
$log->debug( "Returning DBIx::Connector object for data source $data_source and user $dbuser" );
return DBIx::Connector->new(
$data_source,
$dbuser,
$dbpass,
{
PrintError => 0,
RaiseError => 1,
AutoCommit => 1,
AutoInactiveDestroy => 1,
},
);
}
=head2 init_singleton
Initialize the C<$dbix_conn> singleton using dbname, dbuser, and dbpass values
from site configuration. Also set the PGTZ environment variable to the
value of the DOCHAZKA_TIMEZONE config param.
Idempotent.
=cut
sub init_singleton {
$ENV{'PGTZ'} = $site->DOCHAZKA_TIMEZONE;
return if ref( $dbix_conn ) and $dbix_conn->can( 'dbh' );
$dbix_conn = get_arbitrary_dbix_conn(
$site->DOCHAZKA_DBNAME,
$site->DOCHAZKA_DBUSER,
$site->DOCHAZKA_DBPASS,
);
}
=head2 conn_up
Given a L<DBIx::Connector> object, call L<ping> on the associated
database handle and return true or false based on the result.
If no argument is given, returns the status of the C<$dbix_conn>
singleton.
=cut
sub conn_up {
my $arg = shift;
my $conn = $arg || $dbix_conn;
my $bool = 0;
return $bool unless ref( $conn ) eq 'DBIx::Connector';
# the ping command can and will throw and exception if the database server
# is unreachable
try {
$bool = $conn->dbh->ping;
};
return $bool;
}
=head2 conn_status
Given a L<DBIx::Connector> object, call L<ping> on the associated
database handle and return either 'UP' or 'DOWN' based on the result.
If no argument is given, returns the status of the C<$dbix_conn>
singleton.
=cut
sub conn_status {
my $arg = shift;
return conn_up( $arg ) ? "UP" : "DOWN";
}
1;
( run in 1.041 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )