root/branches/release-29/lib/MT/Worker/Publish.pm @ 1343

Revision 1343, 3.6 kB (checked in by mpaschal, 22 months ago)

Process jobs one and a time, instead of trying to yank them all
(We're only coalescing them to get hotter caches, I guess, anyway)
BugzID: 65407

  • Property svn:keywords set to Id Revision
Line 
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
7package MT::Worker::Publish;
8
9use strict;
10use base qw( TheSchwartz::Worker );
11
12use TheSchwartz::Job;
13use Time::HiRes qw(gettimeofday tv_interval);
14use MT::FileInfo;
15
16sub 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 = ($job);
27    my $job_iter;
28    if (my $key = $job->coalesce) {
29        $job_iter = sub {
30            shift @jobs || MT::TheSchwartz->instance->find_job_with_coalescing_value($class, $key);
31        };
32    }
33    else {
34        $job_iter = sub { shift @jobs };
35    }
36
37    my $sync = MT->config('SyncTarget');
38
39    my $throttles = $mt->{throttle} || {};
40    MT::TheSchwartz->debug($mt->translate("Publishing: [quant,_1,file,files]...", scalar(@jobs)));
41
42    my $start = [gettimeofday];
43    my $rebuilt = 0;
44
45    while (my $job = $job_iter->()) {
46        my $fi_id = $job->uniqkey;
47        my $fi = MT::FileInfo->load($fi_id);
48
49        # FileInfo record missing? Strange, but ignore and continue.
50        unless ($fi) {
51            $job->completed();
52            next;
53        }
54
55        # Important: prevents requeuing!
56        $fi->{from_queue} = 1;
57
58        my $mtime = (stat($fi->file_path))[9];
59
60        my $throttle = $throttles->{$fi->template_id}
61                    || $throttles->{lc $fi->archive_type};
62
63        # think about-- throttle by archive type or by template
64        if ($throttle) {
65            if (-f $fi->file_path) {
66                my $time = time;
67                if ($time - $mtime < $throttle) {
68                    # ignore rebuilding this file now; not enough
69                    # time has elapsed for rebuilding this file...
70                    $job->failed("Not ready to publish file " . $fi->file_path . " based on throttle rules.");
71                    next;
72                }
73            }
74        }
75
76        ## MT::TheSchwartz->debug("Publishing: " . RebuildQueue::Daemon::_summary($fi));
77        MT::TheSchwartz->debug("Publishing file " . $fi->file_path . "...");
78        my $res = $mt->publisher->rebuild_from_fileinfo($fi);
79        if (defined $res) {
80            if ( $sync ) {
81                my $sync_job = TheSchwartz::Job->new();
82                $sync_job->funcname('MT::Worker::Sync');
83                $sync_job->uniqkey($fi_id);
84                $sync_job->coalesce($job->coalesce) if $job->coalesce;
85                $sync_job->priority($job->priority) if $job->priority;
86                $job->replace_with($sync_job);
87            } else {
88                $job->completed();
89            }
90            $rebuilt++;
91        } else {
92            my $error = $mt->publisher->errstr;
93            my $errmsg = $mt->translate("Error rebuilding file [_1]" . $fi->file_path . ": " . $error);
94            MT::TheSchwartz->debug($errmsg);
95            $job->permanent_failure($errmsg);
96            require MT::Log;
97            $mt->log({
98                ($fi->blog_id ? ( blog_id => $fi->blog_id ) : () ),
99                message => $errmsg,
100                metadata => $errmsg . ":\n" . $error,
101                category => "publish",
102                level => MT::Log::ERROR(),
103            });
104        }
105    }
106
107    if ($rebuilt) {
108        MT::TheSchwartz->debug($mt->translate("-- set complete ([quant,_1,file,files] in [_2] seconds)", $rebuilt, sprintf("%0.02f", tv_interval($start))));
109    }
110
111}
112
113sub grab_for { 60 }
114sub max_retries { 0 }
115sub retry_delay { 60 }
116
1171;
Note: See TracBrowser for help on using the browser.