root/branches/release-30/lib/MT/Worker/Publish.pm @ 1405

Revision 1405, 3.6 kB (checked in by bchoate, 21 months ago)

Display information about which job is actively publishing (along with any priority assignment) in debug output.

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