root/branches/release-35/lib/MT/Worker/Publish.pm @ 1912

Revision 1912, 4.0 kB (checked in by fumiakiy, 20 months ago)

Added timestamp to log metadata when either periodic tasks finished, or TheSchwartz published or sync'ed files. BugId:66799

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