#!/usr/bin/perl -w

# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id: run-periodic-tasks 2380 2008-05-17 22:41:18Z bchoate $

use strict;

use lib 'lib', '../lib';

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,
);

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

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;

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