File: Publish.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (139 lines) | stat: -rw-r--r-- 4,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

package MT::Worker::Publish;

use strict;
use base qw( TheSchwartz::Worker );

use TheSchwartz::Job;
use Time::HiRes qw(gettimeofday tv_interval);
use MT::FileInfo;
use MT::PublishOption;
use MT::Util qw( log_time );

sub keep_exit_status_for {1}

sub work {
    my $class = shift;
    my TheSchwartz::Job $job = shift;

    # Build this
    my $mt = MT->instance;

    # reset publish timer; don't republish a file if it has
    # this or a later timestamp.
    $mt->publisher->start_time(time);

    # We got triggered to build; lets find coalescing jobs
    # and process them in one pass.

    my @jobs = ($job);
    my $job_iter;
    if ( my $key = $job->coalesce ) {
        $job_iter = sub {
            shift @jobs
                || MT::TheSchwartz->instance->find_job_with_coalescing_value(
                $class, $key );
        };
    }
    else {
        $job_iter = sub { shift @jobs };
    }

    my $sync = MT->config('SyncTarget');

    my $start   = [gettimeofday];
    my $rebuilt = 0;

    while ( my $job = $job_iter->() ) {
        my $fi_id = $job->uniqkey;
        my $fi    = MT::FileInfo->load($fi_id);

        # FileInfo record missing? Strange, but ignore and continue.
        unless ($fi) {
            $job->completed();
            next;
        }

        my $priority = $job->priority ? ", priority " . $job->priority : "";

        # Important: prevents requeuing!
        $fi->{from_queue} = 1;

        require MT::FileMgr;
        my $fmgr  = MT::FileMgr->new('Local');
        my $mtime = $fmgr->file_mod_time( $fi->file_path );

        my $throttle = MT::PublishOption::get_throttle($fi);

        # think about-- throttle by archive type or by template
        if ( $throttle->{type} == MT::PublishOption::SCHEDULED() ) {
            if ( -f $fi->file_path ) {
                my $time = time;
                if ( $time - $mtime < $throttle->{interval} ) {

                    # ignore rebuilding this file now; not enough
                    # time has elapsed for rebuilding this file...
                    $job->grabbed_until(0);
                    $job->driver->update($job);
                    next;
                }
            }
        }

        $job->debug( "Publishing " . $fi->file_path . $priority );

        my $res = $mt->publisher->rebuild_from_fileinfo($fi);
        if ( defined $res ) {
            if ($sync) {
                my $sync_job = TheSchwartz::Job->new();
                $sync_job->funcname('MT::Worker::Sync');
                $sync_job->uniqkey($fi_id);
                $sync_job->coalesce( $job->coalesce ) if $job->coalesce;
                $sync_job->priority( $job->priority ) if $job->priority;
                $job->replace_with($sync_job);
            }
            else {
                $job->completed();
            }
            $rebuilt++;
        }
        else {
            my $error  = $mt->publisher->errstr;
            my $errmsg = $mt->translate( "Error rebuilding file [_1]:[_2]",
                $fi->file_path, $error );
            MT::TheSchwartz->debug($errmsg);
            $job->permanent_failure($errmsg);
            require MT::Log;
            $mt->log(
                {   ( $fi->blog_id ? ( blog_id => $fi->blog_id ) : () ),
                    message  => $errmsg,
                    metadata => log_time() . ' ' . $errmsg . ":\n" . $error,
                    category => "publish",
                    level    => MT::Log::ERROR(),
                }
            );
        }
    }

    if ($rebuilt) {
        MT::TheSchwartz->debug(
            $mt->translate(
                "-- set complete ([quant,_1,file,files] in [_2] seconds)",
                $rebuilt,
                sprintf( "%0.02f", tv_interval($start) )
            )
        );
    }

}

sub grab_for    {60}
sub max_retries {0}
sub retry_delay {60}

1;