| 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 | use MT::PublishOption; |
|---|
| 16 | use MT::Util qw( log_time ); |
|---|
| 17 | |
|---|
| 18 | sub keep_exit_status_for { 1 } |
|---|
| 19 | |
|---|
| 20 | sub work { |
|---|
| 21 | my $class = shift; |
|---|
| 22 | my TheSchwartz::Job $job = shift; |
|---|
| 23 | |
|---|
| 24 | # Build this |
|---|
| 25 | my $mt = MT->instance; |
|---|
| 26 | |
|---|
| 27 | # reset publish timer; don't republish a file if it has |
|---|
| 28 | # this or a later timestamp. |
|---|
| 29 | $mt->publisher->start_time( time ); |
|---|
| 30 | |
|---|
| 31 | # We got triggered to build; lets find coalescing jobs |
|---|
| 32 | # and process them in one pass. |
|---|
| 33 | |
|---|
| 34 | my @jobs = ($job); |
|---|
| 35 | my $job_iter; |
|---|
| 36 | if (my $key = $job->coalesce) { |
|---|
| 37 | $job_iter = sub { |
|---|
| 38 | shift @jobs || MT::TheSchwartz->instance->find_job_with_coalescing_value($class, $key); |
|---|
| 39 | }; |
|---|
| 40 | } |
|---|
| 41 | else { |
|---|
| 42 | $job_iter = sub { shift @jobs }; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | my $sync = MT->config('SyncTarget'); |
|---|
| 46 | |
|---|
| 47 | my $start = [gettimeofday]; |
|---|
| 48 | my $rebuilt = 0; |
|---|
| 49 | |
|---|
| 50 | while (my $job = $job_iter->()) { |
|---|
| 51 | my $fi_id = $job->uniqkey; |
|---|
| 52 | my $fi = MT::FileInfo->load($fi_id); |
|---|
| 53 | |
|---|
| 54 | # FileInfo record missing? Strange, but ignore and continue. |
|---|
| 55 | unless ($fi) { |
|---|
| 56 | $job->completed(); |
|---|
| 57 | next; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | my $priority = $job->priority ? ", priority " . $job->priority : ""; |
|---|
| 61 | |
|---|
| 62 | # Important: prevents requeuing! |
|---|
| 63 | $fi->{from_queue} = 1; |
|---|
| 64 | |
|---|
| 65 | my $mtime = (stat($fi->file_path))[9]; |
|---|
| 66 | |
|---|
| 67 | my $throttle = MT::PublishOption::get_throttle($fi); |
|---|
| 68 | |
|---|
| 69 | # think about-- throttle by archive type or by template |
|---|
| 70 | if ($throttle->{type} == MT::PublishOption::SCHEDULED() ) { |
|---|
| 71 | if (-f $fi->file_path) { |
|---|
| 72 | my $time = time; |
|---|
| 73 | if ($time - $mtime < $throttle->{interval}) { |
|---|
| 74 | # ignore rebuilding this file now; not enough |
|---|
| 75 | # time has elapsed for rebuilding this file... |
|---|
| 76 | $job->grabbed_until(0); |
|---|
| 77 | $job->driver->update($job); |
|---|
| 78 | next; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | $job->debug("Publishing " . $fi->file_path . $priority); |
|---|
| 84 | |
|---|
| 85 | my $res = $mt->publisher->rebuild_from_fileinfo($fi); |
|---|
| 86 | if (defined $res) { |
|---|
| 87 | if ( $sync ) { |
|---|
| 88 | my $sync_job = TheSchwartz::Job->new(); |
|---|
| 89 | $sync_job->funcname('MT::Worker::Sync'); |
|---|
| 90 | $sync_job->uniqkey($fi_id); |
|---|
| 91 | $sync_job->coalesce($job->coalesce) if $job->coalesce; |
|---|
| 92 | $sync_job->priority($job->priority) if $job->priority; |
|---|
| 93 | $job->replace_with($sync_job); |
|---|
| 94 | } else { |
|---|
| 95 | $job->completed(); |
|---|
| 96 | } |
|---|
| 97 | $rebuilt++; |
|---|
| 98 | } else { |
|---|
| 99 | my $error = $mt->publisher->errstr; |
|---|
| 100 | my $errmsg = $mt->translate("Error rebuilding file [_1]" . $fi->file_path . ": " . $error); |
|---|
| 101 | MT::TheSchwartz->debug($errmsg); |
|---|
| 102 | $job->permanent_failure($errmsg); |
|---|
| 103 | require MT::Log; |
|---|
| 104 | $mt->log({ |
|---|
| 105 | ($fi->blog_id ? ( blog_id => $fi->blog_id ) : () ), |
|---|
| 106 | message => $errmsg, |
|---|
| 107 | metadata => log_time() . ' ' . $errmsg . ":\n" . $error, |
|---|
| 108 | category => "publish", |
|---|
| 109 | level => MT::Log::ERROR(), |
|---|
| 110 | }); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | if ($rebuilt) { |
|---|
| 115 | MT::TheSchwartz->debug($mt->translate("-- set complete ([quant,_1,file,files] in [_2] seconds)", $rebuilt, sprintf("%0.02f", tv_interval($start)))); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | sub grab_for { 60 } |
|---|
| 121 | sub max_retries { 0 } |
|---|
| 122 | sub retry_delay { 60 } |
|---|
| 123 | |
|---|
| 124 | 1; |
|---|