| 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 | |
|---|
| 9 | use strict; |
|---|
| 10 | |
|---|
| 11 | use lib 'lib', '../lib', 'extlib', '../extlib'; |
|---|
| 12 | |
|---|
| 13 | my $daemonize = 0; |
|---|
| 14 | my $sleep = 5; |
|---|
| 15 | my $help = 0; |
|---|
| 16 | my $load = 10; |
|---|
| 17 | my $verbose = 0; |
|---|
| 18 | my $scoreboard; |
|---|
| 19 | my $randomize_jobs = 0; |
|---|
| 20 | my $trace_objects = 0; |
|---|
| 21 | |
|---|
| 22 | require Getopt::Long; |
|---|
| 23 | Getopt::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 | ); |
|---|
| 32 | require MT::TheSchwartz; |
|---|
| 33 | |
|---|
| 34 | if ($trace_objects) { |
|---|
| 35 | require Devel::Leak::Object; |
|---|
| 36 | Devel::Leak::Object->import(qw{ GLOBAL_bless }); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | my $proc_process_table = eval { |
|---|
| 40 | require Proc::ProcessTable; |
|---|
| 41 | 1; |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | $@ = undef; |
|---|
| 45 | |
|---|
| 46 | my %cfg; |
|---|
| 47 | $cfg{verbose} = $verbose; |
|---|
| 48 | $cfg{scoreboard} = $scoreboard; |
|---|
| 49 | $cfg{prioritize} = 1; |
|---|
| 50 | $cfg{randomize} = $randomize_jobs; |
|---|
| 51 | |
|---|
| 52 | require MT::Bootstrap; |
|---|
| 53 | require MT; |
|---|
| 54 | |
|---|
| 55 | my $mt = MT->new() or die MT->errstr; |
|---|
| 56 | if ( 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 | |
|---|
| 71 | if ( 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 | } |
|---|
| 78 | if ( 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 | |
|---|
| 93 | my $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 | }; |
|---|
| 100 | if ( ( my $error = $@ ) && $verbose ) { |
|---|
| 101 | print STDERR "Error initializing TheSchwartz: $error\n"; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | if ( $daemonize && $client ) { |
|---|
| 105 | $client->work_periodically($sleep); |
|---|
| 106 | } |
|---|
| 107 | else { |
|---|
| 108 | |
|---|
| 109 | # First, run periodic tasks |
|---|
| 110 | $mt->run_tasks(); |
|---|
| 111 | $client->work_until_done if $client; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | 1; |
|---|