| 1 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 2 | # This program is distributed under the terms of the |
|---|
| 3 | # GNU General Public License, version 2. |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | |
|---|
| 7 | package MT::Worker::Publish; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use base qw( TheSchwartz::Worker ); |
|---|
| 11 | |
|---|
| 12 | use TheSchwartz::Job; |
|---|
| 13 | use Time::HiRes qw(gettimeofday tv_interval); |
|---|
| 14 | use MT::FileInfo; |
|---|
| 15 | |
|---|
| 16 | sub work { |
|---|
| 17 | my $class = shift; |
|---|
| 18 | my TheSchwartz::Job $job = shift; |
|---|
| 19 | |
|---|
| 20 | # Build this |
|---|
| 21 | my $mt = MT->instance; |
|---|
| 22 | |
|---|
| 23 | # We got triggered to build; lets find coalescing jobs |
|---|
| 24 | # and process them in one pass. |
|---|
| 25 | |
|---|
| 26 | my @jobs; |
|---|
| 27 | push @jobs, $job; |
|---|
| 28 | if (my $key = $job->coalesce) { |
|---|
| 29 | while (my $job = MT::TheSchwartz->instance->find_job_with_coalescing_value($class, $key)) { |
|---|
| 30 | push @jobs, $job; |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | my $sync = MT->config('SyncTarget'); |
|---|
| 35 | |
|---|
| 36 | my $throttles = $mt->{throttle} || {}; |
|---|
| 37 | MT::TheSchwartz->debug($mt->translate("Publishing: [quant,_1,file,files]...", scalar(@jobs))); |
|---|
| 38 | |
|---|
| 39 | my $start = [gettimeofday]; |
|---|
| 40 | my $rebuilt = 0; |
|---|
| 41 | |
|---|
| 42 | foreach $job (@jobs) { |
|---|
| 43 | my $fi_id = $job->uniqkey; |
|---|
| 44 | my $fi = MT::FileInfo->load($fi_id); |
|---|
| 45 | |
|---|
| 46 | # FileInfo record missing? Strange, but ignore and continue. |
|---|
| 47 | unless ($fi) { |
|---|
| 48 | $job->completed(); |
|---|
| 49 | next; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | # Important: prevents requeuing! |
|---|
| 53 | $fi->{from_queue} = 1; |
|---|
| 54 | |
|---|
| 55 | my $mtime = (stat($fi->file_path))[9]; |
|---|
| 56 | |
|---|
| 57 | my $throttle = $throttles->{$fi->template_id} |
|---|
| 58 | || $throttles->{lc $fi->archive_type}; |
|---|
| 59 | |
|---|
| 60 | # think about-- throttle by archive type or by template |
|---|
| 61 | if ($throttle) { |
|---|
| 62 | if (-f $fi->file_path) { |
|---|
| 63 | my $time = time; |
|---|
| 64 | if ($time - $mtime < $throttle) { |
|---|
| 65 | # ignore rebuilding this file now; not enough |
|---|
| 66 | # time has elapsed for rebuilding this file... |
|---|
| 67 | $job->failed("Not ready to publish file " . $fi->file_path . " based on throttle rules."); |
|---|
| 68 | next; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | ## MT::TheSchwartz->debug("Publishing: " . RebuildQueue::Daemon::_summary($fi)); |
|---|
| 74 | MT::TheSchwartz->debug("Publishing file " . $fi->file_path . "..."); |
|---|
| 75 | my $res = $mt->publisher->rebuild_from_fileinfo($fi); |
|---|
| 76 | if (defined $res) { |
|---|
| 77 | if ( $sync ) { |
|---|
| 78 | my $sync_job = TheSchwartz::Job->new(); |
|---|
| 79 | $sync_job->funcname('MT::Worker::Sync'); |
|---|
| 80 | $sync_job->uniqkey($fi_id); |
|---|
| 81 | $sync_job->coalesce($job->coalesce) if $job->coalesce; |
|---|
| 82 | $sync_job->priority($job->priority) if $job->priority; |
|---|
| 83 | $job->replace_with($sync_job); |
|---|
| 84 | } else { |
|---|
| 85 | $job->completed(); |
|---|
| 86 | } |
|---|
| 87 | $rebuilt++; |
|---|
| 88 | } else { |
|---|
| 89 | my $error = $mt->publisher->errstr; |
|---|
| 90 | my $errmsg = $mt->translate("Error rebuilding file [_1]" . $fi->file_path . ": " . $error); |
|---|
| 91 | MT::TheSchwartz->debug($errmsg); |
|---|
| 92 | $job->permanent_failure($errmsg); |
|---|
| 93 | require MT::Log; |
|---|
| 94 | $mt->log({ |
|---|
| 95 | ($fi->blog_id ? ( blog_id => $fi->blog_id ) : () ), |
|---|
| 96 | message => $errmsg, |
|---|
| 97 | metadata => $errmsg . ":\n" . $error, |
|---|
| 98 | category => "publish", |
|---|
| 99 | level => MT::Log::ERROR(), |
|---|
| 100 | }); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | if ($rebuilt) { |
|---|
| 105 | MT::TheSchwartz->debug($mt->translate("-- set complete ([quant,_1,file,files] in [_2] seconds)", $rebuilt, sprintf("%0.02f", tv_interval($start)))); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | sub grab_for { 60 } |
|---|
| 111 | sub max_retries { 0 } |
|---|
| 112 | sub retry_delay { 60 } |
|---|
| 113 | |
|---|
| 114 | 1; |
|---|