ECHO_REQ SUBMIT_JOB GRAB_JOB runs ioloop
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 1 Mar 2011 22:58:59 +0000 (22:58 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 1 Mar 2011 22:58:59 +0000 (22:58 +0000)
This allow us to start first worker, and then client and terminate
worker after first batch of jobs

lib/MojoX/Gearman.pm
t/client.t
t/worker.t

index 572af92..fb3a398 100644 (file)
@@ -139,13 +139,13 @@ warn "# WORK_COMPLETE ",dump $data;
                        };
                } elsif ( $type == $packet_type->{NO_JOB} ) {
                        $self->req( 'PRE_SLEEP' );
+                       $self->stop;
                } elsif ( $type == $packet_type->{JOB_ASSIGN} ) {
                        my ( $handle, $function, $workload ) = @data;
                        my $callback = $self->{_function}->{$function};
                        die "no $function callback" unless ref $callback eq 'CODE';
-                       warn "# calling $data callback $callback";
                        my $out = $callback->( $workload );
-                       warn "# === ",dump $out;
+                       warn "## $data $callback = ", dump $out;
                        $self->req( 'WORK_COMPLETE', $handle, $out );
                        $self->req( 'GRAB_JOB' );
                } elsif ( $type == $packet_type->{NOOP} ) {
@@ -175,12 +175,11 @@ warn "# WORK_COMPLETE ",dump $data;
 
        if ( $type eq 'CAN_DO' ) {
                $self->{_function}->{$data} = $callback;
+               $self->res( $callback );
                warn "# installed $data callback $callback";
-               $self->req( 'GRAB_JOB' );
+       } elsif ( $type =~ m/^(ECHO_REQ|SUBMIT_JOB|GRAB_JOB)$/ ) { # sync commands
+               $self->start;
        }
-               
-
-#      $self->start;
 
        $self->res;
 }
index 4df77c5..3edd922 100755 (executable)
@@ -2,7 +2,7 @@
 use warnings;
 use strict;
 
-use Test::More tests => 7;
+use Test::More tests => 8;
 use Data::Dump qw(dump);
 use lib 'lib';
 
@@ -19,3 +19,5 @@ diag dump $ping;
 
 ok( $g->req( 'ECHO_REQ', "alive" ), 'ECHO - still alive - still alive?' );
 
+cmp_ok( $g->req( 'SUBMIT_JOB', 'mojo_g', '', 42 ), '==', 43, 'mojo_g' );
+
index dc06527..06e8323 100755 (executable)
@@ -2,7 +2,7 @@
 use warnings;
 use strict;
 
-use Test::More tests => 7;
+use Test::More tests => 5;
 use Data::Dump qw(dump);
 use lib 'lib';
 
@@ -11,16 +11,13 @@ use_ok 'MojoX::Gearman';
 my $g = new_ok 'MojoX::Gearman';
 
 my $name = "mojo_g";
-
 ok( my $echo = $g->req( 'CAN_DO', $name, sub {
        my $payload = shift;
        warn "DO $name ", dump($payload), $/;
        return $payload + 1;
-}), 'CAN_DO' );
-diag $echo;
-
-$g->start;
+}), "CAN_DO $name" );
 
-diag "press <enter> to finish";
-<STDIN>;
+diag "start loop";
+ok( $g->req( 'GRAB_JOB' ), 'GRAB_JOB' );
+ok( $g->start, 'start' );