[libtheschwartz-perl] 01/43: [svn-inject] Installing original source of libtheschwartz-perl
dom at earth.li
dom at earth.li
Mon May 9 20:10:49 UTC 2016
This is an automated email from the git hooks/post-receive script.
dom pushed a commit to branch master
in repository libtheschwartz-perl.
commit 4171337ad9d7c8908ed8ad0110cda54abd0c89b3
Author: Dominic Hargreaves <dom at earth.li>
Date: Thu Dec 20 22:53:21 2007 +0000
[svn-inject] Installing original source of libtheschwartz-perl
---
CHANGES | 9 +
MANIFEST | 52 +++
MANIFEST.SKIP | 30 ++
META.yml | 18 +
Makefile.PL | 18 +
bin/schwartzmon | 263 ++++++++++++
doc/http-mappings.txt | 7 +
doc/notes.txt | 8 +
doc/schema.sql | 47 +++
extras/TheSchwartz.spec | 34 ++
extras/check_schwartz | 64 +++
inc/Module/AutoInstall.pm | 768 +++++++++++++++++++++++++++++++++++
inc/Module/Install.pm | 281 +++++++++++++
inc/Module/Install/AutoInstall.pm | 61 +++
inc/Module/Install/Base.pm | 70 ++++
inc/Module/Install/Can.pm | 82 ++++
inc/Module/Install/Fetch.pm | 93 +++++
inc/Module/Install/Include.pm | 34 ++
inc/Module/Install/Makefile.pm | 212 ++++++++++
inc/Module/Install/Metadata.pm | 323 +++++++++++++++
inc/Module/Install/Scripts.pm | 50 +++
inc/Module/Install/Win32.pm | 65 +++
inc/Module/Install/WriteAll.pm | 43 ++
lib/TheSchwartz.pm | 824 ++++++++++++++++++++++++++++++++++++++
lib/TheSchwartz/Error.pm | 12 +
lib/TheSchwartz/ExitStatus.pm | 14 +
lib/TheSchwartz/FuncMap.pm | 36 ++
lib/TheSchwartz/Job.pm | 499 +++++++++++++++++++++++
lib/TheSchwartz/JobHandle.pm | 68 ++++
lib/TheSchwartz/Worker.pm | 141 +++++++
t/05-job-ctor.t | 69 ++++
t/api.t | 104 +++++
t/cleanup.t | 87 ++++
t/client-time-unsync.t | 108 +++++
t/coalesce.t | 74 ++++
t/dead-dbs.t | 51 +++
t/empty-db.t | 35 ++
t/evenly-distribute.t | 61 +++
t/fail-working-multiple.t | 61 +++
t/funcid.t | 44 ++
t/grab-race.t | 88 ++++
t/high-funcid-starvation.t | 54 +++
t/insert-and-do.t | 152 +++++++
t/lib/db-common.pl | 166 ++++++++
t/parallel-workers.t | 106 +++++
t/pod-coverage.t | 30 ++
t/pod.t | 6 +
t/replace-with.t | 75 ++++
t/retry-delay.t | 70 ++++
t/schema-sqlite.sql | 33 ++
t/unique.t | 46 +++
t/work-before-funcids-exist.t | 59 +++
52 files changed, 5805 insertions(+)
diff --git a/CHANGES b/CHANGES
new file mode 100644
index 0000000..5161eb1
--- /dev/null
+++ b/CHANGES
@@ -0,0 +1,9 @@
+1.04 (2007-05-22)
+
+ - no code changes, just packaging/dep/test fixes, as pointed out
+ by Dan Rench <drench at dren.ch>
+
+1.03
+
+ - first packaged release, now that all SixApart products have been
+ using this heavily for quite some time. it's overdue.
diff --git a/MANIFEST b/MANIFEST
new file mode 100644
index 0000000..d69416c
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,52 @@
+bin/schwartzmon
+CHANGES
+doc/http-mappings.txt
+doc/notes.txt
+doc/schema.sql
+extras/check_schwartz
+extras/TheSchwartz.spec
+inc/Module/AutoInstall.pm
+inc/Module/Install.pm
+inc/Module/Install/AutoInstall.pm
+inc/Module/Install/Base.pm
+inc/Module/Install/Can.pm
+inc/Module/Install/Fetch.pm
+inc/Module/Install/Include.pm
+inc/Module/Install/Makefile.pm
+inc/Module/Install/Metadata.pm
+inc/Module/Install/Scripts.pm
+inc/Module/Install/Win32.pm
+inc/Module/Install/WriteAll.pm
+lib/TheSchwartz.pm
+lib/TheSchwartz/Error.pm
+lib/TheSchwartz/ExitStatus.pm
+lib/TheSchwartz/FuncMap.pm
+lib/TheSchwartz/Job.pm
+lib/TheSchwartz/JobHandle.pm
+lib/TheSchwartz/Worker.pm
+Makefile.PL
+MANIFEST This list of files
+MANIFEST.SKIP
+META.yml
+t/05-job-ctor.t
+t/api.t
+t/cleanup.t
+t/client-time-unsync.t
+t/coalesce.t
+t/dead-dbs.t
+t/empty-db.t
+t/evenly-distribute.t
+t/fail-working-multiple.t
+t/funcid.t
+t/grab-race.t
+t/high-funcid-starvation.t
+t/insert-and-do.t
+t/lib/db-common.pl
+t/parallel-workers.t
+t/pod-coverage.t
+t/pod.t
+t/replace-with.t
+t/retry-delay.t
+t/schema-sqlite.sql
+t/unique.t
+t/work-before-funcids-exist.t
diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP
new file mode 100644
index 0000000..fdb17dd
--- /dev/null
+++ b/MANIFEST.SKIP
@@ -0,0 +1,30 @@
+MANIFEST.bak
+ts1.db
+ts2.db
+build-stamp
+install-stamp
+.shipit
+svn-commit.tmp
+
+# Avoid version control files.
+\bRCS\b
+\bCVS\b
+,v$
+\B\.svn\b
+
+# Avoid Makemaker generated and utility files.
+\bMANIFEST\.bak
+\bMakefile$
+\bblib/
+\bMakeMaker-\d
+\bpm_to_blib$
+
+# Avoid Module::Build generated and utility files.
+\bBuild$
+\b_build/
+
+# Avoid temp and backup files.
+~$
+\.old$
+\#$
+\b\.#
diff --git a/META.yml b/META.yml
new file mode 100644
index 0000000..4ec1fdd
--- /dev/null
+++ b/META.yml
@@ -0,0 +1,18 @@
+---
+author: Six Apart <cpan at sixapart.com>
+distribution_type: module
+generated_by: Module::Install version 0.65
+license: perl
+meta-spec:
+ url: http://module-build.sourceforge.net/META-spec-v1.3.html
+ version: 1.3
+name: TheSchwartz
+no_index:
+ directory:
+ - inc
+ - t
+requires:
+ Data::ObjectDriver: 0.04
+ Digest::MD5: 0
+ Storable: 0
+version: 1.04
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..9526630
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,18 @@
+# $Id$
+
+use 5.008;
+use inc::Module::Install;
+
+name('TheSchwartz');
+version_from('lib/TheSchwartz.pm');
+author('Six Apart <cpan at sixapart.com>');
+license('perl');
+
+requires('Data::ObjectDriver' => '0.04');
+requires('Digest::MD5');
+requires('Storable');
+
+install_script('bin/schwartzmon');
+
+auto_install();
+WriteAll();
diff --git a/bin/schwartzmon b/bin/schwartzmon
new file mode 100755
index 0000000..30feea2
--- /dev/null
+++ b/bin/schwartzmon
@@ -0,0 +1,263 @@
+#!/usr/bin/perl
+use strict;
+use DBI;
+use Getopt::Long;
+
+my $dbname = "schwartz";
+my $user = "root";
+my $pass = "";
+my $job = "";
+my $max_age = 0;
+my $max_count = 0;
+
+sub usage {
+ die <<USAGE;
+Usage: schwartzmon <command> [OPTS]
+
+Possible commands:
+ queues View past-due job queue depths. (default cmd)
+ errors View errors.
+
+Global options:
+ --job=<JOBNAME> Only look at one specific job name. Else all are considered.
+ --user=<user> Connect to the database as this user
+ --pass=<pass> Connect to the database with this password
+ --database=<db> Connect to this database
+ --dsn=<dsn> Connect to the database using this DSN
+
+Options for 'queues' command:
+ --maxage=<n> Don't complain if age of overdue job queue is <= 'n'
+ --maxcount=<n> Don't complain if depth of overdue job queue is <= 'n'
+
+Options for 'errors' command:
+ --follow | -f Like 'tail -f' for tracking the error log
+ --last=n Show last 'n' errors from log
+ --inlast=n Show errors in last 'n' seconds
+
+Verbosity:
+ if no alerts, nothing is printed, and exit status is 0.
+
+Exit status:
+ 0 if no alerts,
+ non-zero if there are alerts, in which case the alerts are printed.
+
+USAGE
+
+}
+
+my $opt_help = 0;
+my ($opt_follow, $opt_last, $opt_inlast, $opt_func, $dsn);
+usage() unless GetOptions("job=s" => \$job,
+ "maxage=i" => \$max_age,
+ "maxcount=i" => \$max_count,
+ "help" => \$opt_help,
+ "follow|f" => \$opt_follow,
+ "last=i" => \$opt_last,
+ "inlast=i" => \$opt_inlast,
+ "user=s" => \$user,
+ "pass=s" => \$pass,
+ "dsn=s" => \$dsn,
+ "database=s" => \$dbname,
+ "func=s" => \$opt_func,
+ );
+usage() if $opt_help;
+
+my $cmd = shift || "queues";
+usage() unless $cmd =~ /^queues|errors$/;
+
+my $dbset = DBSet->new;
+
+$dsn ||= "DBI:mysql:$dbname";
+
+$dbset->add(DBHandle->new({ dsn => $dsn, user => $user, pass => $pass}));
+
+if ($cmd eq "queues") { queues($dbset); }
+if ($cmd eq "errors") { errors($dbset); }
+exit 0;
+
+#################
+
+sub queues {
+ my $dbs = shift;
+ my $some_alert = 0;
+ $dbs->foreach(sub {
+ my $db = shift;
+ my $dbh = $db->dbh or next;
+
+ my $funcmap = $dbh->selectall_hashref("SELECT funcid, funcname FROM funcmap", "funcid");
+
+ foreach my $funcid (sort { $funcmap->{$a}{funcname} cmp $funcmap->{$b}{funcname} } keys %$funcmap) {
+ my $funcname = $funcmap->{$funcid}{funcname};
+ next if $job && $funcname ne $job;
+
+ my $now = time();
+ my $inf = $dbh->selectrow_hashref("SELECT COUNT(*) as 'ct', MIN(run_after) 'oldest' FROM job WHERE funcid=? AND run_after <= $now",
+ undef, $funcid);
+ my $behind = $inf->{ct} ? ($now - $inf->{oldest}) : 0;
+
+ # okay by default, then we apply rules:
+ my $okay = 1;
+ $okay = 0 if $behind > $max_age;
+ $okay = 0 if $inf->{ct} > $max_count;
+ next if $okay;
+ $some_alert = 1;
+
+ print "$funcname\n";
+ print " outstanding: $inf->{ct}\n";
+ print " behind_secs: $behind\n";
+ }
+ });
+ exit($some_alert ? 1 : 0);
+}
+
+sub errors {
+ my $dbs = shift;
+
+ if ($opt_follow) {
+ follow_errors($dbs);
+ }
+
+ $opt_last = 100 unless $opt_last || $opt_inlast;
+
+ my @rows;
+ $dbs->foreach(sub {
+ my $db = shift;
+ my $dbh = $db->dbh
+ or next;
+
+ my $extra_where = '';
+ if ($opt_func) {
+ my $funcid = $db->funcid_of_func($opt_func) || 0;
+ $extra_where = "AND funcid=$funcid";
+ }
+
+ my $sql;
+ if ($opt_last) {
+ $sql = "SELECT error_time, jobid, message FROM error WHERE 1=1 $extra_where " .
+ "ORDER BY error_time DESC LIMIT $opt_last";
+ } elsif ($opt_inlast) {
+ my $since = time() - $opt_inlast;
+ $sql = "SELECT error_time, jobid, message FROM error WHERE error_time >= $since $extra_where " .
+ "ORDER BY error_time LIMIT 50000";
+ }
+
+
+ my $sth = $dbh->prepare($sql);
+ $sth->execute;
+ push @rows, $_ while $_ = $sth->fetchrow_hashref;
+ });
+
+ @rows = sort { $a->{error_time} <=> $b->{error_time} } @rows;
+ if ($opt_last && @rows > $opt_last) {
+ shift @rows while @rows > $opt_last;
+ }
+
+ foreach my $r (@rows) {
+ print_error($r);
+ }
+
+}
+
+sub follow_errors {
+ my $dbs = shift;
+
+ while (1) {
+ $dbs->foreach(sub {
+ my $db = shift;
+ my $dbh = $db->dbh
+ or next;
+ my $notes = $db->notes;
+
+ my $lastmax = $notes->{lastmax} || time();
+ my $seen = $notes->{seen} ||= {};
+
+ my $extra_where = '';
+ if ($opt_func) {
+ my $funcid = $db->funcid_of_func($opt_func) || 0;
+ $extra_where = "AND funcid=$funcid";
+ }
+
+ my $sth = $dbh->prepare("SELECT error_time, jobid, message FROM error WHERE error_time >= ? $extra_where ORDER BY error_time");
+ $sth->execute($lastmax);
+ my @errors;
+ push @errors, $_ while $_ = $sth->fetchrow_hashref;
+
+ my $newmax = $lastmax;
+ foreach my $r (@errors) {
+ my $sig = join(",", map { $_, $r->{$_} } sort keys %$r);
+ next if $seen->{$sig};
+ $seen->{$sig} = $r->{error_time};
+ print_error($r);
+ $newmax = $r->{error_time} if $r->{error_time} > $newmax;
+ }
+
+ $notes->{lastmax} = $newmax;
+
+ foreach my $sig (keys %$seen) {
+ my $time = $seen->{$sig};
+ delete $seen->{$sig} if $time < $newmax;
+ }
+
+ });
+ sleep 1;
+ }
+}
+
+sub print_error {
+ my $r = shift;
+ my $msg = $r->{message};
+ $msg =~ s/\s+$//g;
+ printf scalar(localtime($r->{error_time})) . " [$r->{jobid}]: $msg\n";
+}
+
+
+package DBSet;
+
+sub new {
+ return bless [];
+}
+
+sub add {
+ my ($self, $db) = @_;
+ push @$self, $db;
+}
+
+sub foreach {
+ my ($self, $cb) = @_;
+ foreach my $dbh (@$self) {
+ $cb->($dbh);
+ }
+}
+
+package DBHandle;
+
+sub new {
+ my ($class, $dbinf) = @_;
+ return bless $dbinf, $class;
+}
+
+sub notes {
+ my $self = shift;
+ return $self->{notes} ||= {};
+}
+
+# returns DBI handle
+sub dbh {
+ my $self = shift;
+ return $self->{_dbh} ||=
+ DBI->connect($self->{dsn}, $self->{user}, $self->{pass})
+
+}
+
+sub funcid_of_func {
+ my ($self, $func) = @_;
+ my $notes = $self->notes;
+ return $notes->{"funcid_of_$func"} if exists $notes->{"funcid_of_$func"};
+
+ my $dbh = $self->dbh;
+ return $notes->{"funcid_of_$func"} =
+ $dbh->selectrow_array("SELECT funcid FROM funcmap WHERE funcname=?",
+ undef, $func);
+}
+
+
diff --git a/doc/http-mappings.txt b/doc/http-mappings.txt
new file mode 100644
index 0000000..0442ed2
--- /dev/null
+++ b/doc/http-mappings.txt
@@ -0,0 +1,7 @@
+FUTURE: provide HTTP server interface to all TheSchwartz commands.
+
+insert
+insert_jobs
+grab_job
+grab_job_from_handle
+
diff --git a/doc/notes.txt b/doc/notes.txt
new file mode 100644
index 0000000..ab50d3a
--- /dev/null
+++ b/doc/notes.txt
@@ -0,0 +1,8 @@
+TODO: verify replace_with() with empty list is the same as $job->completed
+
+
+policy is all on worker side
+
+client really cheap
+
+priority: low number is higher priority. 0 is highest.
diff --git a/doc/schema.sql b/doc/schema.sql
new file mode 100644
index 0000000..1babbcc
--- /dev/null
+++ b/doc/schema.sql
@@ -0,0 +1,47 @@
+CREATE TABLE funcmap (
+ funcid INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
+ funcname VARCHAR(255) NOT NULL,
+ UNIQUE(funcname)
+);
+
+CREATE TABLE job (
+ jobid BIGINT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
+ funcid INT UNSIGNED NOT NULL,
+ arg MEDIUMBLOB,
+ uniqkey VARCHAR(255) NULL,
+ insert_time INTEGER UNSIGNED,
+ run_after INTEGER UNSIGNED NOT NULL,
+ grabbed_until INTEGER UNSIGNED NOT NULL,
+ priority SMALLINT UNSIGNED,
+ coalesce VARCHAR(255),
+ INDEX (funcid, run_after),
+ UNIQUE(funcid, uniqkey),
+ INDEX (funcid, coalesce)
+);
+
+CREATE TABLE note (
+ jobid BIGINT UNSIGNED NOT NULL,
+ notekey VARCHAR(255),
+ PRIMARY KEY (jobid, notekey),
+ value MEDIUMBLOB
+);
+
+CREATE TABLE error (
+ error_time INTEGER UNSIGNED NOT NULL,
+ jobid BIGINT UNSIGNED NOT NULL,
+ message VARCHAR(255) NOT NULL,
+ funcid INT UNSIGNED NOT NULL DEFAULT 0,
+ INDEX (funcid, error_time),
+ INDEX (error_time),
+ INDEX (jobid)
+);
+
+CREATE TABLE exitstatus (
+ jobid BIGINT UNSIGNED PRIMARY KEY NOT NULL,
+ funcid INT UNSIGNED NOT NULL DEFAULT 0,
+ status SMALLINT UNSIGNED,
+ completion_time INTEGER UNSIGNED,
+ delete_after INTEGER UNSIGNED,
+ INDEX (funcid),
+ INDEX (delete_after)
+);
diff --git a/extras/TheSchwartz.spec b/extras/TheSchwartz.spec
new file mode 100644
index 0000000..d4319b4
--- /dev/null
+++ b/extras/TheSchwartz.spec
@@ -0,0 +1,34 @@
+Name: perl-TheSchwartz
+Version: 0.01
+Release: 1
+Summary: Reliable distributed job system
+License: perl
+Group: Applications/Internet
+BuildRoot: %{_tmppath}/%name-%version-root
+AutoReqProv: no
+Packager: <cpan at sixapart.com>
+
+%description
+
+%prep
+
+%build
+rm -rf trunk
+svn export http://code.sixapart.com/svn/TheSchwartz/trunk
+cd trunk
+%{__perl} Makefile.PL PREFIX=%{buildroot}%{_prefix}
+make
+
+%install
+rm -rf %{buildroot}
+cd trunk
+make install
+rm -rf %{buildroot}/%{_prefix}/lib64
+
+%clean
+rm -rf %{buildroot}
+
+%files
+%{_bindir}/*
+%{_prefix}/lib/*
+#%{_mandir}/*
diff --git a/extras/check_schwartz b/extras/check_schwartz
new file mode 100755
index 0000000..e256edf
--- /dev/null
+++ b/extras/check_schwartz
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -w
+use strict;
+
+# $Id$
+
+## Nagios plugin to check the queue depth of a Schwartz database.
+
+use utils qw( %ERRORS $TIMEOUT );
+use Getopt::Long qw( :config no_ignore_case );
+use DBI;
+
+use constant QUEUE_CRITICAL => 100;
+use constant QUEUE_WARNING => 30;
+
+GetOptions(
+ 'h|help!' => \my($help),
+ 'v|verbose' => \my($verbose),
+ 'dsn=s' => \my($dsn),
+ 'user=s' => \my($user),
+ 'password=s' => \my($pass),
+);
+
+if ($help) {
+ print "$0 --dsn <database-dsn> --user <user> --password <password>";
+ exit $ERRORS{OK};
+}
+
+unless ($dsn && $user) {
+ print <<USAGE;
+You have to supply a database DSN and username.
+
+Example:
+
+ $0 --dsn dbi:mysql:host=schwartz-db1;dbname=theschwartz_comet --user comet
+USAGE
+ exit $ERRORS{UNKNOWN};
+}
+
+sub exit_with {
+ my($code, $msg) = @_;
+ $msg = $msg ? ' - ' . $msg : '';
+ print "TheSchwartz $dsn $code$msg";
+ exit $ERRORS{$code};
+}
+
+my $dbh = DBI->connect($dsn, $user, $pass)
+ or exit_with 'CRITICAL', "Can't connect to $dsn: $DBI::errstr";
+
+my $inf = $dbh->selectrow_arrayref(<<SQL, undef, time);
+SELECT COUNT(*)
+FROM job
+WHERE run_after <= ?
+SQL
+unless ($inf && defined $inf->[0]) {
+ exit_with 'CRITICAL', "Failed getting job count: " . $dbh->errstr;
+}
+
+if ($inf->[0] < QUEUE_WARNING) {
+ exit_with 'OK';
+} elsif ($inf->[0] < QUEUE_CRITICAL) {
+ exit_with 'WARNING', "Schwartz queue depth is $inf->[0]";
+} else {
+ exit_with 'CRITICAL', "Schwartz queue depth is $inf->[0]";
+}
diff --git a/inc/Module/AutoInstall.pm b/inc/Module/AutoInstall.pm
new file mode 100644
index 0000000..7efc552
--- /dev/null
+++ b/inc/Module/AutoInstall.pm
@@ -0,0 +1,768 @@
+#line 1
+package Module::AutoInstall;
+
+use strict;
+use Cwd ();
+use ExtUtils::MakeMaker ();
+
+use vars qw{$VERSION};
+BEGIN {
+ $VERSION = '1.03';
+}
+
+# special map on pre-defined feature sets
+my %FeatureMap = (
+ '' => 'Core Features', # XXX: deprecated
+ '-core' => 'Core Features',
+);
+
+# various lexical flags
+my ( @Missing, @Existing, %DisabledTests, $UnderCPAN, $HasCPANPLUS );
+my ( $Config, $CheckOnly, $SkipInstall, $AcceptDefault, $TestOnly );
+my ( $PostambleActions, $PostambleUsed );
+
+# See if it's a testing or non-interactive session
+_accept_default( $ENV{AUTOMATED_TESTING} or ! -t STDIN );
+_init();
+
+sub _accept_default {
+ $AcceptDefault = shift;
+}
+
+sub missing_modules {
+ return @Missing;
+}
+
+sub do_install {
+ __PACKAGE__->install(
+ [
+ $Config
+ ? ( UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
+ : ()
+ ],
+ @Missing,
+ );
+}
+
+# initialize various flags, and/or perform install
+sub _init {
+ foreach my $arg (
+ @ARGV,
+ split(
+ /[\s\t]+/,
+ $ENV{PERL_AUTOINSTALL} || $ENV{PERL_EXTUTILS_AUTOINSTALL} || ''
+ )
+ )
+ {
+ if ( $arg =~ /^--config=(.*)$/ ) {
+ $Config = [ split( ',', $1 ) ];
+ }
+ elsif ( $arg =~ /^--installdeps=(.*)$/ ) {
+ __PACKAGE__->install( $Config, @Missing = split( /,/, $1 ) );
+ exit 0;
+ }
+ elsif ( $arg =~ /^--default(?:deps)?$/ ) {
+ $AcceptDefault = 1;
+ }
+ elsif ( $arg =~ /^--check(?:deps)?$/ ) {
+ $CheckOnly = 1;
+ }
+ elsif ( $arg =~ /^--skip(?:deps)?$/ ) {
+ $SkipInstall = 1;
+ }
+ elsif ( $arg =~ /^--test(?:only)?$/ ) {
+ $TestOnly = 1;
+ }
+ }
+}
+
+# overrides MakeMaker's prompt() to automatically accept the default choice
+sub _prompt {
+ goto &ExtUtils::MakeMaker::prompt unless $AcceptDefault;
+
+ my ( $prompt, $default ) = @_;
+ my $y = ( $default =~ /^[Yy]/ );
+
+ print $prompt, ' [', ( $y ? 'Y' : 'y' ), '/', ( $y ? 'n' : 'N' ), '] ';
+ print "$default\n";
+ return $default;
+}
+
+# the workhorse
+sub import {
+ my $class = shift;
+ my @args = @_ or return;
+ my $core_all;
+
+ print "*** $class version " . $class->VERSION . "\n";
+ print "*** Checking for Perl dependencies...\n";
+
+ my $cwd = Cwd::cwd();
+
+ $Config = [];
+
+ my $maxlen = length(
+ (
+ sort { length($b) <=> length($a) }
+ grep { /^[^\-]/ }
+ map {
+ ref($_)
+ ? ( ( ref($_) eq 'HASH' ) ? keys(%$_) : @{$_} )
+ : ''
+ }
+ map { +{@args}->{$_} }
+ grep { /^[^\-]/ or /^-core$/i } keys %{ +{@args} }
+ )[0]
+ );
+
+ while ( my ( $feature, $modules ) = splice( @args, 0, 2 ) ) {
+ my ( @required, @tests, @skiptests );
+ my $default = 1;
+ my $conflict = 0;
+
+ if ( $feature =~ m/^-(\w+)$/ ) {
+ my $option = lc($1);
+
+ # check for a newer version of myself
+ _update_to( $modules, @_ ) and return if $option eq 'version';
+
+ # sets CPAN configuration options
+ $Config = $modules if $option eq 'config';
+
+ # promote every features to core status
+ $core_all = ( $modules =~ /^all$/i ) and next
+ if $option eq 'core';
+
+ next unless $option eq 'core';
+ }
+
+ print "[" . ( $FeatureMap{ lc($feature) } || $feature ) . "]\n";
+
+ $modules = [ %{$modules} ] if UNIVERSAL::isa( $modules, 'HASH' );
+
+ unshift @$modules, -default => &{ shift(@$modules) }
+ if ( ref( $modules->[0] ) eq 'CODE' ); # XXX: bugward combatability
+
+ while ( my ( $mod, $arg ) = splice( @$modules, 0, 2 ) ) {
+ if ( $mod =~ m/^-(\w+)$/ ) {
+ my $option = lc($1);
+
+ $default = $arg if ( $option eq 'default' );
+ $conflict = $arg if ( $option eq 'conflict' );
+ @tests = @{$arg} if ( $option eq 'tests' );
+ @skiptests = @{$arg} if ( $option eq 'skiptests' );
+
+ next;
+ }
+
+ printf( "- %-${maxlen}s ...", $mod );
+
+ if ( $arg and $arg =~ /^\D/ ) {
+ unshift @$modules, $arg;
+ $arg = 0;
+ }
+
+ # XXX: check for conflicts and uninstalls(!) them.
+ if (
+ defined( my $cur = _version_check( _load($mod), $arg ||= 0 ) ) )
+ {
+ print "loaded. ($cur" . ( $arg ? " >= $arg" : '' ) . ")\n";
+ push @Existing, $mod => $arg;
+ $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
+ }
+ else {
+ print "missing." . ( $arg ? " (would need $arg)" : '' ) . "\n";
+ push @required, $mod => $arg;
+ }
+ }
+
+ next unless @required;
+
+ my $mandatory = ( $feature eq '-core' or $core_all );
+
+ if (
+ !$SkipInstall
+ and (
+ $CheckOnly
+ or _prompt(
+ qq{==> Auto-install the }
+ . ( @required / 2 )
+ . ( $mandatory ? ' mandatory' : ' optional' )
+ . qq{ module(s) from CPAN?},
+ $default ? 'y' : 'n',
+ ) =~ /^[Yy]/
+ )
+ )
+ {
+ push( @Missing, @required );
+ $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
+ }
+
+ elsif ( !$SkipInstall
+ and $default
+ and $mandatory
+ and
+ _prompt( qq{==> The module(s) are mandatory! Really skip?}, 'n', )
+ =~ /^[Nn]/ )
+ {
+ push( @Missing, @required );
+ $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
+ }
+
+ else {
+ $DisabledTests{$_} = 1 for map { glob($_) } @tests;
+ }
+ }
+
+ $UnderCPAN = _check_lock(); # check for $UnderCPAN
+
+ if ( @Missing and not( $CheckOnly or $UnderCPAN ) ) {
+ require Config;
+ print
+"*** Dependencies will be installed the next time you type '$Config::Config{make}'.\n";
+
+ # make an educated guess of whether we'll need root permission.
+ print " (You may need to do that as the 'root' user.)\n"
+ if eval '$>';
+ }
+ print "*** $class configuration finished.\n";
+
+ chdir $cwd;
+
+ # import to main::
+ no strict 'refs';
+ *{'main::WriteMakefile'} = \&Write if caller(0) eq 'main';
+}
+
+# Check to see if we are currently running under CPAN.pm and/or CPANPLUS;
+# if we are, then we simply let it taking care of our dependencies
+sub _check_lock {
+ return unless @Missing;
+
+ if ($ENV{PERL5_CPANPLUS_IS_RUNNING}) {
+ print <<'END_MESSAGE';
+
+*** Since we're running under CPANPLUS, I'll just let it take care
+ of the dependency's installation later.
+END_MESSAGE
+ return 1;
+ }
+
+ _load_cpan();
+
+ # Find the CPAN lock-file
+ my $lock = MM->catfile( $CPAN::Config->{cpan_home}, ".lock" );
+ return unless -f $lock;
+
+ # Check the lock
+ local *LOCK;
+ return unless open(LOCK, $lock);
+
+ if (
+ ( $^O eq 'MSWin32' ? _under_cpan() : <LOCK> == getppid() )
+ and ( $CPAN::Config->{prerequisites_policy} || '' ) ne 'ignore'
+ ) {
+ print <<'END_MESSAGE';
+
+*** Since we're running under CPAN, I'll just let it take care
+ of the dependency's installation later.
+END_MESSAGE
+ return 1;
+ }
+
+ close LOCK;
+ return;
+}
+
+sub install {
+ my $class = shift;
+
+ my $i; # used below to strip leading '-' from config keys
+ my @config = ( map { s/^-// if ++$i; $_ } @{ +shift } );
+
+ my ( @modules, @installed );
+ while ( my ( $pkg, $ver ) = splice( @_, 0, 2 ) ) {
+
+ # grep out those already installed
+ if ( defined( _version_check( _load($pkg), $ver ) ) ) {
+ push @installed, $pkg;
+ }
+ else {
+ push @modules, $pkg, $ver;
+ }
+ }
+
+ return @installed unless @modules; # nothing to do
+ return @installed if _check_lock(); # defer to the CPAN shell
+
+ print "*** Installing dependencies...\n";
+
+ return unless _connected_to('cpan.org');
+
+ my %args = @config;
+ my %failed;
+ local *FAILED;
+ if ( $args{do_once} and open( FAILED, '.#autoinstall.failed' ) ) {
+ while (<FAILED>) { chomp; $failed{$_}++ }
+ close FAILED;
+
+ my @newmod;
+ while ( my ( $k, $v ) = splice( @modules, 0, 2 ) ) {
+ push @newmod, ( $k => $v ) unless $failed{$k};
+ }
+ @modules = @newmod;
+ }
+
+ if ( _has_cpanplus() ) {
+ _install_cpanplus( \@modules, \@config );
+ } else {
+ _install_cpan( \@modules, \@config );
+ }
+
+ print "*** $class installation finished.\n";
+
+ # see if we have successfully installed them
+ while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
+ if ( defined( _version_check( _load($pkg), $ver ) ) ) {
+ push @installed, $pkg;
+ }
+ elsif ( $args{do_once} and open( FAILED, '>> .#autoinstall.failed' ) ) {
+ print FAILED "$pkg\n";
+ }
+ }
+
+ close FAILED if $args{do_once};
+
+ return @installed;
+}
+
+sub _install_cpanplus {
+ my @modules = @{ +shift };
+ my @config = _cpanplus_config( @{ +shift } );
+ my $installed = 0;
+
+ require CPANPLUS::Backend;
+ my $cp = CPANPLUS::Backend->new;
+ my $conf = $cp->configure_object;
+
+ return unless $conf->can('conf') # 0.05x+ with "sudo" support
+ or _can_write($conf->_get_build('base')); # 0.04x
+
+ # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
+ my $makeflags = $conf->get_conf('makeflags') || '';
+ if ( UNIVERSAL::isa( $makeflags, 'HASH' ) ) {
+ # 0.03+ uses a hashref here
+ $makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST};
+
+ } else {
+ # 0.02 and below uses a scalar
+ $makeflags = join( ' ', split( ' ', $makeflags ), 'UNINST=1' )
+ if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } );
+
+ }
+ $conf->set_conf( makeflags => $makeflags );
+ $conf->set_conf( prereqs => 1 );
+
+
+
+ while ( my ( $key, $val ) = splice( @config, 0, 2 ) ) {
+ $conf->set_conf( $key, $val );
+ }
+
+ my $modtree = $cp->module_tree;
+ while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
+ print "*** Installing $pkg...\n";
+
+ MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall;
+
+ my $success;
+ my $obj = $modtree->{$pkg};
+
+ if ( $obj and defined( _version_check( $obj->{version}, $ver ) ) ) {
+ my $pathname = $pkg;
+ $pathname =~ s/::/\\W/;
+
+ foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) {
+ delete $INC{$inc};
+ }
+
+ my $rv = $cp->install( modules => [ $obj->{module} ] );
+
+ if ( $rv and ( $rv->{ $obj->{module} } or $rv->{ok} ) ) {
+ print "*** $pkg successfully installed.\n";
+ $success = 1;
+ } else {
+ print "*** $pkg installation cancelled.\n";
+ $success = 0;
+ }
+
+ $installed += $success;
+ } else {
+ print << ".";
+*** Could not find a version $ver or above for $pkg; skipping.
+.
+ }
+
+ MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall;
+ }
+
+ return $installed;
+}
+
+sub _cpanplus_config {
+ my @config = ();
+ while ( @_ ) {
+ my ($key, $value) = (shift(), shift());
+ if ( $key eq 'prerequisites_policy' ) {
+ if ( $value eq 'follow' ) {
+ $value = CPANPLUS::Internals::Constants::PREREQ_INSTALL();
+ } elsif ( $value eq 'ask' ) {
+ $value = CPANPLUS::Internals::Constants::PREREQ_ASK();
+ } elsif ( $value eq 'ignore' ) {
+ $value = CPANPLUS::Internals::Constants::PREREQ_IGNORE();
+ } else {
+ die "*** Cannot convert option $key = '$value' to CPANPLUS version.\n";
+ }
+ } else {
+ die "*** Cannot convert option $key to CPANPLUS version.\n";
+ }
+ }
+ return @config;
+}
+
+sub _install_cpan {
+ my @modules = @{ +shift };
+ my @config = @{ +shift };
+ my $installed = 0;
+ my %args;
+
+ _load_cpan();
+ require Config;
+
+ if (CPAN->VERSION < 1.80) {
+ # no "sudo" support, probe for writableness
+ return unless _can_write( MM->catfile( $CPAN::Config->{cpan_home}, 'sources' ) )
+ and _can_write( $Config::Config{sitelib} );
+ }
+
+ # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
+ my $makeflags = $CPAN::Config->{make_install_arg} || '';
+ $CPAN::Config->{make_install_arg} =
+ join( ' ', split( ' ', $makeflags ), 'UNINST=1' )
+ if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } );
+
+ # don't show start-up info
+ $CPAN::Config->{inhibit_startup_message} = 1;
+
+ # set additional options
+ while ( my ( $opt, $arg ) = splice( @config, 0, 2 ) ) {
+ ( $args{$opt} = $arg, next )
+ if $opt =~ /^force$/; # pseudo-option
+ $CPAN::Config->{$opt} = $arg;
+ }
+
+ local $CPAN::Config->{prerequisites_policy} = 'follow';
+
+ while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
+ MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall;
+
+ print "*** Installing $pkg...\n";
+
+ my $obj = CPAN::Shell->expand( Module => $pkg );
+ my $success = 0;
+
+ if ( $obj and defined( _version_check( $obj->cpan_version, $ver ) ) ) {
+ my $pathname = $pkg;
+ $pathname =~ s/::/\\W/;
+
+ foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) {
+ delete $INC{$inc};
+ }
+
+ my $rv = $args{force} ? CPAN::Shell->force( install => $pkg )
+ : CPAN::Shell->install($pkg);
+ $rv ||= eval {
+ $CPAN::META->instance( 'CPAN::Distribution', $obj->cpan_file, )
+ ->{install}
+ if $CPAN::META;
+ };
+
+ if ( $rv eq 'YES' ) {
+ print "*** $pkg successfully installed.\n";
+ $success = 1;
+ }
+ else {
+ print "*** $pkg installation failed.\n";
+ $success = 0;
+ }
+
+ $installed += $success;
+ }
+ else {
+ print << ".";
+*** Could not find a version $ver or above for $pkg; skipping.
+.
+ }
+
+ MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall;
+ }
+
+ return $installed;
+}
+
+sub _has_cpanplus {
+ return (
+ $HasCPANPLUS = (
+ $INC{'CPANPLUS/Config.pm'}
+ or _load('CPANPLUS::Shell::Default')
+ )
+ );
+}
+
+# make guesses on whether we're under the CPAN installation directory
+sub _under_cpan {
+ require Cwd;
+ require File::Spec;
+
+ my $cwd = File::Spec->canonpath( Cwd::cwd() );
+ my $cpan = File::Spec->canonpath( $CPAN::Config->{cpan_home} );
+
+ return ( index( $cwd, $cpan ) > -1 );
+}
+
+sub _update_to {
+ my $class = __PACKAGE__;
+ my $ver = shift;
+
+ return
+ if defined( _version_check( _load($class), $ver ) ); # no need to upgrade
+
+ if (
+ _prompt( "==> A newer version of $class ($ver) is required. Install?",
+ 'y' ) =~ /^[Nn]/
+ )
+ {
+ die "*** Please install $class $ver manually.\n";
+ }
+
+ print << ".";
+*** Trying to fetch it from CPAN...
+.
+
+ # install ourselves
+ _load($class) and return $class->import(@_)
+ if $class->install( [], $class, $ver );
+
+ print << '.'; exit 1;
+
+*** Cannot bootstrap myself. :-( Installation terminated.
+.
+}
+
+# check if we're connected to some host, using inet_aton
+sub _connected_to {
+ my $site = shift;
+
+ return (
+ ( _load('Socket') and Socket::inet_aton($site) ) or _prompt(
+ qq(
+*** Your host cannot resolve the domain name '$site', which
+ probably means the Internet connections are unavailable.
+==> Should we try to install the required module(s) anyway?), 'n'
+ ) =~ /^[Yy]/
+ );
+}
+
+# check if a directory is writable; may create it on demand
+sub _can_write {
+ my $path = shift;
+ mkdir( $path, 0755 ) unless -e $path;
+
+ return 1 if -w $path;
+
+ print << ".";
+*** You are not allowed to write to the directory '$path';
+ the installation may fail due to insufficient permissions.
+.
+
+ if (
+ eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt(
+ qq(
+==> Should we try to re-execute the autoinstall process with 'sudo'?),
+ ((-t STDIN) ? 'y' : 'n')
+ ) =~ /^[Yy]/
+ )
+ {
+
+ # try to bootstrap ourselves from sudo
+ print << ".";
+*** Trying to re-execute the autoinstall process with 'sudo'...
+.
+ my $missing = join( ',', @Missing );
+ my $config = join( ',',
+ UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
+ if $Config;
+
+ return
+ unless system( 'sudo', $^X, $0, "--config=$config",
+ "--installdeps=$missing" );
+
+ print << ".";
+*** The 'sudo' command exited with error! Resuming...
+.
+ }
+
+ return _prompt(
+ qq(
+==> Should we try to install the required module(s) anyway?), 'n'
+ ) =~ /^[Yy]/;
+}
+
+# load a module and return the version it reports
+sub _load {
+ my $mod = pop; # class/instance doesn't matter
+ my $file = $mod;
+
+ $file =~ s|::|/|g;
+ $file .= '.pm';
+
+ local $@;
+ return eval { require $file; $mod->VERSION } || ( $@ ? undef: 0 );
+}
+
+# Load CPAN.pm and it's configuration
+sub _load_cpan {
+ return if $CPAN::VERSION;
+ require CPAN;
+ if ( $CPAN::HandleConfig::VERSION ) {
+ # Newer versions of CPAN have a HandleConfig module
+ CPAN::HandleConfig->load;
+ } else {
+ # Older versions had the load method in Config directly
+ CPAN::Config->load;
+ }
+}
+
+# compare two versions, either use Sort::Versions or plain comparison
+sub _version_check {
+ my ( $cur, $min ) = @_;
+ return unless defined $cur;
+
+ $cur =~ s/\s+$//;
+
+ # check for version numbers that are not in decimal format
+ if ( ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./ ) {
+ if ( ( $version::VERSION or defined( _load('version') )) and
+ version->can('new')
+ ) {
+
+ # use version.pm if it is installed.
+ return (
+ ( version->new($cur) >= version->new($min) ) ? $cur : undef );
+ }
+ elsif ( $Sort::Versions::VERSION or defined( _load('Sort::Versions') ) )
+ {
+
+ # use Sort::Versions as the sorting algorithm for a.b.c versions
+ return ( ( Sort::Versions::versioncmp( $cur, $min ) != -1 )
+ ? $cur
+ : undef );
+ }
+
+ warn "Cannot reliably compare non-decimal formatted versions.\n"
+ . "Please install version.pm or Sort::Versions.\n";
+ }
+
+ # plain comparison
+ local $^W = 0; # shuts off 'not numeric' bugs
+ return ( $cur >= $min ? $cur : undef );
+}
+
+# nothing; this usage is deprecated.
+sub main::PREREQ_PM { return {}; }
+
+sub _make_args {
+ my %args = @_;
+
+ $args{PREREQ_PM} = { %{ $args{PREREQ_PM} || {} }, @Existing, @Missing }
+ if $UnderCPAN or $TestOnly;
+
+ if ( $args{EXE_FILES} and -e 'MANIFEST' ) {
+ require ExtUtils::Manifest;
+ my $manifest = ExtUtils::Manifest::maniread('MANIFEST');
+
+ $args{EXE_FILES} =
+ [ grep { exists $manifest->{$_} } @{ $args{EXE_FILES} } ];
+ }
+
+ $args{test}{TESTS} ||= 't/*.t';
+ $args{test}{TESTS} = join( ' ',
+ grep { !exists( $DisabledTests{$_} ) }
+ map { glob($_) } split( /\s+/, $args{test}{TESTS} ) );
+
+ my $missing = join( ',', @Missing );
+ my $config =
+ join( ',', UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
+ if $Config;
+
+ $PostambleActions = (
+ $missing
+ ? "\$(PERL) $0 --config=$config --installdeps=$missing"
+ : "\$(NOECHO) \$(NOOP)"
+ );
+
+ return %args;
+}
+
+# a wrapper to ExtUtils::MakeMaker::WriteMakefile
+sub Write {
+ require Carp;
+ Carp::croak "WriteMakefile: Need even number of args" if @_ % 2;
+
+ if ($CheckOnly) {
+ print << ".";
+*** Makefile not written in check-only mode.
+.
+ return;
+ }
+
+ my %args = _make_args(@_);
+
+ no strict 'refs';
+
+ $PostambleUsed = 0;
+ local *MY::postamble = \&postamble unless defined &MY::postamble;
+ ExtUtils::MakeMaker::WriteMakefile(%args);
+
+ print << "." unless $PostambleUsed;
+*** WARNING: Makefile written with customized MY::postamble() without
+ including contents from Module::AutoInstall::postamble() --
+ auto installation features disabled. Please contact the author.
+.
+
+ return 1;
+}
+
+sub postamble {
+ $PostambleUsed = 1;
+
+ return << ".";
+
+config :: installdeps
+\t\$(NOECHO) \$(NOOP)
+
+checkdeps ::
+\t\$(PERL) $0 --checkdeps
+
+installdeps ::
+\t$PostambleActions
+
+.
+
+}
+
+1;
+
+__END__
+
+#line 1003
diff --git a/inc/Module/Install.pm b/inc/Module/Install.pm
new file mode 100644
index 0000000..af6a59c
--- /dev/null
+++ b/inc/Module/Install.pm
@@ -0,0 +1,281 @@
+#line 1
+package Module::Install;
+
+# For any maintainers:
+# The load order for Module::Install is a bit magic.
+# It goes something like this...
+#
+# IF ( host has Module::Install installed, creating author mode ) {
+# 1. Makefile.PL calls "use inc::Module::Install"
+# 2. $INC{inc/Module/Install.pm} set to installed version of inc::Module::Install
+# 3. The installed version of inc::Module::Install loads
+# 4. inc::Module::Install calls "require Module::Install"
+# 5. The ./inc/ version of Module::Install loads
+# } ELSE {
+# 1. Makefile.PL calls "use inc::Module::Install"
+# 2. $INC{inc/Module/Install.pm} set to ./inc/ version of Module::Install
+# 3. The ./inc/ version of Module::Install loads
+# }
+
+use 5.004;
+use strict 'vars';
+
+use vars qw{$VERSION};
+BEGIN {
+ # All Module::Install core packages now require synchronised versions.
+ # This will be used to ensure we don't accidentally load old or
+ # different versions of modules.
+ # This is not enforced yet, but will be some time in the next few
+ # releases once we can make sure it won't clash with custom
+ # Module::Install extensions.
+ $VERSION = '0.65';
+}
+
+# Whether or not inc::Module::Install is actually loaded, the
+# $INC{inc/Module/Install.pm} is what will still get set as long as
+# the caller loaded module this in the documented manner.
+# If not set, the caller may NOT have loaded the bundled version, and thus
+# they may not have a MI version that works with the Makefile.PL. This would
+# result in false errors or unexpected behaviour. And we don't want that.
+my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
+unless ( $INC{$file} ) {
+ die <<"END_DIE";
+Please invoke ${\__PACKAGE__} with:
+
+ use inc::${\__PACKAGE__};
+
+not:
+
+ use ${\__PACKAGE__};
+
+END_DIE
+}
+
+# If the script that is loading Module::Install is from the future,
+# then make will detect this and cause it to re-run over and over
+# again. This is bad. Rather than taking action to touch it (which
+# is unreliable on some platforms and requires write permissions)
+# for now we should catch this and refuse to run.
+if ( -f $0 and (stat($0))[9] > time ) {
+ die << "END_DIE";
+Your installer $0 has a modification time in the future.
+
+This is known to create infinite loops in make.
+
+Please correct this, then run $0 again.
+
+END_DIE
+}
+
+use Cwd ();
+use File::Find ();
+use File::Path ();
+use FindBin;
+
+*inc::Module::Install::VERSION = *VERSION;
+ at inc::Module::Install::ISA = __PACKAGE__;
+
+sub autoload {
+ my $self = shift;
+ my $who = $self->_caller;
+ my $cwd = Cwd::cwd();
+ my $sym = "${who}::AUTOLOAD";
+ $sym->{$cwd} = sub {
+ my $pwd = Cwd::cwd();
+ if ( my $code = $sym->{$pwd} ) {
+ # delegate back to parent dirs
+ goto &$code unless $cwd eq $pwd;
+ }
+ $$sym =~ /([^:]+)$/ or die "Cannot autoload $who - $sym";
+ unshift @_, ($self, $1);
+ goto &{$self->can('call')} unless uc($1) eq $1;
+ };
+}
+
+sub import {
+ my $class = shift;
+ my $self = $class->new(@_);
+ my $who = $self->_caller;
+
+ unless ( -f $self->{file} ) {
+ require "$self->{path}/$self->{dispatch}.pm";
+ File::Path::mkpath("$self->{prefix}/$self->{author}");
+ $self->{admin} = "$self->{name}::$self->{dispatch}"->new( _top => $self );
+ $self->{admin}->init;
+ @_ = ($class, _self => $self);
+ goto &{"$self->{name}::import"};
+ }
+
+ *{"${who}::AUTOLOAD"} = $self->autoload;
+ $self->preload;
+
+ # Unregister loader and worker packages so subdirs can use them again
+ delete $INC{"$self->{file}"};
+ delete $INC{"$self->{path}.pm"};
+}
+
+sub preload {
+ my ($self) = @_;
+
+ unless ( $self->{extensions} ) {
+ $self->load_extensions(
+ "$self->{prefix}/$self->{path}", $self
+ );
+ }
+
+ my @exts = @{$self->{extensions}};
+ unless ( @exts ) {
+ my $admin = $self->{admin};
+ @exts = $admin->load_all_extensions;
+ }
+
+ my %seen;
+ foreach my $obj ( @exts ) {
+ while (my ($method, $glob) = each %{ref($obj) . '::'}) {
+ next unless $obj->can($method);
+ next if $method =~ /^_/;
+ next if $method eq uc($method);
+ $seen{$method}++;
+ }
+ }
+
+ my $who = $self->_caller;
+ foreach my $name ( sort keys %seen ) {
+ *{"${who}::$name"} = sub {
+ ${"${who}::AUTOLOAD"} = "${who}::$name";
+ goto &{"${who}::AUTOLOAD"};
+ };
+ }
+}
+
+sub new {
+ my ($class, %args) = @_;
+
+ # ignore the prefix on extension modules built from top level.
+ my $base_path = Cwd::abs_path($FindBin::Bin);
+ unless ( Cwd::abs_path(Cwd::cwd()) eq $base_path ) {
+ delete $args{prefix};
+ }
+
+ return $args{_self} if $args{_self};
+
+ $args{dispatch} ||= 'Admin';
+ $args{prefix} ||= 'inc';
+ $args{author} ||= ($^O eq 'VMS' ? '_author' : '.author');
+ $args{bundle} ||= 'inc/BUNDLES';
+ $args{base} ||= $base_path;
+ $class =~ s/^\Q$args{prefix}\E:://;
+ $args{name} ||= $class;
+ $args{version} ||= $class->VERSION;
+ unless ( $args{path} ) {
+ $args{path} = $args{name};
+ $args{path} =~ s!::!/!g;
+ }
+ $args{file} ||= "$args{base}/$args{prefix}/$args{path}.pm";
+
+ bless( \%args, $class );
+}
+
+sub call {
+ my ($self, $method) = @_;
+ my $obj = $self->load($method) or return;
+ splice(@_, 0, 2, $obj);
+ goto &{$obj->can($method)};
+}
+
+sub load {
+ my ($self, $method) = @_;
+
+ $self->load_extensions(
+ "$self->{prefix}/$self->{path}", $self
+ ) unless $self->{extensions};
+
+ foreach my $obj (@{$self->{extensions}}) {
+ return $obj if $obj->can($method);
+ }
+
+ my $admin = $self->{admin} or die <<"END_DIE";
+The '$method' method does not exist in the '$self->{prefix}' path!
+Please remove the '$self->{prefix}' directory and run $0 again to load it.
+END_DIE
+
+ my $obj = $admin->load($method, 1);
+ push @{$self->{extensions}}, $obj;
+
+ $obj;
+}
+
+sub load_extensions {
+ my ($self, $path, $top) = @_;
+
+ unless ( grep { lc $_ eq lc $self->{prefix} } @INC ) {
+ unshift @INC, $self->{prefix};
+ }
+
+ foreach my $rv ( $self->find_extensions($path) ) {
+ my ($file, $pkg) = @{$rv};
+ next if $self->{pathnames}{$pkg};
+
+ local $@;
+ my $new = eval { require $file; $pkg->can('new') };
+ unless ( $new ) {
+ warn $@ if $@;
+ next;
+ }
+ $self->{pathnames}{$pkg} = delete $INC{$file};
+ push @{$self->{extensions}}, &{$new}($pkg, _top => $top );
+ }
+
+ $self->{extensions} ||= [];
+}
+
+sub find_extensions {
+ my ($self, $path) = @_;
+
+ my @found;
+ File::Find::find( sub {
+ my $file = $File::Find::name;
+ return unless $file =~ m!^\Q$path\E/(.+)\.pm\Z!is;
+ my $subpath = $1;
+ return if lc($subpath) eq lc($self->{dispatch});
+
+ $file = "$self->{path}/$subpath.pm";
+ my $pkg = "$self->{name}::$subpath";
+ $pkg =~ s!/!::!g;
+
+ # If we have a mixed-case package name, assume case has been preserved
+ # correctly. Otherwise, root through the file to locate the case-preserved
+ # version of the package name.
+ if ( $subpath eq lc($subpath) || $subpath eq uc($subpath) ) {
+ open PKGFILE, "<$subpath.pm" or die "find_extensions: Can't open $subpath.pm: $!";
+ my $in_pod = 0;
+ while ( <PKGFILE> ) {
+ $in_pod = 1 if /^=\w/;
+ $in_pod = 0 if /^=cut/;
+ next if ($in_pod || /^=cut/); # skip pod text
+ next if /^\s*#/; # and comments
+ if ( m/^\s*package\s+($pkg)\s*;/i ) {
+ $pkg = $1;
+ last;
+ }
+ }
+ close PKGFILE;
+ }
+
+ push @found, [ $file, $pkg ];
+ }, $path ) if -d $path;
+
+ @found;
+}
+
+sub _caller {
+ my $depth = 0;
+ my $call = caller($depth);
+ while ( $call eq __PACKAGE__ ) {
+ $depth++;
+ $call = caller($depth);
+ }
+ return $call;
+}
+
+1;
diff --git a/inc/Module/Install/AutoInstall.pm b/inc/Module/Install/AutoInstall.pm
new file mode 100644
index 0000000..b4b55af
--- /dev/null
+++ b/inc/Module/Install/AutoInstall.pm
@@ -0,0 +1,61 @@
+#line 1
+package Module::Install::AutoInstall;
+
+use strict;
+use Module::Install::Base;
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+sub AutoInstall { $_[0] }
+
+sub run {
+ my $self = shift;
+ $self->auto_install_now(@_);
+}
+
+sub write {
+ my $self = shift;
+ $self->auto_install(@_);
+}
+
+sub auto_install {
+ my $self = shift;
+ return if $self->{done}++;
+
+ # Flatten array of arrays into a single array
+ my @core = map @$_, map @$_, grep ref,
+ $self->build_requires, $self->requires;
+
+ my @config = @_;
+
+ # We'll need Module::AutoInstall
+ $self->include('Module::AutoInstall');
+ require Module::AutoInstall;
+
+ Module::AutoInstall->import(
+ (@config ? (-config => \@config) : ()),
+ (@core ? (-core => \@core) : ()),
+ $self->features,
+ );
+
+ $self->makemaker_args( Module::AutoInstall::_make_args() );
+
+ my $class = ref($self);
+ $self->postamble(
+ "# --- $class section:\n" .
+ Module::AutoInstall::postamble()
+ );
+}
+
+sub auto_install_now {
+ my $self = shift;
+ $self->auto_install(@_);
+ Module::AutoInstall::do_install();
+}
+
+1;
diff --git a/inc/Module/Install/Base.pm b/inc/Module/Install/Base.pm
new file mode 100644
index 0000000..b46a8ca
--- /dev/null
+++ b/inc/Module/Install/Base.pm
@@ -0,0 +1,70 @@
+#line 1
+package Module::Install::Base;
+
+$VERSION = '0.65';
+
+# Suspend handler for "redefined" warnings
+BEGIN {
+ my $w = $SIG{__WARN__};
+ $SIG{__WARN__} = sub { $w };
+}
+
+### This is the ONLY module that shouldn't have strict on
+# use strict;
+
+#line 41
+
+sub new {
+ my ($class, %args) = @_;
+
+ foreach my $method ( qw(call load) ) {
+ *{"$class\::$method"} = sub {
+ shift()->_top->$method(@_);
+ } unless defined &{"$class\::$method"};
+ }
+
+ bless( \%args, $class );
+}
+
+#line 61
+
+sub AUTOLOAD {
+ my $self = shift;
+ local $@;
+ my $autoload = eval { $self->_top->autoload } or return;
+ goto &$autoload;
+}
+
+#line 76
+
+sub _top { $_[0]->{_top} }
+
+#line 89
+
+sub admin {
+ $_[0]->_top->{admin} or Module::Install::Base::FakeAdmin->new;
+}
+
+sub is_admin {
+ $_[0]->admin->VERSION;
+}
+
+sub DESTROY {}
+
+package Module::Install::Base::FakeAdmin;
+
+my $Fake;
+sub new { $Fake ||= bless(\@_, $_[0]) }
+
+sub AUTOLOAD {}
+
+sub DESTROY {}
+
+# Restore warning handler
+BEGIN {
+ $SIG{__WARN__} = $SIG{__WARN__}->();
+}
+
+1;
+
+#line 138
diff --git a/inc/Module/Install/Can.pm b/inc/Module/Install/Can.pm
new file mode 100644
index 0000000..9bcf278
--- /dev/null
+++ b/inc/Module/Install/Can.pm
@@ -0,0 +1,82 @@
+#line 1
+package Module::Install::Can;
+
+use strict;
+use Module::Install::Base;
+use Config ();
+### This adds a 5.005 Perl version dependency.
+### This is a bug and will be fixed.
+use File::Spec ();
+use ExtUtils::MakeMaker ();
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+# check if we can load some module
+### Upgrade this to not have to load the module if possible
+sub can_use {
+ my ($self, $mod, $ver) = @_;
+ $mod =~ s{::|\\}{/}g;
+ $mod .= '.pm' unless $mod =~ /\.pm$/i;
+
+ my $pkg = $mod;
+ $pkg =~ s{/}{::}g;
+ $pkg =~ s{\.pm$}{}i;
+
+ local $@;
+ eval { require $mod; $pkg->VERSION($ver || 0); 1 };
+}
+
+# check if we can run some command
+sub can_run {
+ my ($self, $cmd) = @_;
+
+ my $_cmd = $cmd;
+ return $_cmd if (-x $_cmd or $_cmd = MM->maybe_command($_cmd));
+
+ for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
+ my $abs = File::Spec->catfile($dir, $_[1]);
+ return $abs if (-x $abs or $abs = MM->maybe_command($abs));
+ }
+
+ return;
+}
+
+# can we locate a (the) C compiler
+sub can_cc {
+ my $self = shift;
+ my @chunks = split(/ /, $Config::Config{cc}) or return;
+
+ # $Config{cc} may contain args; try to find out the program part
+ while (@chunks) {
+ return $self->can_run("@chunks") || (pop(@chunks), next);
+ }
+
+ return;
+}
+
+# Fix Cygwin bug on maybe_command();
+if ( $^O eq 'cygwin' ) {
+ require ExtUtils::MM_Cygwin;
+ require ExtUtils::MM_Win32;
+ if ( ! defined(&ExtUtils::MM_Cygwin::maybe_command) ) {
+ *ExtUtils::MM_Cygwin::maybe_command = sub {
+ my ($self, $file) = @_;
+ if ($file =~ m{^/cygdrive/}i and ExtUtils::MM_Win32->can('maybe_command')) {
+ ExtUtils::MM_Win32->maybe_command($file);
+ } else {
+ ExtUtils::MM_Unix->maybe_command($file);
+ }
+ }
+ }
+}
+
+1;
+
+__END__
+
+#line 157
diff --git a/inc/Module/Install/Fetch.pm b/inc/Module/Install/Fetch.pm
new file mode 100644
index 0000000..0d2c39c
--- /dev/null
+++ b/inc/Module/Install/Fetch.pm
@@ -0,0 +1,93 @@
+#line 1
+package Module::Install::Fetch;
+
+use strict;
+use Module::Install::Base;
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+sub get_file {
+ my ($self, %args) = @_;
+ my ($scheme, $host, $path, $file) =
+ $args{url} =~ m|^(\w+)://([^/]+)(.+)/(.+)| or return;
+
+ if ( $scheme eq 'http' and ! eval { require LWP::Simple; 1 } ) {
+ $args{url} = $args{ftp_url}
+ or (warn("LWP support unavailable!\n"), return);
+ ($scheme, $host, $path, $file) =
+ $args{url} =~ m|^(\w+)://([^/]+)(.+)/(.+)| or return;
+ }
+
+ $|++;
+ print "Fetching '$file' from $host... ";
+
+ unless (eval { require Socket; Socket::inet_aton($host) }) {
+ warn "'$host' resolve failed!\n";
+ return;
+ }
+
+ return unless $scheme eq 'ftp' or $scheme eq 'http';
+
+ require Cwd;
+ my $dir = Cwd::getcwd();
+ chdir $args{local_dir} or return if exists $args{local_dir};
+
+ if (eval { require LWP::Simple; 1 }) {
+ LWP::Simple::mirror($args{url}, $file);
+ }
+ elsif (eval { require Net::FTP; 1 }) { eval {
+ # use Net::FTP to get past firewall
+ my $ftp = Net::FTP->new($host, Passive => 1, Timeout => 600);
+ $ftp->login("anonymous", 'anonymous at example.com');
+ $ftp->cwd($path);
+ $ftp->binary;
+ $ftp->get($file) or (warn("$!\n"), return);
+ $ftp->quit;
+ } }
+ elsif (my $ftp = $self->can_run('ftp')) { eval {
+ # no Net::FTP, fallback to ftp.exe
+ require FileHandle;
+ my $fh = FileHandle->new;
+
+ local $SIG{CHLD} = 'IGNORE';
+ unless ($fh->open("|$ftp -n")) {
+ warn "Couldn't open ftp: $!\n";
+ chdir $dir; return;
+ }
+
+ my @dialog = split(/\n/, <<"END_FTP");
+open $host
+user anonymous anonymous\@example.com
+cd $path
+binary
+get $file $file
+quit
+END_FTP
+ foreach (@dialog) { $fh->print("$_\n") }
+ $fh->close;
+ } }
+ else {
+ warn "No working 'ftp' program available!\n";
+ chdir $dir; return;
+ }
+
+ unless (-f $file) {
+ warn "Fetching failed: $@\n";
+ chdir $dir; return;
+ }
+
+ return if exists $args{size} and -s $file != $args{size};
+ system($args{run}) if exists $args{run};
+ unlink($file) if $args{remove};
+
+ print(((!exists $args{check_for} or -e $args{check_for})
+ ? "done!" : "failed! ($!)"), "\n");
+ chdir $dir; return !$?;
+}
+
+1;
diff --git a/inc/Module/Install/Include.pm b/inc/Module/Install/Include.pm
new file mode 100644
index 0000000..964b93d
--- /dev/null
+++ b/inc/Module/Install/Include.pm
@@ -0,0 +1,34 @@
+#line 1
+package Module::Install::Include;
+
+use strict;
+use Module::Install::Base;
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+sub include {
+ shift()->admin->include(@_);
+}
+
+sub include_deps {
+ shift()->admin->include_deps(@_);
+}
+
+sub auto_include {
+ shift()->admin->auto_include(@_);
+}
+
+sub auto_include_deps {
+ shift()->admin->auto_include_deps(@_);
+}
+
+sub auto_include_dependent_dists {
+ shift()->admin->auto_include_dependent_dists(@_);
+}
+
+1;
diff --git a/inc/Module/Install/Makefile.pm b/inc/Module/Install/Makefile.pm
new file mode 100644
index 0000000..eb67033
--- /dev/null
+++ b/inc/Module/Install/Makefile.pm
@@ -0,0 +1,212 @@
+#line 1
+package Module::Install::Makefile;
+
+use strict 'vars';
+use Module::Install::Base;
+use ExtUtils::MakeMaker ();
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+sub Makefile { $_[0] }
+
+my %seen = ();
+
+sub prompt {
+ shift;
+
+ # Infinite loop protection
+ my @c = caller();
+ if ( ++$seen{"$c[1]|$c[2]|$_[0]"} > 3 ) {
+ die "Caught an potential prompt infinite loop ($c[1]|$c[2]|$_[0])";
+ }
+
+ # In automated testing, always use defaults
+ if ( $ENV{AUTOMATED_TESTING} and ! $ENV{PERL_MM_USE_DEFAULT} ) {
+ local $ENV{PERL_MM_USE_DEFAULT} = 1;
+ goto &ExtUtils::MakeMaker::prompt;
+ } else {
+ goto &ExtUtils::MakeMaker::prompt;
+ }
+}
+
+sub makemaker_args {
+ my $self = shift;
+ my $args = ($self->{makemaker_args} ||= {});
+ %$args = ( %$args, @_ ) if @_;
+ $args;
+}
+
+# For mm args that take multiple space-seperated args,
+# append an argument to the current list.
+sub makemaker_append {
+ my $self = shift;
+ my $name = shift;
+ my $args = $self->makemaker_args;
+ $args->{name} = defined $args->{$name}
+ ? join( ' ', $args->{name}, @_ )
+ : join( ' ', @_ );
+}
+
+sub build_subdirs {
+ my $self = shift;
+ my $subdirs = $self->makemaker_args->{DIR} ||= [];
+ for my $subdir (@_) {
+ push @$subdirs, $subdir;
+ }
+}
+
+sub clean_files {
+ my $self = shift;
+ my $clean = $self->makemaker_args->{clean} ||= {};
+ %$clean = (
+ %$clean,
+ FILES => join(' ', grep length, $clean->{FILES}, @_),
+ );
+}
+
+sub realclean_files {
+ my $self = shift;
+ my $realclean = $self->makemaker_args->{realclean} ||= {};
+ %$realclean = (
+ %$realclean,
+ FILES => join(' ', grep length, $realclean->{FILES}, @_),
+ );
+}
+
+sub libs {
+ my $self = shift;
+ my $libs = ref $_[0] ? shift : [ shift ];
+ $self->makemaker_args( LIBS => $libs );
+}
+
+sub inc {
+ my $self = shift;
+ $self->makemaker_args( INC => shift );
+}
+
+sub write {
+ my $self = shift;
+ die "&Makefile->write() takes no arguments\n" if @_;
+
+ my $args = $self->makemaker_args;
+ $args->{DISTNAME} = $self->name;
+ $args->{NAME} = $self->module_name || $self->name || $self->determine_NAME($args);
+ $args->{VERSION} = $self->version || $self->determine_VERSION($args);
+ $args->{NAME} =~ s/-/::/g;
+ if ( $self->tests ) {
+ $args->{test} = { TESTS => $self->tests };
+ }
+ if ($] >= 5.005) {
+ $args->{ABSTRACT} = $self->abstract;
+ $args->{AUTHOR} = $self->author;
+ }
+ if ( eval($ExtUtils::MakeMaker::VERSION) >= 6.10 ) {
+ $args->{NO_META} = 1;
+ }
+ if ( eval($ExtUtils::MakeMaker::VERSION) > 6.17 and $self->sign ) {
+ $args->{SIGN} = 1;
+ }
+ unless ( $self->is_admin ) {
+ delete $args->{SIGN};
+ }
+
+ # merge both kinds of requires into prereq_pm
+ my $prereq = ($args->{PREREQ_PM} ||= {});
+ %$prereq = ( %$prereq, map { @$_ } map { @$_ } grep $_,
+ ($self->build_requires, $self->requires) );
+
+ # merge both kinds of requires into prereq_pm
+ my $subdirs = ($args->{DIR} ||= []);
+ if ($self->bundles) {
+ foreach my $bundle (@{ $self->bundles }) {
+ my ($file, $dir) = @$bundle;
+ push @$subdirs, $dir if -d $dir;
+ delete $prereq->{$file};
+ }
+ }
+
+ if ( my $perl_version = $self->perl_version ) {
+ eval "use $perl_version; 1"
+ or die "ERROR: perl: Version $] is installed, "
+ . "but we need version >= $perl_version";
+ }
+
+ $args->{INSTALLDIRS} = $self->installdirs;
+
+ my %args = map { ( $_ => $args->{$_} ) } grep {defined($args->{$_})} keys %$args;
+
+ my $user_preop = delete $args{dist}->{PREOP};
+ if (my $preop = $self->admin->preop($user_preop)) {
+ $args{dist} = $preop;
+ }
+
+ my $mm = ExtUtils::MakeMaker::WriteMakefile(%args);
+ $self->fix_up_makefile($mm->{FIRST_MAKEFILE} || 'Makefile');
+}
+
+sub fix_up_makefile {
+ my $self = shift;
+ my $makefile_name = shift;
+ my $top_class = ref($self->_top) || '';
+ my $top_version = $self->_top->VERSION || '';
+
+ my $preamble = $self->preamble
+ ? "# Preamble by $top_class $top_version\n"
+ . $self->preamble
+ : '';
+ my $postamble = "# Postamble by $top_class $top_version\n"
+ . ($self->postamble || '');
+
+ local *MAKEFILE;
+ open MAKEFILE, "< $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
+ my $makefile = do { local $/; <MAKEFILE> };
+ close MAKEFILE or die $!;
+
+ $makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /;
+ $makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g;
+ $makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g;
+ $makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m;
+ $makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m;
+
+ # Module::Install will never be used to build the Core Perl
+ # Sometimes PERL_LIB and PERL_ARCHLIB get written anyway, which breaks
+ # PREFIX/PERL5LIB, and thus, install_share. Blank them if they exist
+ $makefile =~ s/^PERL_LIB = .+/PERL_LIB =/m;
+ #$makefile =~ s/^PERL_ARCHLIB = .+/PERL_ARCHLIB =/m;
+
+ # Perl 5.005 mentions PERL_LIB explicitly, so we have to remove that as well.
+ $makefile =~ s/("?)-I\$\(PERL_LIB\)\1//g;
+
+ # XXX - This is currently unused; not sure if it breaks other MM-users
+ # $makefile =~ s/^pm_to_blib\s+:\s+/pm_to_blib :: /mg;
+
+ open MAKEFILE, "> $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
+ print MAKEFILE "$preamble$makefile$postamble" or die $!;
+ close MAKEFILE or die $!;
+
+ 1;
+}
+
+sub preamble {
+ my ($self, $text) = @_;
+ $self->{preamble} = $text . $self->{preamble} if defined $text;
+ $self->{preamble};
+}
+
+sub postamble {
+ my ($self, $text) = @_;
+ $self->{postamble} ||= $self->admin->postamble;
+ $self->{postamble} .= $text if defined $text;
+ $self->{postamble}
+}
+
+1;
+
+__END__
+
+#line 338
diff --git a/inc/Module/Install/Metadata.pm b/inc/Module/Install/Metadata.pm
new file mode 100644
index 0000000..b5658c9
--- /dev/null
+++ b/inc/Module/Install/Metadata.pm
@@ -0,0 +1,323 @@
+#line 1
+package Module::Install::Metadata;
+
+use strict 'vars';
+use Module::Install::Base;
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+my @scalar_keys = qw{
+ name module_name abstract author version license
+ distribution_type perl_version tests installdirs
+};
+
+my @tuple_keys = qw{
+ build_requires requires recommends bundles
+};
+
+sub Meta { shift }
+sub Meta_ScalarKeys { @scalar_keys }
+sub Meta_TupleKeys { @tuple_keys }
+
+foreach my $key (@scalar_keys) {
+ *$key = sub {
+ my $self = shift;
+ return $self->{values}{$key} if defined wantarray and !@_;
+ $self->{values}{$key} = shift;
+ return $self;
+ };
+}
+
+foreach my $key (@tuple_keys) {
+ *$key = sub {
+ my $self = shift;
+ return $self->{values}{$key} unless @_;
+
+ my @rv;
+ while (@_) {
+ my $module = shift or last;
+ my $version = shift || 0;
+ if ( $module eq 'perl' ) {
+ $version =~ s{^(\d+)\.(\d+)\.(\d+)}
+ {$1 + $2/1_000 + $3/1_000_000}e;
+ $self->perl_version($version);
+ next;
+ }
+ my $rv = [ $module, $version ];
+ push @rv, $rv;
+ }
+ push @{ $self->{values}{$key} }, @rv;
+ @rv;
+ };
+}
+
+sub install_as_core { $_[0]->installdirs('perl') }
+sub install_as_cpan { $_[0]->installdirs('site') }
+sub install_as_site { $_[0]->installdirs('site') }
+sub install_as_vendor { $_[0]->installdirs('vendor') }
+
+sub sign {
+ my $self = shift;
+ return $self->{'values'}{'sign'} if defined wantarray and !@_;
+ $self->{'values'}{'sign'} = ( @_ ? $_[0] : 1 );
+ return $self;
+}
+
+sub dynamic_config {
+ my $self = shift;
+ unless ( @_ ) {
+ warn "You MUST provide an explicit true/false value to dynamic_config, skipping\n";
+ return $self;
+ }
+ $self->{'values'}{'dynamic_config'} = $_[0] ? 1 : 0;
+ return $self;
+}
+
+sub all_from {
+ my ( $self, $file ) = @_;
+
+ unless ( defined($file) ) {
+ my $name = $self->name
+ or die "all_from called with no args without setting name() first";
+ $file = join('/', 'lib', split(/-/, $name)) . '.pm';
+ $file =~ s{.*/}{} unless -e $file;
+ die "all_from: cannot find $file from $name" unless -e $file;
+ }
+
+ $self->version_from($file) unless $self->version;
+ $self->perl_version_from($file) unless $self->perl_version;
+
+ # The remaining probes read from POD sections; if the file
+ # has an accompanying .pod, use that instead
+ my $pod = $file;
+ if ( $pod =~ s/\.pm$/.pod/i and -e $pod ) {
+ $file = $pod;
+ }
+
+ $self->author_from($file) unless $self->author;
+ $self->license_from($file) unless $self->license;
+ $self->abstract_from($file) unless $self->abstract;
+}
+
+sub provides {
+ my $self = shift;
+ my $provides = ( $self->{values}{provides} ||= {} );
+ %$provides = (%$provides, @_) if @_;
+ return $provides;
+}
+
+sub auto_provides {
+ my $self = shift;
+ return $self unless $self->is_admin;
+
+ unless (-e 'MANIFEST') {
+ warn "Cannot deduce auto_provides without a MANIFEST, skipping\n";
+ return $self;
+ }
+
+ # Avoid spurious warnings as we are not checking manifest here.
+
+ local $SIG{__WARN__} = sub {1};
+ require ExtUtils::Manifest;
+ local *ExtUtils::Manifest::manicheck = sub { return };
+
+ require Module::Build;
+ my $build = Module::Build->new(
+ dist_name => $self->name,
+ dist_version => $self->version,
+ license => $self->license,
+ );
+ $self->provides(%{ $build->find_dist_packages || {} });
+}
+
+sub feature {
+ my $self = shift;
+ my $name = shift;
+ my $features = ( $self->{values}{features} ||= [] );
+
+ my $mods;
+
+ if ( @_ == 1 and ref( $_[0] ) ) {
+ # The user used ->feature like ->features by passing in the second
+ # argument as a reference. Accomodate for that.
+ $mods = $_[0];
+ } else {
+ $mods = \@_;
+ }
+
+ my $count = 0;
+ push @$features, (
+ $name => [
+ map {
+ ref($_) ? ( ref($_) eq 'HASH' ) ? %$_
+ : @$_
+ : $_
+ } @$mods
+ ]
+ );
+
+ return @$features;
+}
+
+sub features {
+ my $self = shift;
+ while ( my ( $name, $mods ) = splice( @_, 0, 2 ) ) {
+ $self->feature( $name, @$mods );
+ }
+ return $self->{values}->{features}
+ ? @{ $self->{values}->{features} }
+ : ();
+}
+
+sub no_index {
+ my $self = shift;
+ my $type = shift;
+ push @{ $self->{values}{no_index}{$type} }, @_ if $type;
+ return $self->{values}{no_index};
+}
+
+sub read {
+ my $self = shift;
+ $self->include_deps( 'YAML', 0 );
+
+ require YAML;
+ my $data = YAML::LoadFile('META.yml');
+
+ # Call methods explicitly in case user has already set some values.
+ while ( my ( $key, $value ) = each %$data ) {
+ next unless $self->can($key);
+ if ( ref $value eq 'HASH' ) {
+ while ( my ( $module, $version ) = each %$value ) {
+ $self->can($key)->($self, $module => $version );
+ }
+ }
+ else {
+ $self->can($key)->($self, $value);
+ }
+ }
+ return $self;
+}
+
+sub write {
+ my $self = shift;
+ return $self unless $self->is_admin;
+ $self->admin->write_meta;
+ return $self;
+}
+
+sub version_from {
+ my ( $self, $file ) = @_;
+ require ExtUtils::MM_Unix;
+ $self->version( ExtUtils::MM_Unix->parse_version($file) );
+}
+
+sub abstract_from {
+ my ( $self, $file ) = @_;
+ require ExtUtils::MM_Unix;
+ $self->abstract(
+ bless(
+ { DISTNAME => $self->name },
+ 'ExtUtils::MM_Unix'
+ )->parse_abstract($file)
+ );
+}
+
+sub _slurp {
+ my ( $self, $file ) = @_;
+
+ local *FH;
+ open FH, "< $file" or die "Cannot open $file.pod: $!";
+ do { local $/; <FH> };
+}
+
+sub perl_version_from {
+ my ( $self, $file ) = @_;
+
+ if (
+ $self->_slurp($file) =~ m/
+ ^
+ use \s*
+ v?
+ ([\d_\.]+)
+ \s* ;
+ /ixms
+ )
+ {
+ my $v = $1;
+ $v =~ s{_}{}g;
+ $self->perl_version($1);
+ }
+ else {
+ warn "Cannot determine perl version info from $file\n";
+ return;
+ }
+}
+
+sub author_from {
+ my ( $self, $file ) = @_;
+ my $content = $self->_slurp($file);
+ if ($content =~ m/
+ =head \d \s+ (?:authors?)\b \s*
+ ([^\n]*)
+ |
+ =head \d \s+ (?:licen[cs]e|licensing|copyright|legal)\b \s*
+ .*? copyright .*? \d\d\d[\d.]+ \s* (?:\bby\b)? \s*
+ ([^\n]*)
+ /ixms) {
+ my $author = $1 || $2;
+ $author =~ s{E<lt>}{<}g;
+ $author =~ s{E<gt>}{>}g;
+ $self->author($author);
+ }
+ else {
+ warn "Cannot determine author info from $file\n";
+ }
+}
+
+sub license_from {
+ my ( $self, $file ) = @_;
+
+ if (
+ $self->_slurp($file) =~ m/
+ (
+ =head \d \s+
+ (?:licen[cs]e|licensing|copyright|legal)\b
+ .*?
+ )
+ (=head\\d.*|=cut.*|)
+ \z
+ /ixms
+ )
+ {
+ my $license_text = $1;
+ my @phrases = (
+ 'under the same (?:terms|license) as perl itself' => 'perl',
+ 'GNU public license' => 'gpl',
+ 'GNU lesser public license' => 'gpl',
+ 'BSD license' => 'bsd',
+ 'Artistic license' => 'artistic',
+ 'GPL' => 'gpl',
+ 'LGPL' => 'lgpl',
+ 'BSD' => 'bsd',
+ 'Artistic' => 'artistic',
+ 'MIT' => 'MIT',
+ );
+ while ( my ( $pattern, $license ) = splice( @phrases, 0, 2 ) ) {
+ $pattern =~ s{\s+}{\\s+}g;
+ if ( $license_text =~ /\b$pattern\b/i ) {
+ $self->license($license);
+ return 1;
+ }
+ }
+ }
+
+ warn "Cannot determine license info from $file\n";
+ return 'unknown';
+}
+
+1;
diff --git a/inc/Module/Install/Scripts.pm b/inc/Module/Install/Scripts.pm
new file mode 100644
index 0000000..7a4608c
--- /dev/null
+++ b/inc/Module/Install/Scripts.pm
@@ -0,0 +1,50 @@
+#line 1
+package Module::Install::Scripts;
+
+use strict;
+use Module::Install::Base;
+use File::Basename ();
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+sub prompt_script {
+ my ($self, $script_file) = @_;
+
+ my ($prompt, $abstract, $default);
+ foreach my $line ( $self->_read_script($script_file) ) {
+ last unless $line =~ /^#/;
+ $prompt = $1 if $line =~ /^#\s*prompt:\s+(.*)/;
+ $default = $1 if $line =~ /^#\s*default:\s+(.*)/;
+ $abstract = $1 if $line =~ /^#\s*abstract:\s+(.*)/;
+ }
+ unless (defined $prompt) {
+ my $script_name = File::Basename::basename($script_file);
+ $prompt = "Do you want to install '$script_name'";
+ $prompt .= " ($abstract)" if defined $abstract;
+ $prompt .= '?';
+ }
+ return unless $self->prompt($prompt, ($default || 'n')) =~ /^[Yy]/;
+ $self->install_script($script_file);
+}
+
+sub install_script {
+ my $self = shift;
+ my $args = $self->makemaker_args;
+ my $exe_files = $args->{EXE_FILES} ||= [];
+ push @$exe_files, @_;
+}
+
+sub _read_script {
+ my ($self, $script_file) = @_;
+ local *SCRIPT;
+ open SCRIPT, $script_file
+ or die "Can't open '$script_file' for input: $!\n";
+ return <SCRIPT>;
+}
+
+1;
diff --git a/inc/Module/Install/Win32.pm b/inc/Module/Install/Win32.pm
new file mode 100644
index 0000000..42cb653
--- /dev/null
+++ b/inc/Module/Install/Win32.pm
@@ -0,0 +1,65 @@
+#line 1
+package Module::Install::Win32;
+
+use strict;
+use Module::Install::Base;
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+# determine if the user needs nmake, and download it if needed
+sub check_nmake {
+ my $self = shift;
+ $self->load('can_run');
+ $self->load('get_file');
+
+ require Config;
+ return unless (
+ $^O eq 'MSWin32' and
+ $Config::Config{make} and
+ $Config::Config{make} =~ /^nmake\b/i and
+ ! $self->can_run('nmake')
+ );
+
+ print "The required 'nmake' executable not found, fetching it...\n";
+
+ require File::Basename;
+ my $rv = $self->get_file(
+ url => 'http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/Nmake15.exe',
+ ftp_url => 'ftp://ftp.microsoft.com/Softlib/MSLFILES/Nmake15.exe',
+ local_dir => File::Basename::dirname($^X),
+ size => 51928,
+ run => 'Nmake15.exe /o > nul',
+ check_for => 'Nmake.exe',
+ remove => 1,
+ );
+
+ if (!$rv) {
+ die <<'END_MESSAGE';
+
+-------------------------------------------------------------------------------
+
+Since you are using Microsoft Windows, you will need the 'nmake' utility
+before installation. It's available at:
+
+ http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/Nmake15.exe
+ or
+ ftp://ftp.microsoft.com/Softlib/MSLFILES/Nmake15.exe
+
+Please download the file manually, save it to a directory in %PATH% (e.g.
+C:\WINDOWS\COMMAND\), then launch the MS-DOS command line shell, "cd" to
+that directory, and run "Nmake15.exe" from there; that will create the
+'nmake.exe' file needed by this module.
+
+You may then resume the installation process described in README.
+
+-------------------------------------------------------------------------------
+END_MESSAGE
+ }
+}
+
+1;
diff --git a/inc/Module/Install/WriteAll.pm b/inc/Module/Install/WriteAll.pm
new file mode 100644
index 0000000..d0908fb
--- /dev/null
+++ b/inc/Module/Install/WriteAll.pm
@@ -0,0 +1,43 @@
+#line 1
+package Module::Install::WriteAll;
+
+use strict;
+use Module::Install::Base;
+
+use vars qw{$VERSION $ISCORE @ISA};
+BEGIN {
+ $VERSION = '0.65';
+ $ISCORE = 1;
+ @ISA = qw{Module::Install::Base};
+}
+
+sub WriteAll {
+ my $self = shift;
+ my %args = (
+ meta => 1,
+ sign => 0,
+ inline => 0,
+ check_nmake => 1,
+ @_
+ );
+
+ $self->sign(1) if $args{sign};
+ $self->Meta->write if $args{meta};
+ $self->admin->WriteAll(%args) if $self->is_admin;
+
+ if ( $0 =~ /Build.PL$/i ) {
+ $self->Build->write;
+ } else {
+ $self->check_nmake if $args{check_nmake};
+ unless ( $self->makemaker_args->{'PL_FILES'} ) {
+ $self->makemaker_args( PL_FILES => {} );
+ }
+ if ($args{inline}) {
+ $self->Inline->write;
+ } else {
+ $self->Makefile->write;
+ }
+ }
+}
+
+1;
diff --git a/lib/TheSchwartz.pm b/lib/TheSchwartz.pm
new file mode 100644
index 0000000..db70422
--- /dev/null
+++ b/lib/TheSchwartz.pm
@@ -0,0 +1,824 @@
+# $Id: TheSchwartz.pm 122 2007-05-22 17:11:37Z bradfitz $
+
+package TheSchwartz;
+use strict;
+use fields qw( databases retry_seconds dead_dsns retry_at funcmap_cache verbose all_abilities current_abilities current_job cached_drivers driver_cache_expiration );
+
+our $VERSION = "1.04";
+
+use Carp qw( croak );
+use Data::ObjectDriver::Errors;
+use Data::ObjectDriver::Driver::DBI;
+use Digest::MD5 qw( md5_hex );
+use List::Util qw( shuffle );
+use TheSchwartz::FuncMap;
+use TheSchwartz::Job;
+use TheSchwartz::JobHandle;
+
+use constant RETRY_DEFAULT => 30;
+use constant OK_ERRORS => { map { $_ => 1 } Data::ObjectDriver::Errors->UNIQUE_CONSTRAINT, };
+
+# test harness hooks
+our $T_AFTER_GRAB_SELECT_BEFORE_UPDATE;
+our $T_LOST_RACE;
+
+## Number of jobs to fetch at a time in find_job_for_workers.
+our $FIND_JOB_BATCH_SIZE = 50;
+
+sub new {
+ my TheSchwartz $client = shift;
+ my %args = @_;
+ $client = fields::new($client) unless ref $client;
+
+ croak "databases must be an arrayref if specified"
+ unless !exists $args{databases} || ref $args{databases} eq 'ARRAY';
+ my $databases = delete $args{databases};
+
+ $client->{retry_seconds} = delete $args{retry_seconds} || RETRY_DEFAULT;
+ $client->set_verbose(delete $args{verbose});
+ $client->{driver_cache_expiration} = delete $args{driver_cache_expiration} || 0;
+ croak "unknown options ", join(', ', keys %args) if keys %args;
+
+ $client->hash_databases($databases);
+ $client->reset_abilities;
+ $client->{dead_dsns} = {};
+ $client->{retry_at} = {};
+ $client->{funcmap_cache} = {};
+
+ return $client;
+}
+
+sub debug {
+ my TheSchwartz $client = shift;
+ return unless $client->{verbose};
+ $client->{verbose}->(@_); # ($msg, $job) but $job is optional
+}
+
+sub hash_databases {
+ my TheSchwartz $client = shift;
+ my($list) = @_;
+ for my $ref (@$list) {
+ my $full = join '|', map { $ref->{$_} || '' } qw( dsn user pass );
+ $client->{databases}{ md5_hex($full) } = $ref;
+ }
+}
+
+sub driver_for {
+ my TheSchwartz $client = shift;
+ my($hashdsn) = @_;
+ my $driver;
+ my $t = time;
+ my $cache_duration = $client->{driver_cache_expiration};
+ if ($cache_duration && $client->{cached_drivers}{$hashdsn}{create_ts} && $client->{cached_drivers}{$hashdsn}{create_ts} + $cache_duration > $t) {
+ $driver = $client->{cached_drivers}{$hashdsn}{driver};
+ } else {
+ my $db = $client->{databases}{$hashdsn};
+ $driver = Data::ObjectDriver::Driver::DBI->new(
+ dsn => $db->{dsn},
+ username => $db->{user},
+ password => $db->{pass},
+ ($db->{prefix} ? (prefix => $db->{prefix}) : ()),
+ );
+ if ($cache_duration) {
+ $client->{cached_drivers}{$hashdsn}{driver} = $driver;
+ $client->{cached_drivers}{$hashdsn}{create_ts} = $t;
+ }
+ }
+ return $driver;
+}
+
+sub mark_database_as_dead {
+ my TheSchwartz $client = shift;
+ my($hashdsn) = @_;
+ $client->{dead_dsns}{$hashdsn} = 1;
+ $client->{retry_at}{$hashdsn} = time + $client->{retry_seconds};
+}
+
+sub is_database_dead {
+ my TheSchwartz $client = shift;
+ my($hashdsn) = @_;
+ ## If this database is marked as dead, check the retry time. If
+ ## it has passed, try the database again to see if it's undead.
+ if ($client->{dead_dsns}{$hashdsn}) {
+ if ($client->{retry_at}{$hashdsn} < time) {
+ delete $client->{dead_dsns}{$hashdsn};
+ delete $client->{retry_at}{$hashdsn};
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+sub lookup_job {
+ my TheSchwartz $client = shift;
+ my $handle = $client->handle_from_string(@_);
+ my $driver = $client->driver_for($handle->dsn_hashed);
+
+ my $id = $handle->jobid;
+ my $job = $driver->lookup('TheSchwartz::Job' => $handle->jobid)
+ or return;
+
+ $job->handle($handle);
+ $job->funcname( $client->funcid_to_name($driver, $handle->dsn_hashed, $job->funcid) );
+ return $job;
+}
+
+sub list_jobs {
+ my TheSchwartz $client = shift;
+ my $arg = shift;
+ my @options;
+ push @options, run_after => { op => '<=', value => $arg->{run_after} } if exists $arg->{run_after};
+ push @options, grabbed_until => { op => '<=', value => $arg->{grabbed_until} } if exists $arg->{grabbed_until};
+ die "No funcname" unless exists $arg->{funcname};
+
+ $arg->{want_handle} = 1 unless defined $arg->{want_handle};
+ my $limit = $arg->{limit} || $FIND_JOB_BATCH_SIZE;
+
+ if ($arg->{coalesce}) {
+ $arg->{coalesce_op} ||= '=';
+ push @options, coalesce => { op => $arg->{coalesce_op}, value => $arg->{coalesce}};
+ }
+
+ my @jobs;
+ for my $hashdsn ($client->shuffled_databases) {
+ ## If the database is dead, skip it
+ next if $client->is_database_dead($hashdsn);
+ my $driver = $client->driver_for($hashdsn);
+ my $funcid;
+ if (ref($arg->{funcname})) {
+ $funcid = [map { $client->funcname_to_id($driver, $hashdsn, $_) } @{$arg->{funcname}}];
+ } else {
+ $funcid = $client->funcname_to_id($driver, $hashdsn, $arg->{funcname});
+ }
+
+ if ($arg->{want_handle}) {
+ push @jobs, map {
+ my $handle = TheSchwartz::JobHandle->new({
+ dsn_hashed => $hashdsn,
+ client => $client,
+ jobid => $_->jobid
+ });
+ $_->handle($handle);
+ $_;
+ } $driver->search('TheSchwartz::Job' => {
+ funcid => $funcid,
+ @options
+ }, { limit => $limit });
+ } else {
+ push @jobs, $driver->search('TheSchwartz::Job' => {
+ funcid => $funcid,
+ @options
+ }, { limit => $limit });
+ }
+ }
+ return @jobs;
+}
+
+sub find_job_with_coalescing_prefix {
+ my TheSchwartz $client = shift;
+ my ($funcname, $coval) = @_;
+ $coval .= "%";
+ return $client->_find_job_with_coalescing('LIKE', $funcname, $coval);
+}
+
+sub find_job_with_coalescing_value {
+ my TheSchwartz $client = shift;
+ return $client->_find_job_with_coalescing('=', @_);
+}
+
+sub _find_job_with_coalescing {
+ my TheSchwartz $client = shift;
+ my ($op, $funcname, $coval) = @_;
+
+ for my $hashdsn ($client->shuffled_databases) {
+ ## If the database is dead, skip it
+ next if $client->is_database_dead($hashdsn);
+
+ my $driver = $client->driver_for($hashdsn);
+ my $unixtime = $driver->dbd->sql_for_unixtime;
+
+ my @jobs;
+ eval {
+ ## Search for jobs in this database where:
+ ## 1. funcname is in the list of abilities this $client supports;
+ ## 2. the job is scheduled to be run (run_after is in the past);
+ ## 3. no one else is working on the job (grabbed_until is in
+ ## in the past).
+ my $funcid = $client->funcname_to_id($driver, $hashdsn, $funcname);
+
+ @jobs = $driver->search('TheSchwartz::Job' => {
+ funcid => $funcid,
+ run_after => \ "<= $unixtime",
+ grabbed_until => \ "<= $unixtime",
+ coalesce => { op => $op, value => $coval },
+ }, { limit => $FIND_JOB_BATCH_SIZE });
+ };
+ if ($@) {
+ unless (OK_ERRORS->{ $driver->last_error || 0 }) {
+ $client->mark_database_as_dead($hashdsn);
+ }
+ }
+
+ my $job = $client->_grab_a_job($hashdsn, @jobs);
+ return $job if $job;
+ }
+}
+
+sub find_job_for_workers {
+ my TheSchwartz $client = shift;
+ my($worker_classes) = @_;
+ $worker_classes ||= $client->{current_abilities};
+
+ for my $hashdsn ($client->shuffled_databases) {
+ ## If the database is dead, skip it.
+ next if $client->is_database_dead($hashdsn);
+
+ my $driver = $client->driver_for($hashdsn);
+ my $unixtime = $driver->dbd->sql_for_unixtime;
+
+ my @jobs;
+ eval {
+ ## Search for jobs in this database where:
+ ## 1. funcname is in the list of abilities this $client supports;
+ ## 2. the job is scheduled to be run (run_after is in the past);
+ ## 3. no one else is working on the job (grabbed_until is in
+ ## in the past).
+ my @ids = map { $client->funcname_to_id($driver, $hashdsn, $_) }
+ @$worker_classes;
+
+ @jobs = $driver->search('TheSchwartz::Job' => {
+ funcid => \@ids,
+ run_after => \ "<= $unixtime",
+ grabbed_until => \ "<= $unixtime",
+ }, { limit => $FIND_JOB_BATCH_SIZE });
+ };
+ if ($@) {
+ unless (OK_ERRORS->{ $driver->last_error || 0 }) {
+ $client->mark_database_as_dead($hashdsn);
+ }
+ }
+
+ # for test harness race condition testing
+ $T_AFTER_GRAB_SELECT_BEFORE_UPDATE->() if $T_AFTER_GRAB_SELECT_BEFORE_UPDATE;
+
+ my $job = $client->_grab_a_job($hashdsn, @jobs);
+ return $job if $job;
+ }
+}
+
+sub _grab_a_job {
+ my TheSchwartz $client = shift;
+ my $hashdsn = shift;
+ my $driver = $client->driver_for($hashdsn);
+
+ ## Got some jobs! Randomize them to avoid contention between workers.
+ my @jobs = shuffle(@_);
+
+ JOB:
+ while (my $job = shift @jobs) {
+ ## Convert the funcid to a funcname, based on this database's map.
+ $job->funcname( $client->funcid_to_name($driver, $hashdsn, $job->funcid) );
+
+ ## Update the job's grabbed_until column so that
+ ## no one else takes it.
+ my $worker_class = $job->funcname;
+ my $old_grabbed_until = $job->grabbed_until;
+
+ my $unixtime_sql = $driver->dbd->sql_for_unixtime;
+ my $server_time = $driver->rw_handle->selectrow_array("SELECT $unixtime_sql")
+ or die "expected a server time";
+
+ $job->grabbed_until($server_time + ($worker_class->grab_for || 1));
+
+ ## Update the job in the database, and end the transaction.
+ if ($driver->update($job, { grabbed_until => $old_grabbed_until }) < 1) {
+ ## We lost the race to get this particular job--another worker must
+ ## have got it and already updated it. Move on to the next job.
+ $T_LOST_RACE->() if $T_LOST_RACE;
+ next JOB;
+ }
+
+ ## Now prepare the job, and return it.
+ my $handle = TheSchwartz::JobHandle->new({
+ dsn_hashed => $hashdsn,
+ jobid => $job->jobid,
+ });
+ $handle->client($client);
+ $job->handle($handle);
+ return $job;
+ }
+
+ return undef;
+}
+
+
+sub shuffled_databases {
+ my TheSchwartz $client = shift;
+ my @dsns = keys %{ $client->{databases} };
+ return shuffle(@dsns);
+}
+
+sub insert_job_to_driver {
+ my $client = shift;
+ my($job, $driver, $hashdsn) = @_;
+ eval {
+ ## Set the funcid of the job, based on the funcname. Since each
+ ## database has a separate cache, this needs to be calculated based
+ ## on the hashed DSN. Also: this might fail, if the database is dead.
+ $job->funcid( $client->funcname_to_id($driver, $hashdsn, $job->funcname) );
+
+ ## Now, insert the job. This also might fail.
+ $driver->insert($job);
+ };
+ if ($@) {
+ unless (OK_ERRORS->{ $driver->last_error || 0 }) {
+ $client->mark_database_as_dead($hashdsn);
+ }
+ } elsif ($job->jobid) {
+ ## We inserted the job successfully!
+ ## Attach a handle to the job, and return the handle.
+ my $handle = TheSchwartz::JobHandle->new({
+ dsn_hashed => $hashdsn,
+ client => $client,
+ jobid => $job->jobid
+ });
+ $job->handle($handle);
+ return $handle;
+ }
+ return undef;
+}
+
+sub insert_jobs {
+ my TheSchwartz $client = shift;
+ my (@jobs) = @_;
+
+ ## Try each of the databases that are registered with $client, in
+ ## random order. If we successfully create the job, exit the loop.
+ my @handles;
+ DATABASE:
+ for my $hashdsn ($client->shuffled_databases) {
+ ## If the database is dead, skip it.
+ next if $client->is_database_dead($hashdsn);
+
+ my $driver = $client->driver_for($hashdsn);
+ $driver->begin_work;
+ for my $j (@jobs) {
+ my $h = $client->insert_job_to_driver($j, $driver, $hashdsn);
+ if ($h) {
+ push @handles, $h;
+ } else {
+ $driver->rollback;
+ @handles = ();
+ next DATABASE;
+ }
+ }
+ last if eval { $driver->commit };
+ @handles = ();
+ next DATABASE;
+ }
+
+ return wantarray ? @handles : scalar @handles;
+}
+
+sub insert {
+ my TheSchwartz $client = shift;
+ my $job = shift;
+ if (ref($_[0]) eq "TheSchwartz::Job") {
+ croak "Can't insert multiple jobs with method 'insert'\n";
+ }
+ unless (ref($job) eq 'TheSchwartz::Job') {
+ $job = TheSchwartz::Job->new_from_array($job, $_[0]);
+ }
+
+ ## Try each of the databases that are registered with $client, in
+ ## random order. If we successfully create the job, exit the loop.
+ for my $hashdsn ($client->shuffled_databases) {
+ ## If the database is dead, skip it.
+ next if $client->is_database_dead($hashdsn);
+
+ my $driver = $client->driver_for($hashdsn);
+
+ ## Try to insert the job into this database. If we get a handle
+ ## back, return it.
+ my $handle = $client->insert_job_to_driver($job, $driver, $hashdsn);
+ return $handle if $handle;
+ }
+
+ ## If the job wasn't submitted successfully to any database, return.
+ return undef;
+}
+
+sub handle_from_string {
+ my TheSchwartz $client = shift;
+ my $handle = TheSchwartz::JobHandle->new_from_string(@_);
+ $handle->client($client);
+ return $handle;
+}
+
+sub can_do {
+ my TheSchwartz $client = shift;
+ my($class) = @_;
+ push @{ $client->{all_abilities} }, $class;
+ push @{ $client->{current_abilities} }, $class;
+}
+
+sub reset_abilities {
+ my TheSchwartz $client = shift;
+ $client->{all_abilities} = [];
+ $client->{current_abilities} = [];
+}
+
+sub restore_full_abilities {
+ my $client = shift;
+ $client->{current_abilities} = [ @{ $client->{all_abilities} } ];
+}
+
+sub temporarily_remove_ability {
+ my $client = shift;
+ my($class) = @_;
+ $client->{current_abilities} = [
+ grep { $_ ne $class } @{ $client->{current_abilities} }
+ ];
+ if (!@{ $client->{current_abilities} }) {
+ $client->restore_full_abilities;
+ }
+}
+
+sub work_on {
+ my TheSchwartz $client = shift;
+ my $hstr = shift; # Handle string
+ my $job = $client->lookup_job($hstr) or
+ return 0;
+ return $client->work_once($job);
+}
+
+sub work {
+ my TheSchwartz $client = shift;
+ my($delay) = @_;
+ $delay ||= 5;
+ while (1) {
+ sleep $delay unless $client->work_once;
+ }
+}
+
+sub work_until_done {
+ my TheSchwartz $client = shift;
+ while (1) {
+ $client->work_once or last;
+ }
+}
+
+## Returns true if it did something, false if no jobs were found
+sub work_once {
+ my TheSchwartz $client = shift;
+ my $job = shift; # optional specific job to work on
+
+ ## Look for a job with our current set of abilities. Note that the
+ ## list of current abilities may not be equal to the full set of
+ ## abilities, to allow for even distribution between jobs.
+ $job ||= $client->find_job_for_workers;
+
+ ## If we didn't find anything, restore our full abilities, and try
+ ## again.
+ if (!$job &&
+ @{ $client->{current_abilities} } < @{ $client->{all_abilities} }) {
+ $client->restore_full_abilities;
+ $job = $client->find_job_for_workers;
+ }
+
+ my $class = $job ? $job->funcname : undef;
+ if ($job) {
+ $job->debug("TheSchwartz::work_once got job of class '$class'");
+ } else {
+ $client->debug("TheSchwartz::work_once found no jobs");
+ }
+
+ ## If we still don't have anything, return.
+ return unless $job;
+
+ ## Now that we found a job for this particular funcname, remove it
+ ## from our list of current abilities. So the next time we look for a
+ ## we'll find a job for a different funcname. This prevents starvation of
+ ## high funcid values because of the way MySQL's indexes work.
+ $client->temporarily_remove_ability($class);
+
+ $class->work_safely($job);
+
+ ## We got a job, so return 1 so work_until_done (which calls this method)
+ ## knows to keep looking for jobs.
+ return 1;
+}
+
+sub funcid_to_name {
+ my TheSchwartz $client = shift;
+ my($driver, $hashdsn, $funcid) = @_;
+ my $cache = $client->_funcmap_cache($hashdsn);
+ return $cache->{funcid2name}{$funcid};
+}
+
+sub funcname_to_id {
+ my TheSchwartz $client = shift;
+ my($driver, $hashdsn, $funcname) = @_;
+ my $cache = $client->_funcmap_cache($hashdsn);
+ unless (exists $cache->{funcname2id}{$funcname}) {
+ my $map = TheSchwartz::FuncMap->create_or_find($driver, $funcname);
+ $cache->{funcname2id}{ $map->funcname } = $map->funcid;
+ $cache->{funcid2name}{ $map->funcid } = $map->funcname;
+ }
+ return $cache->{funcname2id}{$funcname};
+}
+
+sub _funcmap_cache {
+ my TheSchwartz $client = shift;
+ my($hashdsn) = @_;
+ unless (exists $client->{funcmap_cache}{$hashdsn}) {
+ my $driver = $client->driver_for($hashdsn);
+ my @maps = $driver->search('TheSchwartz::FuncMap');
+ my $cache = { funcname2id => {}, funcid2name => {} };
+ for my $map (@maps) {
+ $cache->{funcname2id}{ $map->funcname } = $map->funcid;
+ $cache->{funcid2name}{ $map->funcid } = $map->funcname;
+ }
+ $client->{funcmap_cache}{$hashdsn} = $cache;
+ }
+ return $client->{funcmap_cache}{$hashdsn};
+}
+
+# accessors
+
+sub verbose {
+ my TheSchwartz $client = shift;
+ return $client->{verbose};
+}
+
+sub set_verbose {
+ my TheSchwartz $client = shift;
+ my $logger = shift; # or non-coderef to just print to stderr
+ if ($logger && ref $logger ne "CODE") {
+ $logger = sub {
+ my $msg = shift;
+ $msg =~ s/\s+$//;
+ print STDERR "$msg\n";
+ };
+ }
+ $client->{verbose} = $logger;
+}
+
+# current job being worked. so if something dies, work_safely knows which to mark as dead.
+sub current_job {
+ my TheSchwartz $client = shift;
+ $client->{current_job};
+}
+
+sub set_current_job {
+ my TheSchwartz $client = shift;
+ $client->{current_job} = shift;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+TheSchwartz - reliable job queue
+
+=head1 SYNOPSIS
+
+ # MyApp.pm
+ package MyApp;
+
+ sub work_asynchronously {
+ my %args = @_;
+
+ my $client = TheSchwartz->new( databases => $DATABASE_INFO );
+ $client->insert('MyWorker', \%args);
+ }
+
+
+ # myworker.pl
+ package MyWorker;
+ use base qw( TheSchwartz::Worker );
+
+ sub work {
+ my $class = shift;
+ my TheSchwartz::Job $job = shift;
+
+ print "Workin' hard or hardly workin'? Hyuk!!\n";
+
+ $job->completed();
+ }
+
+ package main;
+
+ my $client = TheSchwartz->new( databases => $DATABASE_INFO );
+ $client->can_do('MyWorker');
+ $client->work();
+
+
+=head1 DESCRIPTION
+
+TheSchwartz is a reliable job queue system. Your application can put jobs into
+the system, and your worker processes can pull jobs from the queue atomically
+to perform. Failed jobs can be left in the queue to retry later.
+
+I<Abilities> specify what jobs a worker process can perform. Abilities are the
+names of C<TheSchwartz::Worker> subclasses, as in the synopsis: the C<MyWorker>
+class name is used to specify that the worker script can perform the job. When
+using the C<TheSchwartz> client's C<work> functions, the class-ability duality
+is used to automatically dispatch to the proper class to do the actual work.
+
+TheSchwartz clients will also prefer to do jobs for unused abilities before
+reusing a particular ability, to avoid exhausting the supply of one kind of job
+while jobs of other types stack up.
+
+Some jobs with high setup times can be performed more efficiently if a group of
+related jobs are performed together. TheSchwartz offers a facility to
+I<coalesce> jobs into groups, which a properly constructed worker can find and
+perform at once. For example, if your worker were delivering email, you might
+store the domain name from the recipient's address as the coalescing value. The
+worker that grabs that job could then batch deliver all the mail for that
+domain once it connects to that domain's mail server.
+
+=head1 USAGE
+
+=head2 C<TheSchwartz-E<gt>new( %args )>
+
+Optional members of C<%args> are:
+
+=over 4
+
+=item * C<databases>
+
+An arrayref of database information. TheSchwartz workers can use multiple
+databases, such that if any of them are unavailable, the worker will search for
+appropriate jobs in the other databases automatically.
+
+Each member of the C<databases> value should be a hashref containing:
+
+=over 4
+
+=item * C<dsn>
+
+The database DSN for this database.
+
+=item * C<user>
+
+The username to use when connecting to this database.
+
+=item * C<pass>
+
+The password to use when connecting to this database.
+
+=back
+
+=item * C<verbose>
+
+A value indicating whether to log debug messages. If C<verbose> is a coderef,
+it is called to log debug messages. If C<verbose> is not a coderef but is some
+other true value, debug messages will be sent to C<STDERR>. Otherwise, debug
+messages will not be logged.
+
+=item * C<driver_cache_expiration>
+
+Optional value to control how long database connections are cached for in seconds.
+By default, connections are not cached. To re-use the same database connection for
+five minutes, pass driver_cache_expiration => 300 to the constructor. Improves job
+throughput in cases where the work to process a job is small compared to the database
+connection set-up and tear-down time.
+
+=item * C<retry_seconds>
+
+The number of seconds after which to try reconnecting to apparently dead
+databases. If not given, TheSchwartz will retry connecting to databases after
+30 seconds.
+
+=back
+
+=head2 C<$client-E<gt>list_jobs( %args )>
+
+Returns a list of C<TheSchwartz::Job> objects matching the given arguments. The
+required members of C<%args> are:
+
+=over 4
+
+=item * C<funcname>
+
+the name of the function or a reference to an array of functions
+
+=item * C<run_after>
+
+the value you want to check <= against on the run_after column
+
+=item * C<grabbed_until>
+
+the value you want to check <= against on the grabbed_until column
+
+=item * C<coalesce_op>
+
+defaults to '=', set it to whatever you want to compare the coalesce field too
+if you want to search, you can use 'LIKE'
+
+=item * C<coalesce>
+
+coalesce value to search for, if you set op to 'LIKE' you can use '%' here,
+do remember that '%' searches anchored at the beginning of the string are
+much faster since it is can do a btree index lookup
+
+=item * C<want_handle>
+
+if you want all your jobs to be set up using a handle. defaults to true.
+this option might be removed, as you should always have this on a Job object.
+
+=back
+
+It is important to remember that this function doesnt lock anything, it just
+returns as many jobs as there is up to amount of databases * FIND_JOB_BATCH_SIZE
+
+=head2 C<$client-E<gt>lookup_job( $handle_id )>
+
+Returns a C<TheSchwartz::Job> corresponding to the given handle ID.
+
+=head2 C<$client-E<gt>set_verbose( $verbose )>
+
+Sets the current logging function to C<$verbose> if it's a coderef. If not a
+coderef, enables debug logging to C<STDERR> if C<$verbose> is true; otherwise,
+disables logging.
+
+=head1 POSTING JOBS
+
+The methods of TheSchwartz clients used by applications posting jobs to the
+queue are:
+
+=head2 C<$client-E<gt>insert( $job )>
+
+Adds the given C<TheSchwartz::Job> to one of the client's job databases.
+
+=head2 C<$client-E<gt>insert( $funcname, $arg )>
+
+Adds a new job with funcname C<$funcname> and arguments C<$arg> to the queue.
+
+=head2 C<$client-E<gt>insert_jobs( @jobs )>
+
+Adds the given C<TheSchwartz::Job> objects to one of the client's job
+databases. All the given jobs are recorded in I<one> job database.
+
+=head1 WORKING
+
+The methods of TheSchwartz clients for use in worker processes are:
+
+=head2 C<$client-E<gt>can_do( $ability )>
+
+Adds C<$ability> to the list of abilities C<$client> is capable of performing.
+Subsequent calls to that client's C<work> methods will find jobs requiring the
+given ability.
+
+=head2 C<$client-E<gt>work_once()>
+
+Find and perform one job C<$client> can do.
+
+=head2 C<$client-E<gt>work_until_done()>
+
+Find and perform jobs C<$client> can do until no more such jobs are found in
+any of the client's job databases.
+
+=head2 C<$client-E<gt>work( [$delay] )>
+
+Find and perform any jobs C<$client> can do, forever. When no job is available,
+the working process will sleep for C<$delay> seconds (or 5, if not specified)
+before looking again.
+
+=head2 C<$client-E<gt>find_job_for_workers( [$abilities] )>
+
+Returns a C<TheSchwartz::Job> for a random job that the client can do. If
+specified, the job returned matches one of the abilities in the arrayref
+C<$abilities>, rather than C<$client>'s abilities.
+
+=head2 C<$client-E<gt>find_job_with_coalescing_value( $ability, $coval )>
+
+Returns a C<TheSchwartz::Job> for a random job for a worker capable of
+C<$ability> and with a coalescing value of C<$coval>.
+
+=head2 C<$client-E<gt>find_job_with_coalescing_prefix( $ability, $coval )>
+
+Returns a C<TheSchwartz::Job> for a random job for a worker capable of
+C<$ability> and with a coalescing value beginning with C<$coval>.
+
+Note the C<TheSchwartz> implementation of this function uses a C<LIKE> query to
+find matching jobs, with all the attendant performance implications for your
+job databases.
+
+=head1 COPYRIGHT, LICENSE & WARRANTY
+
+This software is Copyright 2007, Six Apart Ltd, cpan at sixapart.com. All
+rights reserved.
+
+TheSchwartz is free software; you may redistribute it and/or modify it
+under the same terms as Perl itself.
+
+TheScwhartz comes with no warranty of any kind.
+
+=cut
+
diff --git a/lib/TheSchwartz/Error.pm b/lib/TheSchwartz/Error.pm
new file mode 100644
index 0000000..3b6eeea
--- /dev/null
+++ b/lib/TheSchwartz/Error.pm
@@ -0,0 +1,12 @@
+# $Id: Error.pm 101 2006-08-23 21:22:49Z bradfitz $
+
+package TheSchwartz::Error;
+use strict;
+use base qw( Data::ObjectDriver::BaseObject );
+
+__PACKAGE__->install_properties({
+ columns => [ qw( jobid funcid message error_time ) ],
+ datasource => 'error',
+ });
+
+1;
diff --git a/lib/TheSchwartz/ExitStatus.pm b/lib/TheSchwartz/ExitStatus.pm
new file mode 100644
index 0000000..8794f11
--- /dev/null
+++ b/lib/TheSchwartz/ExitStatus.pm
@@ -0,0 +1,14 @@
+# $Id: ExitStatus.pm 102 2006-08-23 21:40:26Z bradfitz $
+
+package TheSchwartz::ExitStatus;
+use strict;
+use base qw( Data::ObjectDriver::BaseObject );
+
+__PACKAGE__->install_properties({
+ columns => [ qw( jobid status funcid
+ completion_time delete_after ) ],
+ datasource => 'exitstatus',
+ primary_key => 'jobid',
+ });
+
+1;
diff --git a/lib/TheSchwartz/FuncMap.pm b/lib/TheSchwartz/FuncMap.pm
new file mode 100644
index 0000000..3179082
--- /dev/null
+++ b/lib/TheSchwartz/FuncMap.pm
@@ -0,0 +1,36 @@
+# $Id: FuncMap.pm 42 2006-05-16 05:15:30Z btrott $
+
+package TheSchwartz::FuncMap;
+use strict;
+use base qw( Data::ObjectDriver::BaseObject );
+
+use Carp qw( croak );
+
+__PACKAGE__->install_properties({
+ columns => [ qw( funcid funcname ) ],
+ datasource => 'funcmap',
+ primary_key => 'funcid',
+ });
+
+sub create_or_find {
+ my $class = shift;
+ my($driver, $funcname) = @_;
+
+ ## Attempt to insert a new funcmap row. Since the funcname column is
+ ## UNIQUE, if the row already exists, an exception will be thrown.
+ my $map = $class->new;
+ $map->funcname($funcname);
+ eval { $driver->insert($map) };
+
+ ## If we got an exception, try to load the record with this funcname;
+ ## in all likelihood, the exception was that the record was added by
+ ## another process.
+ if (my $err = $@) {
+ ($map) = $driver->search('TheSchwartz::FuncMap' =>
+ { funcname => $funcname }
+ ) or croak "Can't find or create funcname $funcname: $err";
+ }
+ return $map;
+}
+
+1;
diff --git a/lib/TheSchwartz/Job.pm b/lib/TheSchwartz/Job.pm
new file mode 100644
index 0000000..a541522
--- /dev/null
+++ b/lib/TheSchwartz/Job.pm
@@ -0,0 +1,499 @@
+# $Id$
+
+package TheSchwartz::Job;
+use strict;
+use base qw( Data::ObjectDriver::BaseObject );
+
+use Carp qw( croak );
+use Storable ();
+use TheSchwartz::Error;
+use TheSchwartz::ExitStatus;
+use TheSchwartz::JobHandle;
+
+__PACKAGE__->install_properties({
+ columns => [qw(jobid funcid arg uniqkey insert_time
+ run_after grabbed_until priority coalesce)],
+ datasource => 'job',
+ column_defs => { arg => 'blob' },
+ primary_key => 'jobid',
+ });
+
+__PACKAGE__->add_trigger(pre_save => sub {
+ my ($job) = @_;
+ my $arg = $job->arg
+ or return;
+ if (ref($arg)) {
+ $job->arg(Storable::nfreeze($arg));
+ }
+});
+
+__PACKAGE__->add_trigger(post_load => sub {
+ my ($job) = @_;
+ my $arg = $job->arg
+ or return;
+ $job->arg(_cond_thaw($job->arg))
+});
+
+sub new_from_array {
+ my $class = shift;
+ my(@arg) = @_;
+ croak "usage: new_from_array(funcname, arg)" unless @arg == 2;
+ return $class->new(
+ funcname => $arg[0],
+ arg => $arg[1],
+ );
+}
+
+sub new {
+ my $class = shift;
+ my(%param) = @_;
+ my $job = $class->SUPER::new;
+ if (my $arg = $param{arg}) {
+ if (ref($arg) eq 'SCALAR') {
+ $param{arg} = Storable::thaw($$arg);
+ } elsif (!ref($arg)) {
+ # if a regular scalar, test to see if it's a storable or not.
+ $param{arg} = _cond_thaw($arg);
+ }
+ }
+ $param{run_after} ||= time;
+ $param{grabbed_until} ||= 0;
+ for my $key (keys %param) {
+ $job->$key($param{$key});
+ }
+ return $job;
+}
+
+sub funcname {
+ my $job = shift;
+ if (@_) {
+ $job->{__funcname} = shift;
+ }
+
+ # lazily load,
+ if (!$job->{__funcname}) {
+ my $handle = $job->handle;
+ my $client = $handle->client;
+ my $driver = $client->driver_for($handle->dsn_hashed);
+ my $funcname = $client->funcid_to_name($driver, $handle->dsn_hashed, $job->funcid)
+ or die "Failed to lookup funcname of job $job";
+ return $job->{__funcname} = $funcname;
+ }
+ return $job->{__funcname};
+}
+
+sub handle {
+ my $job = shift;
+ if (@_) {
+ $job->{__handle} = $_[0];
+ }
+ return $job->{__handle};
+}
+
+sub driver {
+ my $job = shift;
+ unless (exists $job->{__driver}) {
+ my $handle = $job->handle;
+ $job->{__driver} = $handle->client->driver_for($handle->dsn_hashed);
+ }
+ return $job->{__driver};
+}
+
+sub add_failure {
+ my $job = shift;
+ my($msg) = @_;
+ my $error = TheSchwartz::Error->new;
+ $error->error_time(time());
+ $error->jobid($job->jobid);
+ $error->funcid($job->funcid);
+ $error->message($msg || '');
+
+ my $driver = $job->driver;
+ $driver->insert($error);
+
+ # and let's lazily clean some errors while we're here.
+ my $unixtime = $driver->dbd->sql_for_unixtime;
+ my $maxage = $TheSchwartz::T_ERRORS_MAX_AGE || (86400*7);
+ $driver->remove('TheSchwartz::Error', {
+ error_time => \ "< $unixtime - $maxage",
+ }, {
+ nofetch => 1,
+ limit => $driver->dbd->can_delete_with_limit ? 1000 : undef,
+ });
+
+ return $error;
+}
+
+sub exit_status { shift->handle->exit_status }
+sub failure_log { shift->handle->failure_log }
+sub failures { shift->handle->failures }
+
+sub set_exit_status {
+ my $job = shift;
+ my($exit) = @_;
+ my $class = $job->funcname;
+ my $secs = $class->keep_exit_status_for or return;
+ my $status = TheSchwartz::ExitStatus->new;
+ $status->jobid($job->jobid);
+ $status->funcid($job->funcid);
+ $status->completion_time(time);
+ $status->delete_after($status->completion_time + $secs);
+ $status->status($exit);
+
+ my $driver = $job->driver;
+ $driver->insert($status);
+
+ # and let's lazily clean some exitstatus while we're here. but
+ # rather than doing this query all the time, we do it 1/nth of the
+ # time, and deleting up to n*10 queries while we're at it.
+ # default n is 10% of the time, doing 100 deletes.
+ my $clean_thres = $TheSchwartz::T_EXITSTATUS_CLEAN_THRES || 0.10;
+ if (rand() < $clean_thres) {
+ my $unixtime = $driver->dbd->sql_for_unixtime;
+ $driver->remove('TheSchwartz::ExitStatus', {
+ delete_after => \ "< $unixtime",
+ }, {
+ nofetch => 1,
+ limit => $driver->dbd->can_delete_with_limit ? int(1 / $clean_thres * 100) : undef,
+ });
+ }
+
+ return $status;
+}
+
+sub did_something {
+ my $job = shift;
+ if (@_) {
+ $job->{__did_something} = shift;
+ }
+ return $job->{__did_something};
+}
+
+sub debug {
+ my ($job, $msg) = @_;
+ $job->handle->client->debug($msg, $job);
+}
+
+sub completed {
+ my $job = shift;
+ $job->debug("job completed");
+ if ($job->did_something) {
+ $job->debug("can't call 'completed' on already finished job");
+ return 0;
+ }
+ $job->did_something(1);
+ $job->set_exit_status(0);
+ $job->driver->remove($job);
+}
+
+sub permanent_failure {
+ my ($job, $msg, $ex_status) = @_;
+ if ($job->did_something) {
+ $job->debug("can't call 'permanent_failure' on already finished job");
+ return 0;
+ }
+ $job->_failed($msg, $ex_status, 0);
+}
+
+sub failed {
+ my ($job, $msg, $ex_status) = @_;
+ if ($job->did_something) {
+ $job->debug("can't call 'failed' on already finished job");
+ return 0;
+ }
+
+ ## If this job class specifies that jobs should be retried,
+ ## update the run_after if necessary, but keep the job around.
+
+ my $class = $job->funcname;
+ my $failures = $job->failures + 1; # include this one, since we haven't ->add_failure yet
+ my $max_retries = $class->max_retries($job);
+
+ $job->debug("job failed. considering retry. is max_retries of $max_retries >= failures of $failures?");
+ $job->_failed($msg, $ex_status, $max_retries >= $failures, $failures);
+}
+
+sub _failed {
+ my ($job, $msg, $exit_status, $_retry, $failures) = @_;
+ $job->did_something(1);
+ $job->debug("job failed: " . ($msg || "<no message>"));
+
+ ## Mark the failure in the error table.
+ $job->add_failure($msg);
+
+ if ($_retry) {
+ my $class = $job->funcname;
+ if (my $delay = $class->retry_delay($failures)) {
+ $job->run_after(time() + $delay);
+ }
+ $job->grabbed_until(0);
+ $job->driver->update($job);
+ } else {
+ $job->set_exit_status($exit_status || 1);
+ $job->driver->remove($job);
+ }
+}
+
+sub replace_with {
+ my $job = shift;
+ my(@jobs) = @_;
+
+ if ($job->did_something) {
+ $job->debug("can't call 'replace_with' on already finished job");
+ return 0;
+ }
+ # Note: we don't set 'did_something' here because completed does it down below.
+
+ ## The new jobs @jobs should be inserted into the same database as $job,
+ ## which they're replacing. So get a driver for the database that $job
+ ## belongs to.
+ my $handle = $job->handle;
+ my $client = $handle->client;
+ my $hashdsn = $handle->dsn_hashed;
+ my $driver = $job->driver;
+
+ $job->debug("replacing job with " . (scalar @jobs) . " other jobs");
+
+ ## Start a transaction.
+ $driver->begin_work;
+
+ ## Insert the new jobs.
+ for my $j (@jobs) {
+ $client->insert_job_to_driver($j, $driver, $hashdsn);
+ }
+
+ ## Mark the original job as completed successfully.
+ $job->completed;
+
+ # for testing
+ if ($TheSchwartz::Job::_T_REPLACE_WITH_FAIL) {
+ $driver->rollback;
+ die "commit failed for driver: due to testing\n";
+ }
+
+ ## Looks like it's all ok, so commit.
+ $driver->commit;
+}
+
+sub set_as_current {
+ my $job = shift;
+ my $client = $job->handle->client;
+ $client->set_current_job($job);
+}
+
+sub _cond_thaw {
+ my $data = shift;
+
+ my $magic = eval { Storable::read_magic($data); };
+ if ($magic && $magic->{major} && $magic->{major} >= 2 && $magic->{major} <= 5) {
+ my $thawed = eval { Storable::thaw($data) };
+ if ($@) {
+ # false alarm... looked like a Storable, but wasn't.
+ return $data;
+ }
+ return $thawed;
+ } else {
+ return $data;
+ }
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+TheSchwartz::Job - jobs for the reliable job queue
+
+=head1 SYNOPSIS
+
+ my $client = TheSchwartz->new( databases => $DATABASE_INFO );
+
+ my $job = TheSchwartz::Job->new_from_array('MyWorker', foo => 'bar');
+ $client->dispatch_async($job);
+
+ $job = TheSchwartz::Job->new(
+ funcname => 'MyWorker',
+ uniqkey => 7,
+ arg => [ foo => 'bar' ],
+ );
+ $client->dispatch_async($job);
+
+=head1 DESCRIPTION
+
+C<TheSchwartz::Job> models the jobs that are posted to the job queue by your
+application, then grabbed and performed by your worker processes.
+
+C<TheSchwartz::Job> is a C<Data::ObjectDriver> model class. See
+L<Data::ObjectDriver::BaseObject>.
+
+=head1 FIELDS
+
+C<TheSchwartz::Job> objects have these possible fields:
+
+=head2 C<jobid>
+
+The unique numeric identifier for this job. Set automatically when saved.
+
+=head2 C<funcid>
+
+The numeric identifier for the type of job to perform. C<TheSchwartz> clients
+map function names (also known as abilities and worker class names) to these
+numbers using C<TheSchwartz::FuncMap> records.
+
+=head2 C<arg>
+
+Arbitrary state data to supply to the worker process for this job. If specified
+as a reference, the data is frozen to a blob with the C<Storable> module.
+
+=head2 C<uniqkey>
+
+An arbitrary string identifier used to prevent applications from posting
+duplicate jobs. At most one with the same C<uniqkey> value can be posted to a
+single C<TheSchwartz> database.
+
+=head2 C<insert_time>
+
+The C<insert_time> field is not used.
+
+=head2 C<run_after>
+
+The UNIX system time after which the job can next be attempted by a worker
+process. This timestamp is set when a job is first created or is released after
+a failure.
+
+=head2 C<grabbed_until>
+
+The UNIX system time after which the job can next be available by a worker
+process. This timestamp is set when a job is grabbed by a worker process, and
+reset to C<0> when is released due to failure to complete the job.
+
+=head2 C<priority>
+
+The C<priority> field is not used.
+
+=head2 C<coalesce>
+
+A string used to discover jobs that can be efficiently pipelined with a given
+job due to some shared resource. For example, for email delivery jobs, the
+domain of an email address could be used as the C<coalesce> value. A worker
+process could then deliver all the mail queued for a given mail host after
+connecting to it once.
+
+=head1 USAGE
+
+=head2 C<TheSchwartz::Job-E<gt>new( %args )>
+
+Returns a new job object with the given data. Members of C<%args> can be keyed
+on any of the fields described above, or C<funcname>.
+
+=head2 C<TheSchwartz::Job-E<gt>new_from_array( $funcname, $arg )>
+
+Returns a new job with the given function name (also called I<ability> or
+I<worker class>), and the scalar or reference C<$arg> for an argument.
+
+=head2 C<$job-E<gt>funcname([ $funcname ])>
+
+Returns the function name for the given job, after setting it to C<$funcname>,
+if specified.
+
+=head2 C<$job-E<gt>handle([ $handle ])>
+
+Returns the C<TheSchwartz::JobHandle> object describing this job, after setting
+it to C<$handle>, if specified. A I<job handle> is a convenience class for
+accessing other records related to jobs; as its convenience methods are also
+available directly from C<TheSchwartz::Job> instances, you will usually not
+need to work directly with job handles.
+
+=head2 C<$job-E<gt>driver()>
+
+Returns the C<Data::ObjectDriver> object driver for accessing the database in
+which C<$job> is stored. See L<Data::ObjectDriver>.
+
+=head2 C<$job-E<gt>add_failure( $msg )>
+
+Records and returns a new C<TheSchwartz::Error> object representing a failure
+to perform C<$job>, for reason C<$msg>.
+
+=head2 C<$job-E<gt>exit_status()>
+
+Returns the I<exit status> specified by the worker that either completed the
+job or declared it failed permanently. The exit status for a job will be
+available for a period of time after the job has exited the queue. That time is
+defined in the job's worker class's C<keep_exit_status_for()> method.
+
+=head2 C<$job-E<gt>failure_log()>
+
+Returns a list of the error messages specified to C<add_failure()> when a
+worker failed to perform the given job.
+
+=head2 C<$job-E<gt>failures()>
+
+Returns the number of times a worker has grabbed this job, only to fail to
+complete it.
+
+=head2 C<$job-E<gt>set_exit_status( $status )>
+
+Records the exit status of the given job as C<$status>.
+
+=head2 C<$job-E<gt>did_something([ $value ])>
+
+Returns whether the given job has been completed or failed since it was created
+or loaded, setting whether it has to C<$value> first, if specified.
+
+=head2 C<$job-E<gt>debug( $msg )>
+
+Sends the given message to the job's C<TheSchwartz> client as debug output.
+
+=head2 C<$job-E<gt>set_as_current()>
+
+Set C<$job> as the current job being performed by its associated C<TheSchwartz>
+client.
+
+=head1 WORKING
+
+C<TheSchwartz::Worker> classes should use these methods to update the status of
+their jobs:
+
+=head2 C<$job-E<gt>completed()>
+
+Records that the given job has been fully performed and removes it from the job
+queue. Completing a job records its exit status as C<0>.
+
+=head2 C<$job-E<gt>failed( $msg, $exit_status )>
+
+Records that the worker performing this job failed to complete it, for reason
+C<$msg>.
+
+If workers have not failed to complete the job more times than the maximum
+number of retries for that type of job, the job will be reattempted after its
+retry delay has elapsed. The maximum number of retries and the delay before a
+retry are defined in the job's worker class definition as C<max_retries()> and
+C<retry_delay()> respectively.
+
+If workers I<have> exceeded the maximum number of reattempts for this job, the
+job's exit status is recorded as C<$exit_status>, and the job is removed from
+the queue. If C<$exit_status> is not defined or C<0>, the job will be recorded
+with an exit status of C<1>, to indicate a failure.
+
+=head2 C<$job-E<gt>permanent_failure( $msg, $exit_status )>
+
+Records that the worker performing this job failed to complete it, as in
+C<failed()>, but that the job should I<not> be reattempted, no matter how many
+times the job has been attempted before. The job's exit status is thus recorded
+as C<$exit_status> (or C<1>), and the job is removed from the queue.
+
+=head2 C<$job-E<gt>replace_with( @jobs )>
+
+Atomically replaces the single job C<$job> with the given set of jobs.
+
+This can be used to decompose one "metajob" posted by your application into a
+set of jobs workers can perform, or to post a job or jobs required to complete
+the process already partly performed.
+
+=head1 SEE ALSO
+
+L<Data::ObjectDriver>, L<Data::ObjectDriver::BaseObject>, L<Storable>
+
+=cut
+
diff --git a/lib/TheSchwartz/JobHandle.pm b/lib/TheSchwartz/JobHandle.pm
new file mode 100644
index 0000000..639d00e
--- /dev/null
+++ b/lib/TheSchwartz/JobHandle.pm
@@ -0,0 +1,68 @@
+# $Id: JobHandle.pm 24 2006-05-01 19:40:38Z btrott $
+
+package TheSchwartz::JobHandle;
+use strict;
+use base qw( Class::Accessor::Fast );
+
+__PACKAGE__->mk_accessors(qw( dsn_hashed jobid client ));
+
+use TheSchwartz::ExitStatus;
+use TheSchwartz::Job;
+
+sub new_from_string {
+ my $class = shift;
+ my($hstr) = @_;
+ my($hashdsn, $jobid) = split /\-/, $hstr, 2;
+ return TheSchwartz::JobHandle->new({
+ dsn_hashed => $hashdsn,
+ jobid => $jobid,
+ });
+}
+
+sub as_string {
+ my $handle = shift;
+ return join '-', $handle->dsn_hashed, $handle->jobid;
+}
+
+sub driver {
+ my $handle = shift;
+ unless (exists $handle->{__driver}) {
+ $handle->{__driver} = $handle->client->driver_for($handle->dsn_hashed);
+ }
+ return $handle->{__driver};
+}
+
+sub job {
+ my $handle = shift;
+ my $job = $handle->client->lookup_job($handle->as_string) or return;
+ $job->handle($handle);
+ return $job;
+}
+
+sub is_pending {
+ my $handle = shift;
+ return $handle->job ? 1 : 0;
+}
+
+sub exit_status {
+ my $handle = shift;
+ my $status = $handle->driver->lookup(
+ 'TheSchwartz::ExitStatus' => $handle->jobid
+ ) or return;
+ return $status->status;
+}
+
+sub failure_log {
+ my $handle = shift;
+ my @failures = $handle->driver->search('TheSchwartz::Error' =>
+ { jobid => $handle->jobid },
+ );
+ return map { $_->message } @failures;
+}
+
+sub failures {
+ my $handle = shift;
+ return scalar $handle->failure_log;
+}
+
+1;
diff --git a/lib/TheSchwartz/Worker.pm b/lib/TheSchwartz/Worker.pm
new file mode 100644
index 0000000..bbe61ab
--- /dev/null
+++ b/lib/TheSchwartz/Worker.pm
@@ -0,0 +1,141 @@
+# $Id: Worker.pm 106 2006-10-16 21:42:32Z mpaschal $
+
+package TheSchwartz::Worker;
+use strict;
+
+use Carp qw( croak );
+use Storable ();
+
+sub grab_job {
+ my $class = shift;
+ my($client) = @_;
+ return $client->find_job_for_workers([ $class ]);
+}
+
+sub keep_exit_status_for { 0 }
+sub max_retries { 0 }
+sub retry_delay { 0 }
+sub grab_for { 60 * 60 } ## 1 hour
+
+sub work_safely {
+ my ($class, $job) = @_;
+ my $client = $job->handle->client;
+ my $res;
+
+ $job->debug("Working on $class ...");
+ $job->set_as_current;
+ eval {
+ $res = $class->work($job);
+ };
+
+ my $cjob = $client->current_job;
+ if ($@) {
+ $job->debug("Eval failure: $@");
+ $cjob->failed($@);
+ }
+ unless ($cjob->did_something) {
+ $cjob->failed('Job did not explicitly complete, fail, or get replaced');
+ }
+
+ # FIXME: this return value is kinda useless/undefined. should we even return anything? any callers? -brad
+ return $res;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+TheSchwartz::Worker - superclass for defining task behavior
+
+=head1 SYNOPSIS
+
+ package MyWorker;
+ use base qw( TheSchwartz::Worker );
+
+ sub work {
+ my $class = shift;
+ my TheSchwartz::Job $job = shift;
+
+ print "Workin' hard or hardly workin'? Hyuk!!\n";
+
+ $job->completed();
+ }
+
+
+ package main;
+
+ my $client = TheSchwartz->new( databases => $DATABASE_INFO );
+ $client->can_do('MyWorker');
+ $client->work();
+
+=head1 DESCRIPTION
+
+I<TheSchwartz::Worker> objects are the salt of the reliable job queuing earth.
+The behavior required to perform posted jobs are defined in subclasses of
+I<TheSchwartz::Worker>. These subclasses are named for the ability required of
+a C<TheSchwartz> client to do the job, so that the clients can dispatch
+automatically to the approprate worker routine.
+
+Because jobs can be performed by any machine running code for capable worker
+classes, C<TheSchwartz::Worker>s are generally stateless. All mutable state is
+stored in the C<TheSchwartz::Job> objects. This means all
+C<TheSchwartz::Worker> methods are I<class> methods, and C<TheSchwartz::Worker>
+classes are generally never instantiated.
+
+=head1 SUBCLASSING
+
+Define and customize how a job is performed by overriding these methods in your
+subclass:
+
+=head2 C<$class-E<gt>work( $job )>
+
+Performs the job that required ability C<$class>. Override this method to
+define how to do the job you're defining.
+
+Note that will need to call C<$job-E<gt>completed()> or C<$job-E<gt>failed()>
+as appropriate to indicate success or failure. See L<TheSchwartz::Job>.
+
+=head2 C<$class-E<gt>max_retries( $job )>
+
+Returns the number of times workers should attempt the given job. After this
+many tries, the job is marked as completed with errors (that is, a
+C<TheSchwartz::ExitStatus> is recorded for it) and removed from the queue. By
+default, returns 0.
+
+=head2 C<$class-E<gt>retry_delay( $num_failures )>
+
+Returns the number of seconds after a failure workers should wait until
+reattempting a job that has already failed C<$num_failures> times. By default,
+returns 0.
+
+=head2 C<$class-E<gt>keep_exit_status_for()>
+
+Returns the number of seconds to allow a C<TheSchwartz::ExitStatus> record for
+a job performed by this worker class to exist. By default, returns 0.
+
+=head2 C<$class-E<gt>grab_for()>
+
+Returns the number of seconds workers of this class will claim a grabbed a job.
+That is, returns the length of the I<timeout> after which other workers will
+decide a worker that claimed a job has crashed or faulted without marking the
+job failed. Jobs that are marked as failed by a worker are also marked for
+immediate retry after a delay indicated by C<retry_delay()>.
+
+=head1 USAGE
+
+=head2 C<$class-E<gt>grab_job( $client )>
+
+Finds and claims a job for workers with ability C<$class>, using C<TheSchwartz>
+client C<$client>. This job can then be passed to C<work()> or C<work_safely()>
+to perform it.
+
+=head2 C<$class-E<gt>work_safely( $job )>
+
+Performs the job associated with the worker's class name. If an error is thrown
+while doing the job, the job is appropriately marked as failed, unlike when
+calling C<work()> directly.
+
+=cut
+
diff --git a/t/05-job-ctor.t b/t/05-job-ctor.t
new file mode 100644
index 0000000..13cedea
--- /dev/null
+++ b/t/05-job-ctor.t
@@ -0,0 +1,69 @@
+# $Id$
+
+use strict;
+use warnings;
+use Test::More tests => 19;
+
+use TheSchwartz;
+use Storable;
+
+# With this test, all data structures are in memory so far. Nothing's
+# been inserted into the database because we have no client object
+# yet with which to insert.
+
+my $args = { scoops => 2, with => ['cheese','love'] };
+my $fargs = Storable::nfreeze($args);
+
+my $job1 = TheSchwartz::Job->new_from_array("feedmajor", $fargs);
+isa_ok($job1, 'TheSchwartz::Job');
+my $job2 = TheSchwartz::Job->new_from_array("feedmajor", \$fargs);
+isa_ok($job2, 'TheSchwartz::Job');
+my $job3 = TheSchwartz::Job->new(funcname => 'feedmajor', arg => $args);
+isa_ok($job3, 'TheSchwartz::Job');
+my $job4 = TheSchwartz::Job->new(funcname => 'feedmajor', arg => $fargs);
+isa_ok($job4, 'TheSchwartz::Job');
+my $job5 = TheSchwartz::Job->new(funcname => 'feedmajor', arg => \$fargs);
+isa_ok($job5, 'TheSchwartz::Job');
+
+is_deeply($job1->column_values, $job2->column_values, "job2 is equivalent");
+is_deeply($job1->column_values, $job3->column_values, "job3 is equivalent");
+is_deeply($job1->column_values, $job4->column_values, "job4 is equivalent");
+is_deeply($job1->column_values, $job5->column_values, "job5 is equivalent");
+
+my $job6 = TheSchwartz::Job->new(
+ funcname => 'feeddog',
+ run_after => time() + 60,
+ priority => 7,
+ arg => { scoops => 2, with => ['cheese','love'] },
+ coalesce => 'major',
+ jobid => int(rand()*5000),
+ );
+isa_ok $job6, 'TheSchwartz::Job';
+
+# second arg can also be an arrayref
+my $job_a1 = TheSchwartz::Job->new_from_array("feedmajor", [ 'cheese', 'water', 'beer' ]);
+my $job_a2 = TheSchwartz::Job->new(funcname => "feedmajor",
+ arg => [ 'cheese', 'water', 'beer' ]);
+is_deeply($job_a1->column_values, $job_a2->column_values, "ctors with arrayrefs match");
+
+my $jobbad = eval { TheSchwartz::Job->new(
+ funcname => 'feeddog',
+ run_atter => time() + 60, # [sic] typo
+ ) };
+ok(!$jobbad, "no bad job");
+ok($@, "error creating job with bad argument");
+
+# can't have multiple non-ref args
+$jobbad = eval { TheSchwartz::Job->new_from_array("feeddog", "scalar1", "scalar2") };
+ok(!$jobbad, "no bad job");
+ok($@, "error creating job with bad argument");
+
+# can't have multiple non-ref args, even if first is scalarref
+$jobbad = eval { TheSchwartz::Job->new_from_array("feeddog", \ "scalar1", "scalar2") };
+ok(!$jobbad, "no bad job");
+ok($@, "error creating job with bad argument");
+
+# can't have multiple non-ref args, even if first is hashrf
+$jobbad = eval { TheSchwartz::Job->new_from_array("feeddog", { with => 'poison' }, { extra => 'arg' }); };
+ok(!$jobbad, "no bad job");
+ok($@, "error creating job with bad argument");
diff --git a/t/api.t b/t/api.t
new file mode 100644
index 0000000..bdb5790
--- /dev/null
+++ b/t/api.t
@@ -0,0 +1,104 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 80;
+
+run_tests(40, sub {
+ foreach my $pfx ("", "testprefix_") {
+
+ my $client = test_client(dbs => ['ts1'],
+ dbprefix => $pfx,
+ );
+
+ my $handle;
+
+ $handle = $client->insert("feedmajor", { scoops => 2, with => ['cheese','love'] });
+ isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";
+ is($handle->is_pending, 1, "job is still pending");
+ is($handle->exit_status, undef, "job hasn't exitted yet");
+
+ # to give to javascript, perl, etc...
+ my $hstr = $handle->as_string; # <digestofdsn>-<jobid>
+ ok($hstr, "handle stringifies");
+
+ my $job = $handle->job;
+ isa_ok $job, 'TheSchwartz::Job';
+ is $job->funcname, 'feedmajor', 'handle->job gives us the right job';
+
+ # getting a handle object back
+ my $hand2 = $client->handle_from_string($hstr);
+ ok($hand2, "handle recreated from stringified version");
+ is($handle->is_pending, 1, "job is still pending");
+ is($handle->exit_status, undef, "job hasn't exitted yet");
+
+ $job = $handle->job;
+ isa_ok $job, 'TheSchwartz::Job';
+ is $job->funcname, 'feedmajor', 'recreated handle gives us the right job';
+
+ $job = TheSchwartz::Job->new(
+ funcname => 'feedmajor',
+ run_after=> time() + 60,
+ priority => 7,
+ arg => { scoops => 2, with => ['cheese','love'] },
+ coalesce => 'major',
+ jobid => int rand(5000),
+ );
+ ok($job);
+
+ $handle = $client->insert($job);
+ isa_ok $handle, 'TheSchwartz::JobHandle';
+
+ # inserting multiple at a time in scalar context
+ {
+ my $job1 = TheSchwartz::Job->new(funcname => 'feedmajor');
+ my $job2 = TheSchwartz::Job->new(funcname => 'feedmajor');
+ my $rv = $client->insert_jobs($job1, $job2);
+ is($rv, 2, "inserted two jobs");
+ }
+
+ # inserting multiple at a time in list context
+ {
+ my $job1 = TheSchwartz::Job->new(funcname => 'feedmajor');
+ my $job2 = TheSchwartz::Job->new(funcname => 'feedmajor');
+ my @handles = $client->insert_jobs($job1, $job2);
+ is(scalar @handles, 2, "inserted two jobs");
+ isa_ok $handles[0], 'TheSchwartz::JobHandle', "got job handle";
+ }
+
+ # inserting with a regular scalar arg
+ {
+ $job = TheSchwartz::Job->new(
+ funcname => 'feedmajor',
+ arg => "gruel that's longer than 11 bytes, for sure!",
+ );
+ ok($job);
+ $handle = $client->insert($job);
+ isa_ok $handle, 'TheSchwartz::JobHandle';
+ }
+
+ # inserting multiple with wrong method fails
+ eval {
+ my $job1 = TheSchwartz::Job->new(funcname => 'feedmajor');
+ my $job2 = TheSchwartz::Job->new(funcname => 'feedmajor');
+ my @handles = $client->insert($job1, $job2);
+ };
+ like($@, qr/multiple jobs with method/, "used wrong method");
+
+ # insert multiple that fail
+ {
+ my $job1 = TheSchwartz::Job->new(funcname => 'feedmajor', uniqkey => 'u1');
+ my $job2 = TheSchwartz::Job->new(funcname => 'feedmajor', uniqkey => 'u1');
+ my @handles = $client->insert_jobs($job1, $job2);
+ is(scalar @handles, 0, "failed to insert anything");
+ }
+
+
+ teardown_dbs('ts1');
+ }
+});
diff --git a/t/cleanup.t b/t/cleanup.t
new file mode 100644
index 0000000..e85ed72
--- /dev/null
+++ b/t/cleanup.t
@@ -0,0 +1,87 @@
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 20;
+
+# for testing:
+$TheSchwartz::T_EXITSTATUS_CLEAN_THRES = 1; # delete 100% of the time, not 10% of the time
+$TheSchwartz::T_ERRORS_MAX_AGE = 2; # keep errors for 3 seconds, not 1 week
+
+run_tests(10, sub {
+ my $client = test_client(dbs => ['ts1']);
+ my $dbh = DBI->connect(dsn_for("ts1"), 'root', '');
+ $client->can_do("Worker::Fail");
+ $client->can_do("Worker::Complete");
+
+ # insert a job which will fail, then succeed.
+ {
+ my $handle = $client->insert("Worker::Fail");
+ isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";
+
+ $client->work_until_done;
+ is($handle->failures, 1, "job has failed once");
+
+ my $min;
+ my $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus");
+ is($rows, 1, "has 1 exitstatus row");
+
+ ok($client->insert("Worker::Complete"), "inserting to-pass job");
+ $client->work_until_done;
+ $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus");
+ is($rows, 2, "has 2 exitstatus rows");
+ ($rows, $min) = $dbh->selectrow_array("SELECT COUNT(*), MIN(jobid) FROM error");
+ is($rows, 1, "has 1 error rows");
+ is($min, 1, "error jobid is the old one");
+
+ # wait for exit status to pass
+ sleep 3;
+
+ # now make another job fail to cleanup some errors
+ $handle = $client->insert("Worker::Fail");
+ $client->work_until_done;
+
+ $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus");
+ is($rows, 1, "1 exit status row now");
+
+ ($rows, $min) = $dbh->selectrow_array("SELECT COUNT(*), MIN(jobid) FROM error");
+ is($rows, 1, "has 1 error row still");
+ is($min, 3, "error jobid is only the new one");
+
+ }
+
+ teardown_dbs('ts1');
+});
+
+############################################################################
+package Worker::Fail;
+use base 'TheSchwartz::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ $job->failed("an error message");
+ return;
+}
+
+sub keep_exit_status_for { 1 } # keep exit status for 20 seconds after on_complete
+
+sub max_retries { 0 }
+
+sub retry_delay { 1 }
+
+# ---------------
+
+package Worker::Complete;
+use base 'TheSchwartz::Worker';
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+ return;
+}
+
+sub keep_exit_status_for { 1 }
+
diff --git a/t/client-time-unsync.t b/t/client-time-unsync.t
new file mode 100644
index 0000000..7a0442d
--- /dev/null
+++ b/t/client-time-unsync.t
@@ -0,0 +1,108 @@
+# $Id$
+# -*-perl-*-
+#
+# This test tests one client with good time, who grabs a job for 5 seconds. But while he's
+# working on it, another client comes along with a clock set to the future, and grabs the job
+# but getting it, since for it, 5 seconds has passed.
+#
+# This tests that the library doesn't rely on the client's time, but the server's time.
+#
+
+use strict;
+use warnings;
+
+# make time() be overridable in the future at runtime, rather than be an opcode:
+BEGIN { *CORE::GLOBAL::time = sub { time() }; }
+no warnings 'redefine';
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 2;
+
+# how we keep track of if job was done twice: signal from children back up to us
+my $got_job = 0;
+my $got_done = 0;
+$SIG{USR1} = sub { $got_job++; };
+$SIG{USR2} = sub { $got_done++; };
+
+# kill children on exit
+my %children; # pid -> 1
+my $parent = $$;
+END {
+ if ($$ == $parent) {
+ my @pids = keys %children;
+ kill 9, @pids if @pids;
+ }
+}
+
+run_tests_innodb(2, sub {
+
+ # put one job into database
+ my $client = test_client(dbs => ['ts1']);
+ $client->insert("Worker::Addition", { numbers => [1, 2] })
+ or die;
+
+ # two children to race. this one with normal time:
+ work();
+
+ # let first dude get started first
+ select(undef, undef, undef, 1.5);
+
+ # make this worker 60 seconds in the future: (well past the grabbed until time)
+ work(60);
+
+ # hang out waiting for children to finish or timeout
+ my $now = time();
+ while ($got_done < 2 && time() < $now + 7) {
+ sleep 1;
+ }
+
+ is($got_done, 2, "two children finished");
+ is($got_job, 1, "only did one job");
+
+ teardown_dbs('ts1');
+});
+
+sub work {
+ my $future = shift;
+
+ # parent:
+ if (my $childpid = fork()) {
+ $children{$childpid} = 1;
+ return;
+ }
+
+ if ($future) {
+ *CORE::GLOBAL::time = sub { CORE::time() + $future };
+ }
+
+ my $client = test_client(dbs => ['ts1'],
+ init => 0);
+
+
+ # child:
+ while (my $job = Worker::Addition->grab_job($client)) {
+ eval { Worker::Addition->work($job); };
+ }
+
+ kill 'USR2', getppid();
+ exit 0;
+}
+
+############################################################################
+package Worker::Addition;
+use base 'TheSchwartz::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ sleep 3;
+ kill 'USR1', getppid();
+ $job->completed;
+}
+
+# tell framework to set 'grabbed_until' to time() + 60. because if
+# we can't add some numbers in 30 seconds, our process probably
+# failed and work should be reassigned.
+sub grab_for { 5 }
+
diff --git a/t/coalesce.t b/t/coalesce.t
new file mode 100644
index 0000000..a40ac0d
--- /dev/null
+++ b/t/coalesce.t
@@ -0,0 +1,74 @@
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 28;
+
+run_tests(14, sub {
+ my $client = test_client(dbs => ['ts1']);
+
+ my @keys = qw(foo bar baz);
+ my $n = 0;
+ for (1..10) {
+ my $key = $keys[$n++ % 3];
+ my $job = TheSchwartz::Job->new(
+ funcname => 'Worker::CoalesceTest',
+ arg => { key => $key, num => $_ },
+ coalesce => $key
+ );
+ my $h = $client->insert($job);
+ ok($h, "inserted $h ($_ = $key)");
+ }
+
+ $client->reset_abilities;
+ $client->can_do("Worker::CoalesceTest");
+
+ Worker::CoalesceTest->set_client($client);
+
+ for (1..3) {
+ my $rv = eval { $client->work_once; };
+ ok($rv, "did stuff");
+ }
+ my $rv = eval { $client->work_once; };
+ ok(!$rv, "nothing to do now");
+
+ teardown_dbs('ts1');
+});
+
+############################################################################
+package Worker::CoalesceTest;
+use base 'TheSchwartz::Worker';
+
+my $client;
+sub set_client { $client = $_[1]; }
+
+sub work {
+ my ($class, $job) = @_;
+ my $args = $job->arg;
+
+ my $key = $args->{key};
+ $job->completed;
+
+ if ($key eq "foo") {
+ while (my $job = $client->find_job_with_coalescing_prefix("Worker::CoalesceTest", "f")) {
+ $job->completed;
+ }
+ } else {
+ while (my $job = $client->find_job_with_coalescing_value("Worker::CoalesceTest", $key)) {
+ $job->completed;
+ }
+ }
+}
+
+sub keep_exit_status_for { 20 } # keep exit status for 20 seconds after on_complete
+
+sub grab_for { 10 }
+
+sub max_retries { 1 }
+
+sub retry_delay { my $class = shift; my $fails = shift; return 2 ** $fails; }
+
diff --git a/t/dead-dbs.t b/t/dead-dbs.t
new file mode 100644
index 0000000..251e6ba
--- /dev/null
+++ b/t/dead-dbs.t
@@ -0,0 +1,51 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 4;
+
+run_tests(2, sub {
+ setup_dbs('ts1');
+ teardown_dbs('ts2'); # doesn't exist
+
+ my $client = test_client(dbs => ['ts2', 'ts1'],
+ init => 0);
+
+ # insert a job
+ my $n_handles = 0;
+ for (1..50) {
+ my $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });
+ $n_handles++ if $handle;
+ }
+ is($n_handles, 50, "got 50 handles");
+
+ # let's do some work. the tedious way, specifying which class should grab a job
+ my $n_grabbed = 0;
+ while (my $job = Worker::Addition->grab_job($client)) {
+ $n_grabbed++;
+ }
+ is($n_grabbed, 50, "grabbed 50 times");
+
+ teardown_dbs('ts1');
+});
+
+############################################################################
+package Worker::Addition;
+use base 'TheSchwartz::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+
+ # ....
+}
+
+# tell framework to set 'grabbed_until' to time() + 60. because if
+# we can't add some numbers in 30 seconds, our process probably
+# failed and work should be reassigned.
+sub grab_for { 30 }
+
diff --git a/t/empty-db.t b/t/empty-db.t
new file mode 100644
index 0000000..6ef8c6f
--- /dev/null
+++ b/t/empty-db.t
@@ -0,0 +1,35 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 6;
+
+run_tests(3, sub {
+ teardown_dbs("tempty1");
+
+ my $client = TheSchwartz->new(databases => [
+ {
+ dsn => dsn_for('tempty1'),
+ user => "root",
+ pass => "",
+ },
+ ]);
+
+ # insert a job
+ {
+ my $handle;
+ $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });
+ ok(!$handle, "can't insert into empty database");
+ $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });
+ ok(!$handle, "still can't insert into empty database");
+ }
+
+ ok(1, "test finishes");
+ teardown_dbs("tempty1");
+});
+
diff --git a/t/evenly-distribute.t b/t/evenly-distribute.t
new file mode 100644
index 0000000..9155c27
--- /dev/null
+++ b/t/evenly-distribute.t
@@ -0,0 +1,61 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 8;
+
+run_tests(4, sub {
+ my $client = test_client(dbs => ['ts1', 'ts2']);
+
+ my $n_jobs = 60;
+ for (1..$n_jobs) {
+ my $handle = $client->insert("Worker::Foo");
+ die unless $handle;
+ }
+
+ my $db1 = DBI->connect(dsn_for("ts1"), 'root', '');
+ my $db2 = DBI->connect(dsn_for("ts2"), 'root', '');
+ die unless $db1 && $db2;
+
+ my $jobs1 = $db1->selectrow_array("SELECT COUNT(*) FROM job");
+ my $jobs2 = $db2->selectrow_array("SELECT COUNT(*) FROM job");
+ is($jobs1 + $jobs2, $n_jobs, "inserted all $n_jobs");
+
+ ok($jobs1 > $n_jobs / 4, "at least a quarter of jobs went to db1 ($jobs1 / $n_jobs)");
+ ok($jobs2 > $n_jobs / 4, "at least a quarter of jobs went to db1 ($jobs2 / $n_jobs)");
+
+ my $do_jobs = int($n_jobs / 2);
+ $client->can_do("Worker::Foo");
+ for (1..$do_jobs) {
+ $client->work_once
+ or die;
+ }
+
+ my $jobs1b = $db1->selectrow_array("SELECT COUNT(*) FROM job");
+ my $jobs2b = $db2->selectrow_array("SELECT COUNT(*) FROM job");
+
+ my $remain_jobs = $n_jobs - $do_jobs;
+ is($jobs1b + $jobs2b, $remain_jobs, "expected jobs remain");
+
+ # deltas: how much work gone done each
+ my $jobs1d = $jobs1 - $jobs1b;
+ my $jobs2d = $jobs2 - $jobs2b;
+
+ # difference in work done:
+ my $workdiff = abs($jobs1d - $jobs2d);
+
+});
+
+sub max { $_[0] > $_[1] ? $_[0] : $_[1] }
+
+package Worker::Foo;
+use base 'TheSchwartz::Worker';
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+}
diff --git a/t/fail-working-multiple.t b/t/fail-working-multiple.t
new file mode 100644
index 0000000..eca5725
--- /dev/null
+++ b/t/fail-working-multiple.t
@@ -0,0 +1,61 @@
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 8;
+
+run_tests(4, sub {
+ my $client = test_client(dbs => ['ts1']);
+
+ my $job2h;
+ for (1..2) {
+ my $job = TheSchwartz::Job->new(
+ funcname => 'Worker::CoalesceTest',
+ arg => { n => $_ },
+ coalesce => "a$_",
+ );
+ my $h = $client->insert($job);
+ $job2h = $h if $_ == 2;
+ ok($h, "inserted $h");
+ }
+
+ $client->reset_abilities;
+ $client->can_do("Worker::CoalesceTest");
+
+ my $job = $client->find_job_with_coalescing_prefix("Worker::CoalesceTest", "a1");
+ Worker::CoalesceTest->work_safely($job);
+
+ # this one should have succeeded:
+ is($job->handle->failures, 0, "no failures on first job");
+
+ # the second one should have failures:
+ is($job2h->failures, 1, "1 failure on second job");
+
+ teardown_dbs('ts1');
+});
+
+############################################################################
+package Worker::CoalesceTest;
+use base 'TheSchwartz::Worker';
+
+
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+ my $arg = $job->arg;
+
+ my $job2 = $job->handle->client->find_job_with_coalescing_prefix("Worker::CoalesceTest", "a2");
+ $job2->set_as_current;
+ die "Failed working on job2\n";
+}
+
+sub keep_exit_status_for { 20 } # keep exit status for 20 seconds after on_complete
+sub grab_for { 10 }
+sub max_retries { 1 }
+sub retry_delay { 10 }
+
+
diff --git a/t/funcid.t b/t/funcid.t
new file mode 100644
index 0000000..a9ba62f
--- /dev/null
+++ b/t/funcid.t
@@ -0,0 +1,44 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 16;
+
+run_tests(8, sub {
+
+ my $client = test_client(dbs => ['ts1']);
+
+ my $handle;
+ $handle = $client->insert("feedmajor", { scoops => 2, with => ['cheese','love'] });
+ isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";
+
+ my $job = $handle->job;
+ isa_ok $job, 'TheSchwartz::Job';
+
+ ok($job->funcid, 'jobs have funcids');
+ is $job->funcname, 'feedmajor', 'handle->job gives us the right job';
+
+ my $job2 = TheSchwartz::Job->new(
+ funcname => 'feedmajor',
+ run_after=> time() + 60,
+ priority => 7,
+ arg => { scoops => 2, with => ['cheese','love'] },
+ coalesce => 'major',
+ jobid => int rand(5000),
+ );
+ ok($job2);
+
+ my $h2 = $client->insert($job2);
+ isa_ok $h2, 'TheSchwartz::JobHandle';
+
+ my $job2_back = $h2->job;
+ ok($job2->funcid, "internal: funcid present");
+ is($job2->funcname, "feedmajor", "funcname mapping worked");
+
+ teardown_dbs('ts1');
+});
diff --git a/t/grab-race.t b/t/grab-race.t
new file mode 100644
index 0000000..923f9ba
--- /dev/null
+++ b/t/grab-race.t
@@ -0,0 +1,88 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 2;
+
+# how we keep track of if job was done twice: signal from children back up to us
+my $work_count = 0;
+$SIG{USR1} = sub {
+ $work_count++;
+};
+
+# force the race condition to happen
+{
+ no warnings 'once';
+ $TheSchwartz::T_AFTER_GRAB_SELECT_BEFORE_UPDATE = sub {
+ select undef, undef, undef, 1.5;
+ };
+}
+
+# kill children on exit
+my %children; # pid -> 1
+END {
+ my @pids = keys %children;
+ kill -9, @pids if @pids;
+}
+
+run_tests_innodb(2, sub {
+
+ # get one job into database, to see if children do it twice:
+ {
+ my $client = test_client(dbs => ['ts1']);
+ my $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });
+ isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";
+ }
+
+ # two children to race to get the above job.
+ work();
+ work();
+
+ # hang out for 3 seconds waiting for children to init/race/finish
+ my $now = time();
+ while (time() < $now + 3) {
+ sleep 1;
+ }
+
+ is($work_count, 1, "only got one signal from worker children");
+ teardown_dbs('ts1');
+});
+
+sub work {
+ # parent:
+ if (my $childpid = fork()) {
+ $children{$childpid} = 1;
+ return;
+ }
+
+ my $client = test_client(dbs => ['ts1'],
+ init => 0);
+
+ # child:
+ my $job = Worker::Addition->grab_job($client);
+ if ($job) {
+ eval { Worker::Addition->work($job); };
+ }
+ exit 0;
+}
+
+############################################################################
+package Worker::Addition;
+use base 'TheSchwartz::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ kill 'USR1', getppid();
+ $job->completed;
+}
+
+# tell framework to set 'grabbed_until' to time() + 60. because if
+# we can't add some numbers in 30 seconds, our process probably
+# failed and work should be reassigned.
+sub grab_for { 30 }
+
diff --git a/t/high-funcid-starvation.t b/t/high-funcid-starvation.t
new file mode 100644
index 0000000..2b764e3
--- /dev/null
+++ b/t/high-funcid-starvation.t
@@ -0,0 +1,54 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 8;
+
+run_tests(4, sub {
+ my $client = test_client(dbs => ['ts1']);
+
+ my $n_jobs = 10;
+ for (1..$n_jobs) {
+ $client->insert("Worker::Job1") or die;
+ $client->insert("Worker::Job2") or die;
+ }
+
+ my $db1 = DBI->connect(dsn_for("ts1"), 'root', '');
+ die unless $db1;
+
+ my $jobs1 = $db1->selectrow_array("SELECT COUNT(*) FROM job WHERE funcid=1");
+ is($jobs1, $n_jobs, "have $n_jobs funcid 1s");
+ my $jobs2 = $db1->selectrow_array("SELECT COUNT(*) FROM job WHERE funcid=2");
+ is($jobs2, $n_jobs, "have $n_jobs funcid 2s");
+
+ my $do_jobs = int($n_jobs / 2);
+ $client->can_do("Worker::Job1");
+ $client->can_do("Worker::Job2");
+ for (1..($do_jobs * 2)) {
+ $client->work_once
+ or die "Couldn't find job to do";
+ }
+
+ my $jobs1b = $db1->selectrow_array("SELECT COUNT(*) FROM job WHERE funcid=1");
+ is($jobs1b, $n_jobs - $do_jobs, "have half funcid 1s");
+ my $jobs2b = $db1->selectrow_array("SELECT COUNT(*) FROM job WHERE funcid=2");
+ is($jobs2b, $n_jobs - $do_jobs, "have half funcid 2s");
+
+
+});
+
+package Worker::Job1;
+use base 'TheSchwartz::Worker';
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+}
+
+package Worker::Job2;
+use base 'Worker::Job1';
+
diff --git a/t/insert-and-do.t b/t/insert-and-do.t
new file mode 100644
index 0000000..bfee381
--- /dev/null
+++ b/t/insert-and-do.t
@@ -0,0 +1,152 @@
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 52;
+
+run_tests(26, sub {
+ my $client = test_client(dbs => ['ts1']);
+
+ # insert a job
+ {
+ my $handle = $client->insert("Worker::Addition", { numbers => [1, 2] });
+ isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";
+ }
+
+ # let's do some work. the tedious way, specifying which class should grab a job
+ {
+ my $job = Worker::Addition->grab_job($client);
+ isa_ok $job, 'TheSchwartz::Job';
+ my $args = $job->arg;
+ is(ref $args, "HASH"); # thawed it for us
+ is_deeply($args, { numbers => [1, 2] }, "got our args back");
+
+ # insert a dummy job to test that next grab ignors it
+ ok($client->insert("dummy", [1,2,3]));
+
+ # verify no more jobs can be grabbed of this type, even though
+ # we haven't done the first one
+ my $job2 = Worker::Addition->grab_job($client);
+ ok(!$job2, "no addition jobs to be grabbed");
+
+ my $rv = eval { Worker::Addition->work($job); };
+ # ....
+ }
+
+ # inserting and getting job w/ regular scalar arg
+ foreach my $scalar ("short_arg",
+ "long arg more than 11 bytes long",
+ "\x05scalar that begins with the 5 byte",
+ )
+ {
+ my $handle = $client->insert("Worker::Addition", $scalar);
+ isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";
+
+ my $job = Worker::Addition->grab_job($client);
+ isa_ok $job, 'TheSchwartz::Job';
+ my $args = $job->arg;
+ ok(!ref $args, "not a reference"); # not a reference
+ is($args, $scalar, "got correct scalar arg back");
+ }
+
+ # insert some more jobs
+ {
+ ok($client->insert("Worker::MergeInternalDict", { foo => 'bar' }));
+ ok($client->insert("Worker::MergeInternalDict", { bar => 'baz' }));
+ ok($client->insert("Worker::MergeInternalDict", { baz => 'foo' }));
+ }
+
+ # work the easier way
+ {
+ Worker::MergeInternalDict->reset;
+ $client->can_do("Worker::MergeInternalDict"); # single arg form: say we can do this job name, which is also its package
+ $client->work_until_done; # blocks until all databases are empty
+ is_deeply(Worker::MergeInternalDict->dict,
+ {
+ foo => "bar",
+ bar => "baz",
+ baz => "foo",
+ }, "all jobs got completed");
+ }
+
+ # errors
+ {
+ $client->reset_abilities; # now it, as a worker, can't do anything
+ $client->can_do("Worker::Division"); # now it can only do one thing
+
+ my $handle = $client->insert("Worker::Division", { n => 5, d => 0 });
+ ok($handle);
+
+ my $job = Worker::Division->grab_job($client);
+ isa_ok $job, 'TheSchwartz::Job';
+
+ # wrapper around 'work' implemented in the base class which runs work in
+ # eval and notes a failure (with backoff) if job died.
+ Worker::Division->work_safely($job);
+
+ is($handle->failures, 1, "job has failed once");
+ like(join('', $handle->failure_log), qr/Illegal division by zero/, "noted that we divided by zero");
+ }
+
+ teardown_dbs('ts1');
+});
+
+############################################################################
+package Worker::Addition;
+use base 'TheSchwartz::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+
+ # ....
+}
+
+# tell framework to set 'grabbed_until' to time() + 60. because if
+# we can't add some numbers in 30 seconds, our process probably
+# failed and work should be reassigned.
+sub grab_for { 30 }
+
+############################################################################
+package Worker::MergeInternalDict;
+use base 'TheSchwartz::Worker';
+my %internal_dict;
+
+sub reset { %internal_dict = (); }
+
+sub dict { \%internal_dict }
+
+sub work {
+ my ($class, $job) = @_;
+ my $args = $job->arg;
+ %internal_dict = (%internal_dict, %$args);
+ $job->completed;
+}
+
+sub grab_for { 10 }
+
+############################################################################
+package Worker::Division;
+use base 'TheSchwartz::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ my $args = $job->arg;
+
+ my $ans = $args->{n} / $args->{d}; # throw it away, just here to die on d==0
+
+ $job->set_exit_status(1);
+ $job->completed;
+}
+
+sub keep_exit_status_for { 20 } # keep exit status for 20 seconds after on_complete
+
+sub grab_for { 10 }
+
+sub max_retries { 1 }
+
+sub retry_delay { my $class = shift; my $fails = shift; return 2 ** $fails; }
+
diff --git a/t/lib/db-common.pl b/t/lib/db-common.pl
new file mode 100755
index 0000000..60bfdbf
--- /dev/null
+++ b/t/lib/db-common.pl
@@ -0,0 +1,166 @@
+# $Id: db-common.pl 91 2006-08-17 00:39:55Z bradfitz $
+
+use strict;
+use File::Spec;
+use Carp qw(croak);
+
+sub run_tests {
+ my ($n, $code) = @_;
+
+ run_tests_mysql($n, $code);
+ run_tests_sqlite($n, $code);
+}
+
+sub run_tests_innodb {
+ my ($n, $code) = @_;
+ run_tests_mysql($n, $code, 1);
+}
+
+sub run_tests_mysql {
+ my ($n, $code, $innodb) = @_;
+ SKIP: {
+ local $ENV{USE_MYSQL} = 1;
+ my $dbh = eval { mysql_dbh() };
+ skip "MySQL not accessible as root on localhost", $n if $@;
+ skip "InnoDB not available on localhost's MySQL", $n if $innodb && ! has_innodb($dbh);
+ $code->();
+ }
+}
+
+sub run_tests_sqlite {
+ my ($n, $code) = @_;
+
+ # SQLite
+ SKIP: {
+ my $rv = eval "use DBD::SQLite; 1";
+ $rv = 0 if $ENV{SKIP_SQLITE};
+ skip "SQLite not installed", $n if !$rv;
+ $code->();
+ }
+}
+
+sub test_client {
+ my %opts = @_;
+ my $dbs = delete $opts{dbs};
+ my $init = delete $opts{init};
+ my $pfx = delete $opts{dbprefix};
+ croak "'dbs' not an ARRAY" unless ref $dbs eq "ARRAY";
+ croak "unknown opts" if %opts;
+ $init = 1 unless defined $init;
+
+ if ($init) {
+ setup_dbs({ prefix => $pfx }, $dbs);
+ }
+
+ return TheSchwartz->new(databases => [
+ map { {
+ dsn => dsn_for($_),
+ user => "root",
+ pass => "",
+ prefix => $pfx,
+ } } @$dbs
+ ]);
+}
+
+sub has_innodb {
+ my $dbh = shift;
+ my $tmpname = "test_to_see_if_innoavail";
+ $dbh->do("CREATE TABLE IF NOT EXISTS $tmpname (i int) ENGINE=INNODB")
+ or return 0;
+ my @row = $dbh->selectrow_array("SHOW CREATE TABLE $tmpname");
+ my $row = join(' ', @row);
+ my $has_it = ($row =~ /=InnoDB/i);
+ $dbh->do("DROP TABLE $tmpname");
+ return $has_it;
+}
+
+sub schema_file {
+ return "doc/schema.sql" if $ENV{USE_MYSQL};
+ return "t/schema-sqlite.sql";
+}
+
+sub db_filename {
+ my($dbname) = @_;
+ return $dbname . '.db';
+}
+
+sub mysql_dbname {
+ my($dbname) = @_;
+ return 't_sch_' . $dbname;
+}
+
+sub dsn_for {
+ my $dbname = shift;
+ if ($ENV{USE_MYSQL}) {
+ return 'dbi:mysql:' . mysql_dbname($dbname);
+ } else {
+ return 'dbi:SQLite:dbname=' . db_filename($dbname);
+ }
+}
+
+sub setup_dbs {
+ shift if $_[0] =~ /\.sql$/; # skip filenames (old)
+
+ my $opts = ref $_[0] eq "HASH" ? shift : {};
+ my $pfx = delete $opts->{prefix} || "";
+ die "unknown opts" if %$opts;
+
+ my(@dbs) = @_;
+ my $dbs = ref $dbs[0] ? $dbs[0] : \@dbs; # support array or arrayref (old)
+
+ my $schema = schema_file();
+ teardown_dbs(@$dbs);
+ for my $dbname (@$dbs) {
+ if ($ENV{USE_MYSQL}) {
+ create_mysql_db(mysql_dbname($dbname));
+ }
+ my $dbh = DBI->connect(dsn_for($dbname),
+ 'root', '', { RaiseError => 1, PrintError => 0 })
+ or die "Couldn't connect: $!\n";
+ my @sql = load_sql($schema);
+ for my $sql (@sql) {
+ $sql =~ s!^\s*create\s+table\s+(\w+)!CREATE TABLE ${pfx}$1!i;
+ $sql .= " ENGINE=INNODB\n" if $ENV{USE_MYSQL};
+ $dbh->do($sql);
+ }
+ $dbh->disconnect;
+ }
+}
+
+sub mysql_dbh {
+ return DBI->connect("DBI:mysql:mysql", "root", "", { RaiseError => 1 })
+ or die "Couldn't connect to database";
+}
+
+sub create_mysql_db {
+ my $dbname = shift;
+ mysql_dbh()->do("CREATE DATABASE $dbname");
+}
+
+sub drop_mysql_db {
+ my $dbname = shift;
+ mysql_dbh()->do("DROP DATABASE IF EXISTS $dbname");
+}
+
+sub teardown_dbs {
+ my(@dbs) = @_;
+ for my $db (@dbs) {
+ if ($ENV{USE_MYSQL}) {
+ drop_mysql_db(mysql_dbname($db));
+ } else {
+ my $file = db_filename($db);
+ next unless -e $file;
+ unlink $file or die "Can't teardown $db: $!";
+ }
+ }
+}
+
+sub load_sql {
+ my($file) = @_;
+ open my $fh, $file or die "Can't open $file: $!";
+ my $sql = do { local $/; <$fh> };
+ close $fh;
+ split /;\s*/, $sql;
+}
+
+1;
diff --git a/t/parallel-workers.t b/t/parallel-workers.t
new file mode 100644
index 0000000..e8c64e8
--- /dev/null
+++ b/t/parallel-workers.t
@@ -0,0 +1,106 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 2;
+
+# how we keep track of if job was done twice: signal from children back up to us
+my $work_count = 0;
+my $lost_race = 0;
+$SIG{USR1} = sub { $work_count++; };
+$SIG{USR2} = sub { $lost_race ++; };
+
+# tell our parent when we lost a race
+{
+ no warnings 'once';
+ $TheSchwartz::FIND_JOB_BATCH_SIZE = 2;
+
+ $TheSchwartz::T_LOST_RACE = sub {
+ $lost_race = 1; # this one's in our child process.
+ kill 'USR2', getppid();
+ };
+
+ $TheSchwartz::T_AFTER_GRAB_SELECT_BEFORE_UPDATE = sub {
+ # force the race condition to happen, at least until we've triggered it
+ select undef, undef, undef, 0.25
+ unless $lost_race;
+ };
+
+}
+
+# kill children on exit
+my %children; # pid -> 1
+END {
+ my @pids = keys %children;
+ kill -9, @pids if @pids;
+}
+
+my $jobs = 40;
+
+run_tests_innodb(2, sub {
+
+ # get one job into database, to see if children do it twice:
+ {
+ my $client = test_client(dbs => ['ts1']);
+ for (1..$jobs) {
+ $client->insert("Worker::Addition", { numbers => [1, 2] })
+ or die;
+ }
+ }
+
+ # two children to race
+ work();
+ work();
+
+ # hang out waiting for children to init/race/finish
+ #
+ while ($work_count < $jobs) {
+ sleep 1;
+ }
+ my $now = time();
+ while (time < $now + 2) {
+ sleep 1;
+ }
+
+ is($work_count, $jobs, "$jobs jobs done");
+ ok($lost_race, "lost the race at least once");
+ teardown_dbs('ts1');
+});
+
+sub work {
+ # parent:
+ if (my $childpid = fork()) {
+ $children{$childpid} = 1;
+ return;
+ }
+
+ my $client = test_client(dbs => ['ts1'],
+ init => 0);
+
+ # child:
+ while (my $job = Worker::Addition->grab_job($client)) {
+ eval { Worker::Addition->work($job); };
+ }
+ exit 0;
+}
+
+############################################################################
+package Worker::Addition;
+use base 'TheSchwartz::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ kill 'USR1', getppid();
+ $job->completed;
+}
+
+# tell framework to set 'grabbed_until' to time() + 60. because if
+# we can't add some numbers in 30 seconds, our process probably
+# failed and work should be reassigned.
+sub grab_for { 5 }
+
diff --git a/t/pod-coverage.t b/t/pod-coverage.t
new file mode 100644
index 0000000..3e8c91c
--- /dev/null
+++ b/t/pod-coverage.t
@@ -0,0 +1,30 @@
+use strict;
+use Test::More;
+
+eval "use Test::Pod::Coverage 1.08";
+plan skip_all => "Test::Pod::Coverage 1.08 required for testing POD coverage" if $@;
+
+## Eventually we would be able to test coverage for all modules with
+## Test::Pod::all_pod_files(), but let's write the docs first.
+my %modules = (
+ 'TheSchwartz' => {
+ also_private => [
+ map { qr{ \A $_ \z }xms } qw(
+ current_job debug driver_for funcid_to_name funcname_to_id
+ handle_from_string hash_databases insert_job_to_driver
+ is_database_dead mark_database_as_dead reset_abilities
+ restore_full_abilities set_current_job shuffled_databases
+ temporarily_remove_ability
+ )
+ ],
+ },
+ 'TheSchwartz::Worker' => 1,
+ 'TheSchwartz::Job' => 1,
+);
+
+plan tests => scalar keys %modules;
+
+while (my ($module, $params) = each %modules) {
+ pod_coverage_ok($module, ref $params ? $params : ());
+}
+
diff --git a/t/pod.t b/t/pod.t
new file mode 100644
index 0000000..762b750
--- /dev/null
+++ b/t/pod.t
@@ -0,0 +1,6 @@
+use strict;
+use Test::More;
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+
+all_pod_files_ok();
diff --git a/t/replace-with.t b/t/replace-with.t
new file mode 100644
index 0000000..cc5051c
--- /dev/null
+++ b/t/replace-with.t
@@ -0,0 +1,75 @@
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 20;
+
+run_tests(10, sub {
+ my $client = test_client(dbs => ['ts1']);
+
+ my $handle = $client->insert("Worker::Foo", { cluster => 'all'});
+ ok($handle);
+
+ my $job = Worker::Foo->grab_job($client);
+ ok($job, "no addition jobs to be grabbed");
+
+ Worker::Foo->work_safely($job);
+
+ $client->can_do("Worker::Foo");
+ $client->work_until_done; # should process 5 jobs.
+
+ # finish a job by replacing it with nothing
+ $handle = $client->insert("Worker::Foo", { cluster => 'gibberish'});
+ ok($handle->is_pending, "job is still pending");
+ $job = $handle->job;
+ $job->replace_with();
+ ok(! $handle->is_pending, "job no longer pending");
+
+ teardown_dbs('ts1');
+});
+
+############################################################################
+package Worker::Foo;
+use base 'TheSchwartz::Worker';
+
+use Test::More; ## Import test methods.
+
+sub work {
+ my ($class, $job) = @_;
+ my $args = $job->arg;
+
+ if ($args->{cluster} eq "all") {
+ ok(1, "got the expand job");
+ my @jobs;
+ for (1..5) {
+ push @jobs, TheSchwartz::Job->new_from_array("Worker::Foo",
+ { cluster => $_ }
+ );
+ }
+ # which does a $job->completed iff all the @jobs, in one txn, insert
+ # on the same database that $job was on. and it should DIE if the
+ # transaction fails, just so txn flow doesn't proceed on accident.
+ # then work_safely with catch the die and call $job->failed
+ $job->replace_with(@jobs);
+ return;
+ }
+
+ if ($args->{cluster} =~ /^\d+$/) {
+ ok(1, "got job $args->{cluster}");
+ $job->completed;
+ return;
+ }
+
+ # if anything were to fall through the bottom of here without
+ # first calling fail/completed/replace_with, or dying, then the
+ # work_safely wrapper should treat it as a "fall-through" failure
+ # and log it, doing the whole retries/delay thing as with a
+ # regular die.
+}
+
+sub grab_for { 30 }
+
diff --git a/t/retry-delay.t b/t/retry-delay.t
new file mode 100644
index 0000000..23dfbf4
--- /dev/null
+++ b/t/retry-delay.t
@@ -0,0 +1,70 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 16;
+
+run_tests(8, sub {
+ my $client = test_client(dbs => ['ts1']);
+
+ # insert a job which will fail, fail, then succeed.
+ {
+ my $handle = $client->insert("Worker::CompleteEventually");
+ isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";
+
+ $client->can_do("Worker::CompleteEventually");
+ $client->work_until_done;
+
+ is($handle->failures, 1, "job has failed once");
+
+ my $job = Worker::CompleteEventually->grab_job($client);
+ ok(!$job, "a job isn't ready yet"); # hasn't been two seconds
+ sleep 3; # 2 seconds plus 1 buffer second
+
+ $job = Worker::CompleteEventually->grab_job($client);
+ ok($job, "got a job, since time has gone by");
+
+ Worker::CompleteEventually->work_safely($job);
+ is($handle->failures, 2, "job has failed twice");
+
+ $job = Worker::CompleteEventually->grab_job($client);
+ ok($job, "got the job back");
+
+ Worker::CompleteEventually->work_safely($job);
+ ok(! $handle->is_pending, "job has exited");
+ is($handle->exit_status, 0, "job succeeded");
+ }
+
+ teardown_dbs('ts1');
+});
+
+############################################################################
+package Worker::CompleteEventually;
+use base 'TheSchwartz::Worker';
+
+sub work {
+ my ($class, $job) = @_;
+ my $failures = $job->failures;
+ if ($failures < 2) {
+ $job->failed;
+ } else {
+ $job->completed;
+ }
+ return;
+}
+
+sub keep_exit_status_for { 20 } # keep exit status for 20 seconds after on_complete
+
+sub max_retries { 2 }
+
+sub retry_delay {
+ my $class = shift;
+ my $fails = shift;
+ return [undef,2,0]->[$fails]; # fails 2 seconds first time, then immediately
+}
+
diff --git a/t/schema-sqlite.sql b/t/schema-sqlite.sql
new file mode 100644
index 0000000..99bf875
--- /dev/null
+++ b/t/schema-sqlite.sql
@@ -0,0 +1,33 @@
+CREATE TABLE funcmap (
+ funcid INTEGER PRIMARY KEY AUTOINCREMENT,
+ funcname VARCHAR(255) NOT NULL,
+ UNIQUE(funcname)
+);
+
+CREATE TABLE job (
+ jobid INTEGER PRIMARY KEY AUTOINCREMENT,
+ funcid INTEGER UNSIGNED NOT NULL,
+ arg MEDIUMBLOB,
+ uniqkey VARCHAR(255) NULL,
+ insert_time INTEGER UNSIGNED,
+ run_after INTEGER UNSIGNED NOT NULL,
+ grabbed_until INTEGER UNSIGNED NOT NULL,
+ priority SMALLINT UNSIGNED,
+ coalesce VARCHAR(255),
+ UNIQUE(funcid,uniqkey)
+);
+
+CREATE TABLE error (
+ error_time INTEGER UNSIGNED NOT NULL,
+ jobid INTEGER NOT NULL,
+ message VARCHAR(255) NOT NULL,
+ funcid INT UNSIGNED NOT NULL DEFAULT 0
+);
+
+CREATE TABLE exitstatus (
+ jobid INTEGER PRIMARY KEY NOT NULL,
+ funcid INT UNSIGNED NOT NULL DEFAULT 0,
+ status SMALLINT UNSIGNED,
+ completion_time INTEGER UNSIGNED,
+ delete_after INTEGER UNSIGNED
+);
diff --git a/t/unique.t b/t/unique.t
new file mode 100644
index 0000000..b605d38
--- /dev/null
+++ b/t/unique.t
@@ -0,0 +1,46 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 12;
+
+#use Data::ObjectDriver;
+#$Data::ObjectDriver::DEBUG = 1;
+
+run_tests(6, sub {
+ my $client = test_client(dbs => ['ts1']);
+ my ($job, $handle);
+
+ # insert a job with unique
+ $job = TheSchwartz::Job->new(
+ funcname => 'feed',
+ uniqkey => "major",
+ );
+ ok($job, "made first feed major job");
+ $handle = $client->insert($job);
+ isa_ok $handle, 'TheSchwartz::JobHandle';
+
+ # insert again (notably to same db) and see it fails
+ $job = TheSchwartz::Job->new(
+ funcname => 'feed',
+ uniqkey => "major",
+ );
+ ok($job, "made another feed major job");
+ $handle = $client->insert($job);
+ ok(! $handle, 'no handle');
+
+ # insert same uniqkey, but different func
+ $job = TheSchwartz::Job->new(
+ funcname => 'scratch',
+ uniqkey => "major",
+ );
+ ok($job, "made scratch major job");
+ $handle = $client->insert($job);
+ isa_ok $handle, 'TheSchwartz::JobHandle';
+
+});
diff --git a/t/work-before-funcids-exist.t b/t/work-before-funcids-exist.t
new file mode 100644
index 0000000..6bea2ad
--- /dev/null
+++ b/t/work-before-funcids-exist.t
@@ -0,0 +1,59 @@
+# $Id$
+# -*-perl-*-
+
+use strict;
+use warnings;
+
+require 't/lib/db-common.pl';
+
+use TheSchwartz;
+use Test::More tests => 4;
+
+run_tests(2, sub {
+ my $client = test_client(dbs => ['ts1']);
+
+ my $handle = $client->insert("Worker::Dummy");
+ ok($handle, "inserted job");
+
+ $client->can_do("Worker::Dummy");
+ $client->can_do("Worker::Dummy2");
+ $client->can_do("Worker::Dummy3");
+ $client->work_until_done;
+
+ ok(! $handle->is_pending, "job is done");
+});
+
+
+
+############################################################################
+package Worker::Dummy;
+use base 'TheSchwartz::Worker';
+sub work {
+ my ($class, $job) = @_;
+ my $subjob = TheSchwartz::Job->new(
+ funcname => 'Worker::Dummy2',
+ );
+ $job->replace_with($subjob);
+}
+
+sub max_retries { 2 }
+sub retry_delay { 5 }
+
+
+
+package Worker::Dummy2;
+use base 'TheSchwartz::Worker';
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+}
+
+package Worker::Dummy3;
+use base 'TheSchwartz::Worker';
+sub work {
+ my ($class, $job) = @_;
+ $job->completed;
+}
+
+
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libtheschwartz-perl.git
More information about the Pkg-perl-cvs-commits
mailing list