simple perl command-line client depenent only on libgearman-client-perl which is...
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 1 May 2010 12:49:20 +0000 (12:49 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 1 May 2010 12:49:20 +0000 (12:49 +0000)
git-svn-id: svn://svn.rot13.org/sysadmin-cookbook@203 191e9f34-6774-4a6d-acfc-7664dacd4a2a

recepies/gearman/gearman.pl [new file with mode: 0755]

diff --git a/recepies/gearman/gearman.pl b/recepies/gearman/gearman.pl
new file mode 100755 (executable)
index 0000000..525fc2a
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+use Data::Dump qw(dump);
+use Getopt::Long;
+use Gearman::Worker;
+
+my $host = '10.60.0.244:4730';
+my $function = 'test';
+
+GetOptions(
+       'host' => \$host,
+       'function' => \$function,
+) || die $!;
+
+
+my $worker = Gearman::Worker->new;
+$worker->job_servers( $host );
+$worker->register_function( $function => sub {
+       my $job = $_[0];
+       my $arg = $_[0]->arg;
+       warn "# job ",dump($job);
+});
+warn "# worker ",dump($worker);
+$worker->work while 1;
+
+=for client
+
+my $client = Gearman::Client->new;
+$client->job_servers($host);
+
+# running a single task
+my $result_ref = $client->do_task("add", "1+2");
+print "1 + 2 = $$result_ref\n";
+
+# waiting on a set of tasks in parallel
+my $taskset = $client->new_task_set;
+$taskset->add_task( "add" => "1+2", {
+       on_complete => sub { ... }
+});
+
+$taskset->add_task( "divide" => "5/0", {
+       on_fail => sub { print "divide by zero error!\n"; },
+});
+$taskset->wait;
+
+=cut