MooseX::TheSchwartz - TheSchwartz based on Moose!


    use MooseX::TheSchwartz;

    my $client = MooseX::TheSchwartz->new();
    # rest are the same as TheSchwartz
    # in some place we insert job into MooseX::TheSchwartz
    # in another place we run this job
    # 1, insert job in cgi/Catalyst
    use MooseX::TheSchwartz;
    my $client = MooseX::TheSchwartz->new();
    $client->insert('My::Worker::A', { args1 => 1, args2 => 2 } );
    # 2, defined the heavy things in My::Worker::A
    package My::Worker::A;
    use base 'MooseX::TheSchwartz::Worker';
    sub work {
        my ($class, $job) = @_;
        # $job is an instance of MooseX::TheSchwartz::Job
        my $args = $job->args;
        # do heavy things like resize photos, add 1 to 2 etc.
    # 3, run the worker in a non-stop script
    use MooseX::TheSchwartz;
    my $client = MooseX::TheSchwartz->new();


TheSchwartz is a powerful job queue. This module is a Moose implemention.

read more on TheSchwartz


  • databases

    An arrayref of dbh.

        my $dbh1 = DBI->conncet(@dbi_info);
        my $dbh2 = $schema->storage->dbh;
        my $client = MooseX::TheSchwartz->new( databases => [ $dbh1, $dbh2 ] );
        # or
        my $client = MooseX::TheSchwartz->new();
        $client->databases( [ $dbh1, $dbh2 ] );
  • verbose

    control the debug.

        my $client = MooseX::TheSchwartz->new( verbose => 1 );
        # or
        my $client = MooseX::TheSchwartz->new();
        $client->verbose( 1 );
        $client->verbose( sub {
            my $msg = shift;
            print STDERR "[INFO] $msg\n";
        } );
  • scoreboard

    save job info to file. by default, the file will be saved at $tmpdir/theschwartz/scoreboard.$$

        my $client = MooseX::TheSchwartz->new( scoreboard => 1 );
        # or
        my $client = MooseX::TheSchwartz->new();
        # be sure the file is there
        $client->scoreboard( "/home/fayland/theschwartz/scoreboard.log" );


The methods of TheSchwartz clients used by applications posting jobs to the queue are:

$client->insert( $job )

Adds the given TheSchwartz::Job to one of the client's job databases.

$client->insert( $funcname, $arg )

Adds a new job with funcname $funcname and arguments $arg to the queue.


The methods of TheSchwartz clients for use in worker processes are:

$client->can_do( $ability )

Adds $ability to the list of abilities $client is capable of performing. Subsequent calls to that client's work methods will find jobs requiring the given ability.


Find and perform one job $client can do.


Find and perform jobs $client can do until no more such jobs are found in any of the client's job databases.

$client->work( [$delay] )

Find and perform any jobs $client can do, forever. When no job is available, the working process will sleep for $delay seconds (or 5, if not specified) before looking again.

$client->find_job_for_workers( [$abilities] )

Returns a TheSchwartz::Job for a random job that the client can do. If specified, the job returned matches one of the abilities in the arrayref $abilities, rather than $client's abilities.

$client->find_job_with_coalescing_value( $ability, $coval )

Returns a TheSchwartz::Job for a random job for a worker capable of $ability and with a coalescing value of $coval.

$client->find_job_with_coalescing_prefix( $ability, $coval )

Returns a TheSchwartz::Job for a random job for a worker capable of $ability and with a coalescing value beginning with $coval.

Note the TheSchwartz implementation of this function uses a LIKE query to find matching jobs, with all the attendant performance implications for your job databases.


Fayland Lam, <fayland at>


Copyright 2008 Fayland Lam, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.