Skip to content

Commit

Permalink
apply rcaputo patch for SYNCHRO_MODE
Browse files Browse the repository at this point in the history
  • Loading branch information
apocalypse committed Feb 10, 2011
2 parents a846fbd + 7af3dbc commit fd22bc1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 69 deletions.
36 changes: 30 additions & 6 deletions lib/POE/Component/SimpleDBI.pm
Expand Up @@ -21,7 +21,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 ) {
Expand Down Expand Up @@ -108,6 +108,9 @@ sub new {

# Cache sql statements?
'PREPARE_CACHED'=> $PREPARE_CACHED,

# Synchronous mode?
'SYNCHRO' => $SYNCHRO_MODE,
},
) or die 'Unable to create a new session!';

Expand Down Expand Up @@ -673,10 +676,15 @@ 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
Expand Down Expand Up @@ -948,7 +956,14 @@ 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!';
Expand Down Expand Up @@ -1447,6 +1462,15 @@ This sets the global PREPARE_CACHED setting. This is a boolean value.
The default is enabled.
=head3 SYNCHRONOUS_MODE
This disables the fork() that the subprocess does. Use this only if you are having issues with the backend
and want to debug the database without dealing with multiprocess issues.
POE::Component::SimpleDBI->new( 'ALIAS', 1, 1 );
The default is disabled.
=head2 Commands
There are a few commands you can trigger in SimpleDBI. They are triggered via $_[KERNEL]->post( ... );
Expand Down
123 changes: 60 additions & 63 deletions lib/POE/Component/SimpleDBI/SubProcess.pm
Expand Up @@ -54,39 +54,8 @@ 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 ) );
return if $input->{'ACTION'} eq 'EXIT';
}
}

Expand All @@ -104,6 +73,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
Expand Down Expand Up @@ -185,15 +193,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
Expand Down Expand Up @@ -230,8 +238,7 @@ sub DB_DISCONNECT {
}

# All done!
output( $output );
return;
return $output;
}

# This subroutine does a DB QUOTE
Expand All @@ -246,9 +253,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, 'RECONNECT' ) )[0] ) {
return Make_Error( 'GONE', 'Lost connection to the database server.' );
}
}

Expand All @@ -271,8 +277,7 @@ sub DB_QUOTE {
}

# All done!
output( $output );
return;
return $output;
}

# This subroutine runs a 'SELECT' style query on the db
Expand All @@ -288,9 +293,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.' ) );
}
}

Expand Down Expand Up @@ -369,8 +373,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
Expand All @@ -386,9 +389,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.' );
}
}

Expand Down Expand Up @@ -448,8 +450,7 @@ sub DB_SINGLE {
}

# Return the data structure
output( $output );
return;
return $output;
}

# This subroutine runs a 'DO' style query on the db
Expand All @@ -466,9 +467,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.' );
}
}

Expand Down Expand Up @@ -535,8 +535,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
Expand All @@ -551,9 +550,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.' );
}
}

Expand Down Expand Up @@ -632,8 +630,7 @@ sub DB_ATOMIC {
}

# Return the data structure
output( $output );
return;
return $output;
}

# This subroutine makes a generic error structure
Expand Down

0 comments on commit fd22bc1

Please sign in to comment.