| 1 |
package MJAM::Connector::Dbi; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use MJAM::main; |
| 7 |
use Data::Dumper; |
| 8 |
|
| 9 |
use POE qw( Component::DBIAgent ); |
| 10 |
|
| 11 |
my $poe_alias; |
| 12 |
|
| 13 |
# --------------------------------------------------------------- |
| 14 |
sub make_queries { |
| 15 |
## makes a hash from sql-query-strings |
| 16 |
## which will be passed to the query-agent later |
| 17 |
my $queries = { |
| 18 |
read_message => 'SELECT * FROM pdmsg WHERE state IS NULL', |
| 19 |
update_one_account => 'update hits_due set hits = hits + ? where acctid = ?', |
| 20 |
}; |
| 21 |
return $queries; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
sub doQuery { |
| 26 |
my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ]; |
| 27 |
|
| 28 |
print "q"; |
| 29 |
|
| 30 |
# reference to session which will recieve results |
| 31 |
#my $sess = $kernel->get_active_session(); |
| 32 |
|
| 33 |
# start query-agent |
| 34 |
$heap->{helper}->setErrorHandler( |
| 35 |
$session => 'got_error' |
| 36 |
); |
| 37 |
|
| 38 |
$heap->{helper}->query( |
| 39 |
read_message # the named query to execute |
| 40 |
=> $session => 'got_row' # the session and state to send results to |
| 41 |
# and any parameters would go here. |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
# --------------------------------------------------------------- |
| 46 |
sub got_row { |
| 47 |
## This state gets called once for every row returned |
| 48 |
## from the query read_hits_due, and then once |
| 49 |
## with $_[ARG0] containing the string 'EOF'. |
| 50 |
|
| 51 |
#carp("============= got result", "\n"); |
| 52 |
|
| 53 |
my ( $kernel, $self, $heap, $row ) = @_[ KERNEL, OBJECT, HEAP, ARG0 ]; |
| 54 |
|
| 55 |
if ( $row ne 'EOF' ) { |
| 56 |
# PROCESS A ROW |
| 57 |
#print "------------------ got row", "\n"; |
| 58 |
print Dumper($row); |
| 59 |
print "."; |
| 60 |
} else { |
| 61 |
# NO MORE ROWS |
| 62 |
print "--- no more rows", "\n"; |
| 63 |
#print "|"; |
| 64 |
# let the kernel run for two/three timeslices ... |
| 65 |
#$kernel->run_one_timeslice(); |
| 66 |
#$kernel->run_one_timeslice(); |
| 67 |
#$kernel->run_one_timeslice(); |
| 68 |
# ... before stopping it by sending an "IDLE"-signal |
| 69 |
# does it do with "IDLE"? |
| 70 |
#$kernel->signal($kernel, "IDLE"); |
| 71 |
# do we need to TERM? |
| 72 |
#$kernel->signal($kernel, "TERM"); |
| 73 |
|
| 74 |
#$kernel->delay_set("start", 5); |
| 75 |
|
| 76 |
#$kernel->delay_set("doQuery", 5); |
| 77 |
#$kernel->yield("doQuery"); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
# --------------------------------------------------------------- |
| 83 |
sub got_error { |
| 84 |
my ( $kernel, $heap, $errmsg ) = @_[ KERNEL, HEAP, ARG0 ]; |
| 85 |
print "$0 got error:", "\n", $errmsg, "\n"; |
| 86 |
#$kernel->signal($kernel, "TERM"); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
# --------------------------------------------------------------- |
| 91 |
sub _start { |
| 92 |
my ( $kernel, $session, $heap, $alias ) = @_[ KERNEL, SESSION, HEAP, ARG0 ]; |
| 93 |
|
| 94 |
$kernel->alias_set($alias); |
| 95 |
$heap->{Alias} = $alias; |
| 96 |
|
| 97 |
# setting up the query-agent |
| 98 |
$heap->{helper} = POE::Component::DBIAgent->new( |
| 99 |
DSN => [ 'dbi:mysql:topscores:host=192.168.10.51', |
| 100 |
"root", |
| 101 |
"" |
| 102 |
], |
| 103 |
Count => 1, # how many agents to start |
| 104 |
Queries => make_queries(), # the "queries"-hash |
| 105 |
Debug => DEBUG(), # turn debugging on/off |
| 106 |
); |
| 107 |
|
| 108 |
#$kernel->post($session, 'doQuery'); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
# --------------------------------------------------------------- |
| 113 |
sub _stop { |
| 114 |
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; |
| 115 |
print "$0 stopped", "\n"; |
| 116 |
} |
| 117 |
|
| 118 |
sub shutdown { |
| 119 |
my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ]; |
| 120 |
print "dbagent shutdown", "\n"; |
| 121 |
$kernel->post($heap->{helper}->{session_main}, 'shutdown'); |
| 122 |
$kernel->run_one_timeslice(); |
| 123 |
$kernel->run_one_timeslice(); |
| 124 |
$kernel->run_one_timeslice(); |
| 125 |
$kernel->alias_remove($poe_alias); |
| 126 |
#$kernel->signal($heap->{helper}, 'TERM'); |
| 127 |
delete $heap->{helper}; |
| 128 |
#$kernel->signal($session, 'TERM'); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
my $session_ref; |
| 134 |
|
| 135 |
# --------------------------------------------------------------- |
| 136 |
sub start { |
| 137 |
|
| 138 |
my $alias = shift; |
| 139 |
$poe_alias = $alias; |
| 140 |
|
| 141 |
# the main session for the query-agent |
| 142 |
$session_ref = POE::Session->create( |
| 143 |
inline_states => { |
| 144 |
_start => \&_start, |
| 145 |
_top => \&_stop, |
| 146 |
got_row => \&got_row, |
| 147 |
got_error => \&got_error, |
| 148 |
doQuery => \&doQuery, |
| 149 |
shutdown => \&shutdown, |
| 150 |
}, |
| 151 |
args => [ $alias ], |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
sub stop { |
| 156 |
$poe_kernel->post($session_ref, 'shutdown'); |
| 157 |
$poe_kernel->run_one_timeslice(); |
| 158 |
$poe_kernel->run_one_timeslice(); |
| 159 |
$poe_kernel->run_one_timeslice(); |
| 160 |
$poe_kernel->run_one_timeslice(); |
| 161 |
kill TERM => $session_ref; |
| 162 |
undef $session_ref; |
| 163 |
} |
| 164 |
|
| 165 |
1; |