1 |
joko |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
|
|
# --------------------------------------------------------------- |
7 |
|
|
# just using POE itself and the DBIAgent-component as a query-agent (QA) |
8 |
|
|
use POE qw( Component::DBIAgent ); |
9 |
|
|
# for debugging ... |
10 |
|
|
use Carp; |
11 |
|
|
use Data::Dumper; |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
# --------------------------------------------------------------- |
15 |
|
|
sub make_queries { |
16 |
|
|
## makes a hash from sql-query-strings |
17 |
|
|
## which will be passed to the query-agent later |
18 |
|
|
my $queries = { |
19 |
|
|
read_message => 'SELECT * FROM pdmsg WHERE state IS NULL', |
20 |
|
|
update_one_account => 'update hits_due set hits = hits + ? where acctid = ?', |
21 |
|
|
}; |
22 |
|
|
return $queries; |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
# --------------------------------------------------------------- |
27 |
|
|
sub got_row { |
28 |
|
|
## This state gets called once for every row returned |
29 |
|
|
## from the query read_hits_due, and then once |
30 |
|
|
## with $_[ARG0] containing the string 'EOF'. |
31 |
|
|
|
32 |
|
|
#carp("============= got result", "\n"); |
33 |
|
|
|
34 |
|
|
my ( $kernel, $self, $heap, $row ) = @_[ KERNEL, OBJECT, HEAP, ARG0 ]; |
35 |
|
|
|
36 |
|
|
if ( $row ne 'EOF' ) { |
37 |
|
|
# PROCESS A ROW |
38 |
|
|
print "------------------ got row", "\n"; |
39 |
|
|
print Dumper($row); |
40 |
|
|
} else { |
41 |
|
|
# NO MORE ROWS |
42 |
|
|
print "--- no more rows", "\n"; |
43 |
|
|
# let the kernel run for two/three timeslices ... |
44 |
|
|
#$kernel->run_one_timeslice(); |
45 |
|
|
#$kernel->run_one_timeslice(); |
46 |
|
|
#$kernel->run_one_timeslice(); |
47 |
|
|
# ... before stopping it by sending an "IDLE"-signal |
48 |
|
|
# does it do with "IDLE"? |
49 |
|
|
#$kernel->signal($kernel, "IDLE"); |
50 |
|
|
# do we need to TERM? |
51 |
|
|
#$kernel->signal($kernel, "TERM"); |
52 |
|
|
} |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
|
56 |
|
|
# --------------------------------------------------------------- |
57 |
|
|
sub got_error { |
58 |
|
|
my ( $kernel, $heap, $errmsg ) = @_[ KERNEL, HEAP, ARG0 ]; |
59 |
|
|
print "$0 got error:", "\n", $errmsg, "\n"; |
60 |
|
|
$kernel->signal($kernel, "TERM"); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
# --------------------------------------------------------------- |
65 |
|
|
sub _start { |
66 |
|
|
my ( $kernel, $session, $heap ) = @_[ KERNEL, HEAP ]; |
67 |
|
|
|
68 |
|
|
# setting up the query-agent |
69 |
|
|
$heap->{helper} = POE::Component::DBIAgent->new( |
70 |
|
|
DSN => [ 'dbi:mysql:topscores:host=192.168.10.51', |
71 |
|
|
"root", |
72 |
|
|
"" |
73 |
|
|
], |
74 |
|
|
Count => 1, # how many agents to start |
75 |
|
|
Queries => make_queries(), # the "queries"-hash |
76 |
|
|
Debug => 1, # turn debugging on/off |
77 |
|
|
); |
78 |
|
|
|
79 |
|
|
# reference to session which will recieve results |
80 |
|
|
#my $sess = $kernel->get_active_session(); |
81 |
|
|
|
82 |
|
|
# start query-agent |
83 |
|
|
$heap->{helper}->setErrorHandler( |
84 |
|
|
$session => 'got_error' |
85 |
|
|
); |
86 |
|
|
$heap->{helper}->query( |
87 |
|
|
read_message # the named query to execute |
88 |
|
|
=> $session => 'got_row' # the session and state to send results to |
89 |
|
|
# and any parameters would go here. |
90 |
|
|
); |
91 |
|
|
|
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
# --------------------------------------------------------------- |
95 |
|
|
sub _stop { |
96 |
|
|
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; |
97 |
|
|
print "$0 stopped", "\n"; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
# --------------------------------------------------------------- |
102 |
|
|
# the handlers for starting, stopping |
103 |
|
|
# and getting valid or invalid responses (errors) |
104 |
|
|
my @handlers = qw( |
105 |
|
|
_start |
106 |
|
|
_stop |
107 |
|
|
got_row |
108 |
|
|
got_error |
109 |
|
|
); |
110 |
|
|
# the main session for the query-agent |
111 |
|
|
POE::Session->create( |
112 |
|
|
package_states => [ main => \@handlers ], |
113 |
|
|
); |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
# --------------------------------------------------------------- |
117 |
|
|
# start the kernel |
118 |
|
|
$poe_kernel->run(); |
119 |
|
|
exit; |