1 |
|
2 |
use strict; |
3 |
use warnings; |
4 |
|
5 |
#sub POE::Kernel::ASSERT_DEFAULT () { 1 } |
6 |
#sub POE::Kernel::TRACE_DEFAULT () { 1 } |
7 |
|
8 |
use POE qw( Component::JobQueue ); |
9 |
|
10 |
my $jobcount = 0; |
11 |
|
12 |
sub worker_start { |
13 |
my ( $kernel, $heap, $postback, @args ) = @_[KERNEL, HEAP, ARG0, ARG1..ARG3 ]; |
14 |
print "worker started", "\n"; |
15 |
print "executing job: ", "(@args)", "\n"; |
16 |
my @job_results = ('hello', 'world'); |
17 |
|
18 |
# Invoke a postback to acknowledge that a job is done. |
19 |
$postback->( @job_results ); |
20 |
} |
21 |
|
22 |
# This is the sub which is called when a postback is invoked. |
23 |
sub job_done { |
24 |
my ($request_packet, $response_packet) = @_[ARG0, ARG1]; |
25 |
|
26 |
my @original_job_params = @{$request_packet}; # original post/fetch |
27 |
my @job_results = @{$response_packet}; # passed to the postback |
28 |
|
29 |
print "original job parameters: (@original_job_params)\n"; |
30 |
print "results of finished job: (@job_results)\n"; |
31 |
$jobcount++; |
32 |
} |
33 |
|
34 |
# Passive worker function. |
35 |
sub spawn_a_worker { |
36 |
my ($postback, @job_params) = @_; # same parameters as posted |
37 |
print "spawning worker", "\n"; |
38 |
my %inline_states = ( |
39 |
'_start' => \&worker_start, |
40 |
); |
41 |
POE::Session->create |
42 |
( inline_states => \%inline_states, # handwaving over details here |
43 |
args => [ $postback, # $postback->(@results) to return |
44 |
@job_params, # parameters of this job |
45 |
], |
46 |
); |
47 |
} |
48 |
|
49 |
# comparison to let queue behave as FIFO |
50 |
sub job_comparer { 1 } |
51 |
|
52 |
|
53 |
# Passive queue waits for enqueue events. |
54 |
my $jq = POE::Component::JobQueue->spawn |
55 |
( Alias => 'myq', # defaults to 'queuer' |
56 |
WorkerLimit => 16, # defaults to 8 |
57 |
Worker => \&spawn_a_worker, # code which will start a session |
58 |
Passive => |
59 |
{ Prioritizer => \&job_comparer, # defaults to sub { 1 } # FIFO |
60 |
}, |
61 |
); |
62 |
|
63 |
|
64 |
# start of main session |
65 |
sub _start { |
66 |
my ( $kernel, $heap ) = @_[KERNEL, HEAP]; |
67 |
#sendJob($kernel); |
68 |
#$kernel->delay( $event, $seconds, @event_args ); |
69 |
$kernel->delay( 'sendJob', 1 ); |
70 |
|
71 |
my $alarm_id; |
72 |
#my $alarm_id = $kernel->delay_set( $event, $seconds_hence, @etc ); |
73 |
$alarm_id = $kernel->delay_set( 'sendJob', 6 ); |
74 |
$alarm_id = $kernel->delay_set( 'sendJob', 6 ); |
75 |
$alarm_id = $kernel->delay_set( 'sendJob', 6 ); |
76 |
$alarm_id = $kernel->delay_set( 'sendJob', 6 ); |
77 |
$alarm_id = $kernel->delay_set( 'sendJob', 6 ); |
78 |
$alarm_id = $kernel->delay_set( 'sendJob', 6 ); |
79 |
$alarm_id = $kernel->delay_set( 'sendJob', 6 ); |
80 |
$alarm_id = $kernel->delay_set( 'sendJob', 1 ); |
81 |
$alarm_id = $kernel->delay_set( 'sendJob', 1 ); |
82 |
$alarm_id = $kernel->delay_set( 'sendJob', 2 ); |
83 |
$alarm_id = $kernel->delay_set( 'sendJob', 3 ); |
84 |
$alarm_id = $kernel->delay_set( 'sendJob', 3 ); |
85 |
$alarm_id = $kernel->delay_set( 'sendJob', 3 ); |
86 |
$alarm_id = $kernel->delay_set( 'sendJob', 4 ); |
87 |
$alarm_id = $kernel->delay_set( 'sendJob', 5 ); |
88 |
$alarm_id = $kernel->delay_set( 'sendJob', 5 ); |
89 |
$alarm_id = $kernel->delay_set( 'sendJob', 5 ); |
90 |
$alarm_id = $kernel->delay_set( 'sendJob', 5 ); |
91 |
|
92 |
print "\n", "start ok", "\n"; |
93 |
|
94 |
} |
95 |
|
96 |
# example for a sendjob-function |
97 |
sub sendJob { |
98 |
my ( $kernel, $heap ) = @_[KERNEL, HEAP]; |
99 |
|
100 |
my @job_params = ('test', 'abc', 'def'); |
101 |
|
102 |
for (my $i = 0; $i < 10; $i++) { |
103 |
|
104 |
# Enqueuing a job in a passive queue. |
105 |
$kernel->post( 'myq', # post to alias |
106 |
'enqueue', # 'enqueue' a job |
107 |
'job_done', # which of our states is notified when it's done |
108 |
@job_params, # job parameters |
109 |
); |
110 |
} |
111 |
print "10 jobs posted", "\n"; |
112 |
|
113 |
} |
114 |
|
115 |
my @handlers = qw( _start job_done sendJob ); |
116 |
POE::Session->create( package_states => [ main => \@handlers ] ); |
117 |
|
118 |
print "kernel boot start", "\n"; |
119 |
$poe_kernel->run(); |
120 |
print "kernel boot ready", "\n"; |
121 |
print "jobcount: $jobcount", "\n"; |
122 |
|
123 |
exit; |