root/branches/release-26/lib/MT/Worker/Sync.pm @ 1174

Revision 1174, 3.6 kB (checked in by bchoate, 23 months ago)

Updated copyright year for source.

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