Show
Ignore:
Timestamp:
05/08/08 00:00:05 (23 months ago)
Author:
bchoate
Message:

Added 'randomly' flag to control whether jobs are shuffled when selecting work to do. Default does not randomize jobs within batch selected.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-38/lib/MT/TheSchwartz.pm

    r1474 r2272  
    1010use base qw( TheSchwartz ); 
    1111use MT::ObjectDriver::Driver::DBI; 
     12use List::Util qw( shuffle ); 
    1213 
    1314my $instance; 
     15 
     16our $RANDOMIZE_JOBS = 0; 
    1417 
    1518sub instance { 
     
    3336    my (%param) = @_; 
    3437    my $workers = delete $param{workers} if exists $param{workers}; 
     38    $RANDOMIZE_JOBS = delete $param{randomize} if exists $param{randomize}; 
    3539 
    3640    my $client = $class->SUPER::new(%param); 
     
    159163} 
    160164 
     165sub _grab_a_job { 
     166    my TheSchwartz $client = shift; 
     167    my $hashdsn = shift; 
     168    my $driver = $client->driver_for($hashdsn); 
     169 
     170    ## Got some jobs! Randomize them to avoid contention between workers. 
     171    my @jobs = $RANDOMIZE_JOBS ? shuffle(@_) : @_; 
     172 
     173  JOB: 
     174    while (my $job = shift @jobs) { 
     175        ## Convert the funcid to a funcname, based on this database's map. 
     176        $job->funcname( $client->funcid_to_name($driver, $hashdsn, $job->funcid) ); 
     177 
     178        ## Update the job's grabbed_until column so that 
     179        ## no one else takes it. 
     180        my $worker_class = $job->funcname; 
     181        my $old_grabbed_until = $job->grabbed_until; 
     182 
     183        my $server_time = $client->get_server_time($driver) 
     184            or die "expected a server time"; 
     185 
     186        $job->grabbed_until($server_time + ($worker_class->grab_for || 1)); 
     187 
     188        ## Update the job in the database, and end the transaction. 
     189        if ($driver->update($job, { grabbed_until => $old_grabbed_until }) < 1) { 
     190            ## We lost the race to get this particular job--another worker must 
     191            ## have got it and already updated it. Move on to the next job. 
     192            $TheSchwartz::T_LOST_RACE->() if $TheSchwartz::T_LOST_RACE; 
     193            next JOB; 
     194        } 
     195 
     196        ## Now prepare the job, and return it. 
     197        my $handle = TheSchwartz::JobHandle->new({ 
     198            dsn_hashed => $hashdsn, 
     199            jobid      => $job->jobid, 
     200        }); 
     201        $handle->client($client); 
     202        $job->handle($handle); 
     203        return $job; 
     204    } 
     205 
     206    return undef; 
     207} 
     208 
    1612091;