forked from sng2c/Gearman-SlotManager
/
SlotManager.pm
170 lines (149 loc) · 4.23 KB
/
SlotManager.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package Gearman::SlotManager;
# ABSTRACT: Managing Worker's lifecycle with Slots
# VERSION
use Devel::GlobalDestruction;
use namespace::autoclean;
use Log::Log4perl qw(:easy);
#Log::Log4perl->easy_init($DEBUG);
Log::Log4perl->easy_init($ERROR);
use Data::Dumper;
use Any::Moose;
use AnyEvent;
use EV;
use AnyEvent::HTTPD;
use Scalar::Util qw(weaken);
use Gearman::Slot;
has slotmap=>(is=>'rw', isa=>'HashRef', default=>sub{ return {}; });
has config=>(is=>'rw', isa=>'HashRef',required=>1);
has idle_watcher=>(is=>'rw');
has httpd=>(is=>'rw');
has port=>(is=>'rw',default=>sub{return 55995;});
sub BUILD{
my $self = shift;
my $conf = $self->config;
my %global = %{$conf->{'global'}};
my %baseconf = (
job_servers=>[''],
min=>1,
max=>1,
workleft=>0,
);
%global = (%baseconf,%global);
my %confs = %{$conf->{slots}};
foreach my $worker (keys %confs){
my %conf = %{$confs{$worker}};
%conf = (%global,%conf);
DEBUG Dumper(\%conf);
my @slots;
foreach (0 .. $conf{max}-1){
my $slot = Gearman::Slot->new(
job_servers=>$conf{job_servers},
libs=>$conf{libs},
workleft=>$conf{workleft},
worker_package=>$worker,
worker_channel=>$worker.'__'.$_,
sbbaseurl=>'http://localhost:'.$self->port,
);
push( @slots, $slot);
}
$self->slotmap->{$worker} = {conf=>\%conf, slots=>\@slots};
}
my $httpd = AnyEvent::HTTPD->new(port=>$self->port);
$httpd->reg_cb (
'/busy'=>sub{
my ($httpd,$req) = @_;
DEBUG "SB busy ".$req->parm('channel');
my ($key,$idx) = split(/__/,$req->parm('channel'));
DEBUG "SB busy $key $idx";
$self->slots($key)->[$idx]->is_busy(1);
$req->respond({content=>['text/plain','ok']});
},
'/idle'=>sub{
my ($httpd,$req) = @_;
DEBUG "SB idle ".$req->parm('channel');
my ($key,$idx) = split(/__/,$req->parm('channel'));
DEBUG "SB idle $key $idx";
$self->slots($key)->[$idx]->is_busy(0);
$req->respond({content=>['text/plain','ok']});
},
);
$self->httpd($httpd);
weaken($self);
}
sub slots{
my $self = shift;
my $key = shift;
return $self->slotmap->{$key}->{slots};
}
sub conf{
my $self = shift;
my $key = shift;
return $self->slotmap->{$key}->{conf};
}
sub start{
DEBUG __PACKAGE__." start";
my $self = shift;
foreach my $key (keys %{$self->slotmap}){
my $slots = $self->slots($key);
my $conf = $self->conf($key);
my $min = $conf->{min};
foreach my $i ( 0 .. $min-1 ){
$slots->[$i]->start();
}
}
my $iw = AE::timer 0,5, sub{$self->on_idle;};
$self->idle_watcher($iw);
#weaken($self);
}
sub on_idle{
my $self = shift;
DEBUG "ON_IDLE";
foreach my $key (keys %{$self->slotmap}){
my @slots = @{$self->slots($key)};
my %conf = %{$self->conf($key)};
my $idle = 0;
my $running = 0;
foreach my $s ( @slots ){
$idle += $s->is_idle;
$running += $s->is_running;
}
DEBUG "[$key] idle: $idle, running: $running";
if( !$idle ){
if( $running < $conf{max} ){
DEBUG "expand $key";
my @stopped = grep{$_->is_stopped}@slots;
shift(@stopped)->start;
}
}
else{
if( $running > $conf{min} ){
DEBUG "reduce $key";
my @running = grep{$_->is_running}@slots;
pop(@running)->stop;
}
}
}
}
sub stop{
DEBUG __PACKAGE__." stop";
my $self = shift;
$self->idle_watcher(undef);
foreach my $key (keys %{$self->slotmap}){
my $slots = $self->slots($key);
foreach my $s ( @{$slots} ){
$s->stop() unless $s->is_stopped;
}
}
}
sub DEMOLISH{
return if in_global_destruction;
DEBUG __PACKAGE__.' DEMOLISHED';
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=head1 SYNOPSIS
Will be updated soon.
See testManager.pl in Gearman::SlotManager directory.
=cut