File: run-periodic-tasks

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 (113 lines) | stat: -rwxr-xr-x 2,827 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
#!/usr/bin/perl -w

# 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$

use strict;
use lib '/usr/share/movabletype/extlib';

my $daemonize = 0;
my $sleep     = 5;
my $help      = 0;
my $load      = 10;
my $verbose   = 0;
my $scoreboard;
my $randomize_jobs = 0;
my $trace_objects  = 0;

require Getopt::Long;
Getopt::Long::GetOptions(
    "daemon"       => \$daemonize,
    "sleep=i"      => \$sleep,
    "load=i"       => \$load,
    "scoreboard=s" => \$scoreboard,
    "randomly"     => \$randomize_jobs,
    "verbose"      => \$verbose,
    "leak"         => \$trace_objects,
);
require MT::TheSchwartz;

if ($trace_objects) {
    require Devel::Leak::Object;
    Devel::Leak::Object->import(qw{ GLOBAL_bless });
}

my $proc_process_table = eval {
    require Proc::ProcessTable;
    1;
};

$@ = undef;

my %cfg;
$cfg{verbose}    = $verbose;
$cfg{scoreboard} = $scoreboard;
$cfg{prioritize} = 1;
$cfg{randomize}  = $randomize_jobs;

require MT::Bootstrap;
require MT;

my $mt = MT->new() or die MT->errstr;
if ( defined(MT->config('RPTProcessCap')) && $proc_process_table ) {
    my $t = new Proc::ProcessTable;
    my $rpt_count;
    foreach my $p ( @{ $t->table } ) {
        my $cmd = $p->cmndline;
        if ( $cmd =~ /^perl/ && $cmd =~ /run-periodic-tasks/ ) {
            $rpt_count += 1;
        }
    }
    if ( $rpt_count > MT->config('RPTProcessCap') ) {
        $rpt_count = $rpt_count - 1;    # Don't report this RPT
        die "$rpt_count processes already running; cancelling RPT launch\n";
    }
}

if ( MT->config('RPTFreeMemoryLimit') ) {
    my $limit = MT->config('RPTFreeMemoryLimit');
    if ( $limit and ! MT::TheSchwartz::_has_enough_swap($limit) ) {
        die
            "Free memory below RPT limit; cancelling RPT launch\n";
    }
}
if ( MT->config('RPTFreeSwapLimit') ) {
    my $swaplimit = MT->config('RPTSwapMemoryLimit');
    if ( $swaplimit and ! MT::TheSchwartz::_has_enough_swap($swaplimit) ) {
        die
            "Free swap memory below RPT limit; cancelling RPT launch\n";
    }
}

$mt->{vtbl}                 = {};
$mt->{is_admin}             = 0;
$mt->{template_dir}         = 'cms';
$mt->{user_class}           = 'MT::Author';
$mt->{plugin_template_path} = 'tmpl';
$mt->run_callbacks( 'init_app', $mt );

my $client = eval {
    require MT::TheSchwartz;
    my $c = MT::TheSchwartz->new(%cfg);
    no warnings 'once';
    $TheSchwartz::FIND_JOB_BATCH_SIZE = $load;
    $c;
};
if ( ( my $error = $@ ) && $verbose ) {
    print STDERR "Error initializing TheSchwartz: $error\n";
}

if ( $daemonize && $client ) {
    $client->work_periodically($sleep);
}
else {

    # First, run periodic tasks
    $mt->run_tasks();
    $client->work_until_done if $client;
}

1;