Skip to content
Browse files

Add a flag to new() to operate in synchronous, single-process mode. S…

…implifies database interaction and reduces multiprocess issues for debugging.
  • Loading branch information...
1 parent 1b8b805 commit 7af3dbc1f27a908e2207d1e585ae2df2e786099b @rcaputo committed Feb 6, 2011
View
BIN POE-Component-SimpleDBI-1.29.tar.gz
Binary file not shown.
View
32 lib/POE/Component/SimpleDBI.pm
@@ -4,7 +4,7 @@ use strict; use warnings;
# Initialize our version
use vars qw( $VERSION );
-$VERSION = '1.29';
+$VERSION = '1.29_04';
# Import what we need from the POE namespace
use POE; # For the constants
@@ -25,7 +25,7 @@ BEGIN {
# Set things in motion!
sub new {
# Get our arguments
- my( $type, $ALIAS, $PREPARE_CACHED ) = @_;
+ my( $type, $ALIAS, $PREPARE_CACHED, $SYNCHRO_MODE ) = @_;
# Get the session alias
if ( ! defined $ALIAS ) {
@@ -112,6 +112,9 @@ sub new {
# Cache sql statements?
'PREPARE_CACHED'=> $PREPARE_CACHED,
+
+ # Synchronous mode?
+ 'SYNCHRO' => $SYNCHRO_MODE,
},
) or die 'Unable to create a new session!';
@@ -677,10 +680,17 @@ sub DB_CONNECT {
push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
}
- # Do we have the wheel running?
- if ( ! defined $_[HEAP]->{'WHEEL'} ) {
- # Create the wheel
- $_[KERNEL]->call( $_[SESSION], 'Setup_Wheel' );
+ # Asynchronous mode.
+ if ( ! defined $_[HEAP]->{SYNCHRO} ) {
+
+ # Do we have the wheel running?
+ if ( ! defined $_[HEAP]->{'WHEEL'} ) {
+ # Create the wheel
+ $_[KERNEL]->call( $_[SESSION], 'Setup_Wheel' );
+ }
+ }
+ else {
+ require POE::Component::SimpleDBI::SubProcess;
}
# Check if the subprocess is not active
@@ -952,7 +962,15 @@ sub Check_Queue {
$_[HEAP]->{'ACTIVE'} = 1;
# Put it in the wheel
- $_[HEAP]->{'WHEEL'}->put( \%queue );
+ if ( defined $_[HEAP]->{SYNCHRO} ) {
+ my $output = (
+ POE::Component::SimpleDBI::SubProcess::process_request(\%queue)
+ );
+ $_[KERNEL]->call($_[SESSION], 'Got_STDOUT', $output) if $output;
+ }
+ else {
+ $_[HEAP]->{'WHEEL'}->put( \%queue );
+ }
} else {
if ( DEBUG ) {
warn 'Check_Queue was called but nothing in the queue!';
View
122 lib/POE/Component/SimpleDBI/SubProcess.pm
@@ -58,39 +58,7 @@ sub main {
# Process each data structure
foreach my $input ( @$data ) {
- # Now, we do the actual work depending on what kind of query it was
- if ( $input->{'ACTION'} eq 'CONNECT' ) {
- # Connect!
- DB_CONNECT( $input );
- } elsif ( $input->{'ACTION'} eq 'DISCONNECT' ) {
- # Disconnect!
- DB_DISCONNECT( $input );
- } elsif ( $input->{'ACTION'} eq 'DO' ) {
- # Fire off the SQL and return success/failure + rows affected
- DB_DO( $input );
- } elsif ( $input->{'ACTION'} eq 'SINGLE' ) {
- # Return a single result
- DB_SINGLE( $input );
- } elsif ( $input->{'ACTION'} eq 'MULTIPLE' ) {
- # Get many results, then return them all at the same time
- DB_MULTIPLE( $input );
- } elsif ( $input->{'ACTION'} eq 'QUOTE' ) {
- DB_QUOTE( $input );
- } elsif ( $input->{'ACTION'} eq 'ATOMIC' ) {
- DB_ATOMIC( $input );
- } elsif ( $input->{'ACTION'} eq 'EXIT' ) {
- # Cleanly disconnect from the DB
- if ( defined $DB ) {
- $DB->disconnect();
- undef $DB;
- }
-
- # EXIT!
- return;
- } else {
- # Unrecognized action!
- output( Make_Error( $input->{'ID'}, 'Unknown action sent from parent' ) );
- }
+ output(process_request($input));
}
}
@@ -108,6 +76,45 @@ sub main {
return;
}
+sub process_request {
+ my $input = shift;
+
+ # Now, we do the actual work depending on what kind of query it was
+ if ( $input->{'ACTION'} eq 'CONNECT' ) {
+ # Connect!
+ my ($success, $output) = DB_CONNECT($input, 0);
+ return $output;
+ } elsif ( $input->{'ACTION'} eq 'DISCONNECT' ) {
+ # Disconnect!
+ return DB_DISCONNECT( $input );
+ } elsif ( $input->{'ACTION'} eq 'DO' ) {
+ # Fire off the SQL and return success/failure + rows affected
+ return DB_DO( $input );
+ } elsif ( $input->{'ACTION'} eq 'SINGLE' ) {
+ # Return a single result
+ return DB_SINGLE( $input );
+ } elsif ( $input->{'ACTION'} eq 'MULTIPLE' ) {
+ # Get many results, then return them all at the same time
+ return DB_MULTIPLE( $input );
+ } elsif ( $input->{'ACTION'} eq 'QUOTE' ) {
+ return DB_QUOTE( $input );
+ } elsif ( $input->{'ACTION'} eq 'ATOMIC' ) {
+ return DB_ATOMIC( $input );
+ } elsif ( $input->{'ACTION'} eq 'EXIT' ) {
+ # Cleanly disconnect from the DB
+ if ( defined $DB ) {
+ $DB->disconnect();
+ undef $DB;
+ }
+
+ # EXIT!
+ return;
+ }
+
+ # Unrecognized action!
+ return( Make_Error( $input->{'ID'}, 'Unknown action sent from parent' ) );
+}
+
# Connects to the DB
sub DB_CONNECT {
# Get the input structure
@@ -189,15 +196,15 @@ sub DB_CONNECT {
# All done!
if ( ! defined $reconn ) {
- output( $output );
+ return (1, $output);
} else {
# Reconnect attempt, was it successful?
if ( ! exists $output->{'ERROR'} ) {
- return 1;
+ return (1, $output);
}
}
- return;
+ return (0, $output);
}
# Disconnects from the DB
@@ -234,8 +241,7 @@ sub DB_DISCONNECT {
}
# All done!
- output( $output );
- return;
+ return $output;
}
# This subroutine does a DB QUOTE
@@ -250,9 +256,8 @@ sub DB_QUOTE {
# Check if we are connected
if ( ! defined $DB or ! $DB->ping() ) {
# Automatically try to reconnect
- if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
- output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
- return;
+ if ( ! (DB_CONNECT( $CONN ))[0] ) {
+ return Make_Error( 'GONE', 'Lost connection to the database server.' );
}
}
@@ -275,8 +280,7 @@ sub DB_QUOTE {
}
# All done!
- output( $output );
- return;
+ return $output;
}
# This subroutine runs a 'SELECT' style query on the db
@@ -292,9 +296,8 @@ sub DB_MULTIPLE {
# Check if we are connected
if ( ! defined $DB or ! $DB->ping() ) {
# Automatically try to reconnect
- if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
- output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
- return;
+ if ( ! (DB_CONNECT( $CONN, 'RECONNECT' ))[0] ) {
+ return( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
}
}
@@ -373,8 +376,7 @@ sub DB_MULTIPLE {
}
# Return the data structure
- output( $output );
- return;
+ return( $output );
}
# This subroutine runs a 'SELECT ... LIMIT 1' style query on the db
@@ -390,9 +392,8 @@ sub DB_SINGLE {
# Check if we are connected
if ( ! defined $DB or ! $DB->ping() ) {
# Automatically try to reconnect
- if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
- output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
- return;
+ if ( ! (DB_CONNECT( $CONN, 'RECONNECT' ))[0] ) {
+ return Make_Error( 'GONE', 'Lost connection to the database server.' );
}
}
@@ -452,8 +453,7 @@ sub DB_SINGLE {
}
# Return the data structure
- output( $output );
- return;
+ return $output;
}
# This subroutine runs a 'DO' style query on the db
@@ -470,9 +470,8 @@ sub DB_DO {
# Check if we are connected
if ( ! defined $DB or ! $DB->ping() ) {
# Automatically try to reconnect
- if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
- output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
- return;
+ if ( ! (DB_CONNECT( $CONN, 'RECONNECT' ))[0] ) {
+ return Make_Error( 'GONE', 'Lost connection to the database server.' );
}
}
@@ -539,8 +538,7 @@ sub DB_DO {
}
# Return the data structure
- output( $output );
- return;
+ return $output;
}
# This subroutine runs a 'DO' style query on the db in a transaction
@@ -555,9 +553,8 @@ sub DB_ATOMIC {
# Check if we are connected
if ( ! defined $DB or ! $DB->ping() ) {
# Automatically try to reconnect
- if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
- output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
- return;
+ if ( ! (DB_CONNECT( $CONN, 'RECONNECT' ))[0] ) {
+ return Make_Error( 'GONE', 'Lost connection to the database server.' );
}
}
@@ -636,8 +633,7 @@ sub DB_ATOMIC {
}
# Return the data structure
- output( $output );
- return;
+ return $output;
}
# This subroutine makes a generic error structure

0 comments on commit 7af3dbc

Please sign in to comment.
Something went wrong with that request. Please try again.