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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::TheSchwartz;
use strict;
use base qw( TheSchwartz );
use MT::ObjectDriver::Driver::Cache::RAM;
use List::Util qw( shuffle );
my $instance;
our $RANDOMIZE_JOBS = 0;
our $OBJECT_REPORT = 0;
sub instance {
$instance ||= MT::TheSchwartz->new();
return $instance;
}
sub debug {
my $class = shift;
$class->instance->SUPER::debug(@_);
}
sub insert {
my $class = shift;
$class->instance->SUPER::insert(@_);
}
sub default_logger {
my ( $msg, $job ) = @_;
# suppress TheSchwartz::Job's 'job completed'
return if $msg eq 'job completed';
$msg =~ s/\s+$//;
print STDERR "$msg\n";
}
sub new {
my $class = shift;
$class->mt_schwartz_init();
my (%param) = @_;
my $workers = delete $param{workers} if exists $param{workers};
$RANDOMIZE_JOBS = delete $param{randomize} if exists $param{randomize};
# Reports object usage inbetween jobs if Devel::Leak::Object is loaded
$OBJECT_REPORT = 1 if $Devel::Leak::Object::VERSION;
$param{verbose} = \&default_logger
if $param{verbose} && ( ref $param{verbose} ne 'CODE' );
my $client = $class->SUPER::new(%param);
if ($client) {
$instance = $client;
unless ($workers) {
$workers = [];
my $all_workers ||= MT->registry("task_workers") || {};
foreach my $id ( keys %$all_workers ) {
my $w = $all_workers->{$id};
my $c = $w->{class} or next;
push @$workers, $c;
}
}
if (@$workers) {
# Can we do this?
foreach my $c (@$workers) {
if ( eval( 'require ' . $c ) ) {
# Yes, we can do this.
$client->can_do($c);
}
else {
# No, we can't. Here's why...
print STDERR "Failed to load worker class '$c': $@\n";
}
}
}
}
return $client;
}
our $initialized;
sub mt_schwartz_init {
return if $initialized;
# Update the datasource for these, since MT adds an addition 'schwartz_'
# prefix for them.
require TheSchwartz::FuncMap;
require TheSchwartz::Job;
require TheSchwartz::Error;
require TheSchwartz::ExitStatus;
TheSchwartz::FuncMap->properties->{datasource} = 'ts_funcmap';
TheSchwartz::Job->properties->{datasource} = 'ts_job';
TheSchwartz::Error->properties->{datasource} = 'ts_error';
TheSchwartz::ExitStatus->properties->{datasource} = 'ts_exitstatus';
return $initialized = 1;
}
sub driver_for {
require MT::TheSchwartz::Job;
return MT::TheSchwartz::Job->dbi_driver;
}
sub shuffled_databases {
my TheSchwartz $client = shift;
return '1';
}
sub hash_databases {
return 1;
}
sub mark_database_as_dead {
return 1;
}
sub is_database_dead {
return 0;
}
sub _has_enough_swap {
my $memory_module;
eval {
require Sys::MemInfo;
$memory_module = q{Sys::MemInfo};
};
my ($mem_limit) = @_;
if ( !defined($mem_limit) ) {
$mem_limit = MT->config('SchwartzSwapMemoryLimit');
}
if ( $mem_limit && $memory_module ) {
my $decoded_limit;
if ( $mem_limit =~ /^\d+[KGM]B?$/ ) {
my $multiplier = 1;
( $mem_limit =~ /GB?$/ ) and $multiplier = 1073741824;
( $mem_limit =~ /MB?$/ ) and $multiplier = 1048576;
( $mem_limit =~ /KB?$/ ) and $multiplier = 1024;
$mem_limit =~ s/[KGM]B?$//;
$mem_limit = $mem_limit * $multiplier;
}
if ( $mem_limit =~ /\d+/ ) {
my $swap;
if ( $memory_module eq q{Sys::MemInfo} ) {
$swap = Sys::MemInfo::get("freeswap");
}
# not enough swap, lets get out of here!
if ( $swap < $mem_limit ) {
return 0;
}
}
}
# default to returning true
# i.e., yes there is enough
return 1;
}
sub _has_enough_memory {
my $memory_module;
eval {
require Sys::MemInfo;
$memory_module = q{Sys::MemInfo};
};
my ($mem_limit) = @_;
if ( !defined($mem_limit) ) {
$mem_limit = MT->config('SchwartzFreeMemoryLimit');
}
if ( $mem_limit && $memory_module ) {
my $decoded_limit;
if ( $mem_limit =~ /^\d+[KGM]B?$/ ) {
my $multiplier = 1;
( $mem_limit =~ /GB?$/ ) and $multiplier = 1073741824;
( $mem_limit =~ /MB?$/ ) and $multiplier = 1048576;
( $mem_limit =~ /KB?$/ ) and $multiplier = 1024;
$mem_limit =~ s/[KGM]B?$//;
$mem_limit = $mem_limit * $multiplier;
}
if ( $mem_limit =~ /\d+/ ) {
my $free;
if ( $memory_module eq q{Sys::MemInfo} ) {
$free = Sys::MemInfo::get("freemem");
}
# not enough free, lets get out of here!
if ( $free < $mem_limit ) {
return 0;
}
}
}
# default to returning true
# i.e., yes there is enough
return 1;
}
# Replacement for TheSchwartz::get_server_time
# to simply return value from dbd->sql_for_unixtime
# if it is a plain number (the driver has no function,
# it's just returning time())
sub get_server_time {
my TheSchwartz $client = shift;
my ($driver) = @_;
my $unixtime_sql = $driver->dbd->sql_for_unixtime;
return $unixtime_sql if $unixtime_sql =~ m/^\d+$/;
return $driver->r_handle->selectrow_array("SELECT $unixtime_sql");
}
sub work_until_done {
my TheSchwartz $client = shift;
if ( !$client ) {
return;
}
my $cap = MT->config('SchwartzClientDeadline'); # in seconds
my $mem_limit = MT->config('SchwartzFreeMemoryLimit');
$mem_limit ||= 0;
my $swap_limit = MT->config('SchwartzSwapMemoryLimit');
$swap_limit ||= 0;
my $deadline;
if ($cap) {
$deadline = time() + $cap;
while ( time() < $deadline ) {
$client->work_once or last;
last unless _has_enough_memory($mem_limit);
last unless _has_enough_memory($swap_limit);
}
}
else {
while (1) {
$client->work_once or last;
last unless _has_enough_memory($mem_limit);
last unless _has_enough_swap($swap_limit);
}
}
}
sub work_periodically {
my TheSchwartz $client = shift;
my ($delay) = @_;
$delay ||= 5;
my $last_task_run = 0;
my $did_work = 0;
# holds state of objects at start
my %obj_start;
if ($OBJECT_REPORT) {
%obj_start = %Devel::Leak::Object::OBJECT_COUNT;
}
while (1) {
my %obj_pre;
if ($OBJECT_REPORT) {
%obj_pre = %Devel::Leak::Object::OBJECT_COUNT;
}
if ( $client->work_once ) {
$did_work = 1;
}
if ( $last_task_run + 60 * 5 < time ) {
MT->run_tasks();
$did_work = 1;
$last_task_run = time;
}
if ($did_work) {
# Clear RAM cache
MT::ObjectDriver::Driver::Cache::RAM->clear_cache;
MT->request->reset();
$did_work = 0;
if ($OBJECT_REPORT) {
my $report = leak_report( \%obj_start, \%obj_pre,
\%Devel::Leak::Object::OBJECT_COUNT );
$client->debug($report) if $report ne '';
}
}
sleep $delay;
}
}
our %persistent;
BEGIN {
%persistent = map { $_ => 1 }
qw( MT::Callback MT::Task MT::Plugin MT::Component MT::ArchiveType MT::TaskMgr MT::WeblogPublisher MT::Serialize TheSchwartz::Job TheSchwartz::JobHandle );
}
sub leak_report {
my ( $start, $pre, $post ) = @_;
my $reported;
my $report = '';
foreach my $class ( sort keys %$post ) {
# skip reporting classes that are persistent in nature
next if exists $persistent{$class};
my $post_count = $post->{$class};
next if !$post_count;
my $pre_count = $pre->{$class} || 0;
my $start_count = $start->{$class} || 0;
next if $post_count == 1; # ignores most singletons
if ( ( $pre_count != $post_count )
|| ( $post_count != $start_count ) )
{
$report
.= "Leak report (class, total, delta from last job(s), delta since process start):\n"
unless $reported;
$report .= sprintf(
"%-40s %-10d %-10d %-10d\n",
$class, $post_count,
$post_count - $pre_count,
$post_count - $start_count
);
$reported = 1;
}
}
return $report;
}
sub _grab_a_job {
my TheSchwartz $client = shift;
my $hashdsn = shift;
my $driver = $client->driver_for($hashdsn);
## Got some jobs! Randomize them to avoid contention between workers.
my @jobs = $RANDOMIZE_JOBS ? shuffle(@_) : @_;
JOB:
while ( my $job = shift @jobs ) {
## Convert the funcid to a funcname, based on this database's map.
$job->funcname(
$client->funcid_to_name( $driver, $hashdsn, $job->funcid ) );
## Update the job's grabbed_until column so that
## no one else takes it.
my $worker_class = $job->funcname;
my $old_grabbed_until = $job->grabbed_until;
my $server_time = $client->get_server_time($driver)
or die "expected a server time";
$job->grabbed_until(
$server_time + ( $worker_class->grab_for || 1 ) );
## Update the job in the database, and end the transaction.
if ( $driver->update( $job, { grabbed_until => $old_grabbed_until } )
< 1 )
{
## We lost the race to get this particular job--another worker must
## have got it and already updated it. Move on to the next job.
$TheSchwartz::T_LOST_RACE->() if $TheSchwartz::T_LOST_RACE;
next JOB;
}
## Now prepare the job, and return it.
my $handle = TheSchwartz::JobHandle->new(
{ dsn_hashed => $hashdsn,
jobid => $job->jobid,
}
);
$handle->client($client);
$job->handle($handle);
return $job;
}
return undef;
}
1;
__END__
=head1 NAME
MT::TheSchwartz - Movable Type
=head1 SYNOPSIS
use MT;
use MT::TheSchwartz;
my $mt = MT->new;
my $schwartz = MT::TheSchwartz->new(%cfg);
$schwartz->work_until_done;
=head1 DESCRIPTION
A subclass of C<TheSchwartz>, a job queue system. The MT subclass is
responsible for configuring TheSchwartz to work with the MT database
configuration and supplies TheSchwartz worker classes from the MT
registry.
=head1 METHODS
=head2 MT::TheSchwartz->instance
Returns the singleton instance for the C<MT::TheSchwartz> class.
=head2 MT::TheSchwartz->debug
Static (or instance) method that simply forwards the parameters given on
to the singleton instance.
=head2 MT::TheSchwartz->insert
Static (or instance) method that simply forwards the parameters given on
to the singleton instance.
=head2 $schwartz->default_logger($msg, $job)
Handles logging messages for C<TheSchwartz>. When a log message is issued,
this method takes over. In this case, this subclass suppresses the
"job completed" messages issued by C<TheSchwartz>.
=head2 $schwartz->work_periodically([$delay])
Invokes C<TheSchwartz> to process available jobs, then queries the MT
session table for available MT registered tasks that may be ready to
run. MT tasks such as publishing scheduled posts, expiration of
spam comments and trackbacks, etc. This routine runs in an infinite
loop-- after scheduled tasks are run, it will delay C<$delay> seconds
(default of 5 seconds), then checks for more Schwartz jobs to process.
=cut
|