Tutorial 2: Dive into Foorum: TheSchwartz

TheSchwartz introduction

TheSchwartz

As CPAN perldoc says: reliable job queue. Basically it splitted into two parts, one is worker while the other is client.

Worker script:

We may have several workers to deal with different tasks. For example TheSchwartz_worker.pl deal with some tasks while TheSchwartz_worker_scraper.pl deal with scraper task.

What's worker for? In my idea, that's just a script to monitor a table in database (check if there is any task to do) every 5 secs. Faked code like this:

while (1) {
    # check if there is any new TODO task in one table of theschwartz database.
    if ($has_new_task) { # scalar @new_tasks
        foreach my $new_task (@new_tasks) {
            $new_task->worker();
        }
    }
    sleep 5;

Where does new task from? Foorum has two ways, always one is form Foorum/Controller|Model, the other is from cron scripts.

example code:

  1. in Foorum/Model
use Foorum::ExternalUtils qw/theschwartz/;
my $client = theschwartz();
$client->insert(
    'Foorum::TheSchwartz::Worker::WorkerExample',
    @args
);
  1. in cron script. http://foorum.googlecode.com/svn/trunk/bin/cron/TheSchwartz_client.pl

That's a simple introduction. Maybe I'm wrong here. :)

Worker in Foorum

For now, we have several workers:

There is a simple RULES that why we use TheSchwartz, put heavy code on backend script instead of httpd.

How to write a Worker in Foorum?

OK, please borrow code from exist ones.

SEE ALSO

Tutorial1, Tutorial3, Tutorial4, Tutorial5

WHERE TO GO NEXT