root/trunk/tools/run-periodic-tasks

Revision 3886, 2.8 kB (checked in by fumiakiy, 5 months ago)

Merged fringale down to trunk. "svn merge -r 3460:3877 http://code.sixapart.com/svn/movabletype/branches/fringale ."

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -w
2
3# Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd.
4# This program is distributed under the terms of the
5# GNU General Public License, version 2.
6#
7# $Id$
8
9use strict;
10
11use lib 'lib', '../lib', 'extlib', '../extlib';
12
13my $daemonize = 0;
14my $sleep     = 5;
15my $help      = 0;
16my $load      = 10;
17my $verbose   = 0;
18my $scoreboard;
19my $randomize_jobs = 0;
20my $trace_objects  = 0;
21
22require Getopt::Long;
23Getopt::Long::GetOptions(
24    "daemon"       => \$daemonize,
25    "sleep=i"      => \$sleep,
26    "load=i"       => \$load,
27    "scoreboard=s" => \$scoreboard,
28    "randomly"     => \$randomize_jobs,
29    "verbose"      => \$verbose,
30    "leak"         => \$trace_objects,
31);
32require MT::TheSchwartz;
33
34if ($trace_objects) {
35    require Devel::Leak::Object;
36    Devel::Leak::Object->import(qw{ GLOBAL_bless });
37}
38
39my $proc_process_table = eval {
40    require Proc::ProcessTable;
41    1;
42};
43
44$@ = undef;
45
46my %cfg;
47$cfg{verbose}    = $verbose;
48$cfg{scoreboard} = $scoreboard;
49$cfg{prioritize} = 1;
50$cfg{randomize}  = $randomize_jobs;
51
52require MT::Bootstrap;
53require MT;
54
55my $mt = MT->new() or die MT->errstr;
56if ( defined(MT->config('RPTProcessCap')) && $proc_process_table ) {
57    my $t = new Proc::ProcessTable;
58    my $rpt_count;
59    foreach my $p ( @{ $t->table } ) {
60        my $cmd = $p->cmndline;
61        if ( $cmd =~ /^perl/ && $cmd =~ /run-periodic-tasks/ ) {
62            $rpt_count += 1;
63        }
64    }
65    if ( $rpt_count > MT->config('RPTProcessCap') ) {
66        $rpt_count = $rpt_count - 1;    # Don't report this RPT
67        die "$rpt_count processes already running; cancelling RPT launch\n";
68    }
69}
70
71if ( MT->config('RPTFreeMemoryLimit') ) {
72    my $limit = MT->config('RPTFreeMemoryLimit');
73    if ( $limit and ! MT::TheSchwartz::_has_enough_swap($limit) ) {
74        die
75            "Free memory below RPT limit; cancelling RPT launch\n";
76    }
77}
78if ( MT->config('RPTFreeSwapLimit') ) {
79    my $swaplimit = MT->config('RPTSwapMemoryLimit');
80    if ( $swaplimit and ! MT::TheSchwartz::_has_enough_swap($swaplimit) ) {
81        die
82            "Free swap memory below RPT limit; cancelling RPT launch\n";
83    }
84}
85
86$mt->{vtbl}                 = {};
87$mt->{is_admin}             = 0;
88$mt->{template_dir}         = 'cms';
89$mt->{user_class}           = 'MT::Author';
90$mt->{plugin_template_path} = 'tmpl';
91$mt->run_callbacks( 'init_app', $mt );
92
93my $client = eval {
94    require MT::TheSchwartz;
95    my $c = MT::TheSchwartz->new(%cfg);
96    no warnings 'once';
97    $TheSchwartz::FIND_JOB_BATCH_SIZE = $load;
98    $c;
99};
100if ( ( my $error = $@ ) && $verbose ) {
101    print STDERR "Error initializing TheSchwartz: $error\n";
102}
103
104if ( $daemonize && $client ) {
105    $client->work_periodically($sleep);
106}
107else {
108
109    # First, run periodic tasks
110    $mt->run_tasks();
111    $client->work_until_done if $client;
112}
113
1141;
Note: See TracBrowser for help on using the browser.