root/branches/release-32/lib/MT/Worker/Publish.pm @ 1543

Revision 1543, 3.6 kB (checked in by auno, 21 months ago)

revert Publish Queue UI implementation.

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