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

Revision 1912, 4.1 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::Sync;
8
9use strict;
10use base qw( TheSchwartz::Worker );
11use Time::HiRes qw(gettimeofday tv_interval);
12use TheSchwartz::Job;
13use MT::FileInfo;
14use MT::Util qw( log_time );
15
16sub work {
17    my $class = shift;
18    my TheSchwartz::Job $job = shift;
19
20    my $sync_set = [gettimeofday];
21
22    # Build this
23    my $mt = MT->instance;
24
25    my $rsync_cmd = MT->config("RsyncPath") || "rsync";
26    my $rsync_opt = $mt->config('RsyncOptions') || '';
27    my @targets = $mt->config('SyncTarget');
28
29    # We got triggered to build; lets find coalescing jobs
30    # and process them in one pass.
31
32    my @jobs;
33    push @jobs, $job;
34    if (my $key = $job->coalesce) {
35        while (my $job = MT::TheSchwartz->instance->find_job_with_coalescing_value($class, $key)) {
36            push @jobs, $job;
37        }
38    }
39
40    MT::TheSchwartz->debug("Syncing: " . scalar(@jobs) . " files...");
41
42    my @files;
43    my @static_fileinfo;
44    foreach $job (@jobs) {
45        my $fi_id = $job->uniqkey;
46        my $fi = MT::FileInfo->load($fi_id);
47
48        if ($fi && (-f $fi->file_path)) {
49            ##MT::TheSchwartz->debug("Syncing: " . RebuildQueue::Daemon::_summary($fi));
50            push @files, $fi->file_path;
51            unless ($fi->template_id) {
52                # static file
53                push @static_fileinfo, $fi;
54            }
55        } else {
56            if (!$fi) {
57                # Don't know where the FileInfo record went. Oh well.
58                $job->completed();
59            } else {
60                unless (-f $fi->file_path) {
61                    MT::TheSchwartz->debug("Warning: couldn't locate file: " . $fi->file_path);
62                    $job->permanent_failure("Couldn't locate file: " . $fi->file_path);
63                }
64            }
65        }
66    }
67
68    my $synced = 0;
69    if (@files) {
70        $synced = scalar @files;
71        require File::Spec;
72        my $file = File::Spec->catfile($mt->config('TempDir'), "publishq-rsync-$$.lst");
73        open FOUT, ">$file";
74        print FOUT join("\n", @files) . "\n";
75        close FOUT;
76        foreach my $target (@targets) {
77            my $cmd = "$rsync_cmd $rsync_opt --files-from=\"$file\" / \"$target\"";
78            MT::TheSchwartz->debug("Syncing files to $target...");
79            my $start = [gettimeofday];
80            my $res = system $cmd;
81            my $exit = $? >> 8;
82            if ($exit != 0) {
83                # TBD: notification to administrator
84                # At the very least, log to MT activity log.
85                my $errmsg = "Error during rsync of files in $file:\n"
86                    . "Command (exit code $res): $cmd";
87                MT::TheSchwartz->debug($errmsg);
88                require MT::Log;
89                $mt->log({
90                    message => $errmsg,
91                    metadata => log_time() . ' ' . $errmsg . "\nFiles affected:\n\t" . join("\n\t", @files),
92                    category => "sync",
93                    level => MT::Log::ERROR(),
94                });
95
96                $_->failed("Error during rsync") foreach @jobs;
97                return;
98            } else {
99                my $elapsed = sprintf("done! (%0.02fs)", tv_interval($start));
100                $mt->log({
101                    message => $mt->translate('Synchrnizing Files Done'),
102                    metadata => log_time() . ' '
103                        . $mt->translate('Done syncing files to [_1] ([_2])', $target, $elapsed),
104                    category => "sync",
105                    level => MT::Log::INFO(),
106                });
107                MT::TheSchwartz->debug($elapsed);
108            }
109        }
110        unlink $file;
111        # clear sync flags...
112        $_->remove foreach @static_fileinfo;
113        $_->completed() foreach @jobs;
114    }
115    if ($synced) {
116        MT::TheSchwartz->debug("-- set complete ($synced files in " . sprintf("%0.02f", tv_interval($sync_set)) . " seconds)");
117    }
118}
119
120sub grab_for { 60 }
121sub max_retries { 10 }
122sub retry_delay { 60 }
123
1241;
Note: See TracBrowser for help on using the browser.