This commit is contained in:
2019-02-06 00:49:12 +03:00
commit 8dbb1bb605
4796 changed files with 506072 additions and 0 deletions

77
wcmtools/gearman/dev/DMap.pm Executable file
View File

@@ -0,0 +1,77 @@
#!/usr/bin/perl
package DMap;
use strict;
use Exporter;
use Storable;
use IO::Socket::INET;
use Gearman::Util;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(dmap);
$Storable::Deparse = 1;
$Storable::Eval = 1;
our @js;
sub set_job_servers {
@js = @_;
}
sub dmap (&@) {
my $code = shift;
my $fz = Storable::freeze($code);
my $sock;
foreach (@js) {
$_ .= ":7003" unless /:/;
$sock = IO::Socket::INET->new(PeerAddr => $js[0]);
last if $sock;
}
die "No jobserver available" unless $sock;
my $send = sub {
print $sock Gearman::Util::pack_req_command(@_);
};
my $err;
my $get = sub {
return Gearman::Util::read_res_packet($sock, \$err);;
};
my $argc = scalar @_;
ARG:
foreach (@_) {
$send->("submit_job", join("\0", "dmap", "", Storable::freeze([ $code, $_ ])));
}
my $waiting = $argc;
my %handle; # n -> handle
my $hct = 0;
my %partial_res;
while ($waiting) {
my $res = $get->()
or die "Failure: $err";
if ($res->{type} eq "job_created") {
$handle{$hct} = ${$res->{blobref}};
$hct++;
next;
}
if ($res->{type} eq "work_complete") {
my $br = $res->{blobref};
$$br =~ s/^(.+?)\0//;
my $handle = $1;
$partial_res{$handle} = Storable::thaw($$br);
$waiting--;
}
}
return map { @{ $partial_res{$handle{$_}} } } (0..$argc-1);
}
1;

40
wcmtools/gearman/dev/client.pl Executable file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/perl
use strict;
use Gearman::Util;
use IO::Socket::INET;
use Data::Dumper;
my $sock = IO::Socket::INET->new(PeerAddr => "localhost:7003")
or die "no socket.";
my $send = sub {
print $sock Gearman::Util::pack_req_command(@_);
};
my $err;
my $get = sub {
return Gearman::Util::read_res_packet($sock, \$err);;
};
#$send->("submit_job_bg", join("\0", "add", "", "5,3"));
$send->("get_status", "FOO");
my $res = $get->() or die "no handle";
die "not a status_res packet" unless $res->{type} eq "status_res";
while (1) {
$send->("submit_job", join("\0", "add", "-", "5,3"));
$res = $get->() or die "no handle";
print Dumper($res);
die "not a job_created res" unless $res->{type} eq "job_created";
while ($res = $get->()) {
print "New packet: " . Dumper($res);
}
print "Error: $err\n";
exit 0;
}

View File

@@ -0,0 +1,76 @@
#!/usr/bin/perl
use strict;
use Gearman::Util;
use IO::Socket::INET;
use Data::Dumper;
use Storable;
$Storable::Eval = 1;
my $server = shift;
$server ||= "localhost";
my $sock = IO::Socket::INET->new(PeerAddr => "$server:7003")
or die "no socket.";
my $send = sub {
print $sock Gearman::Util::pack_req_command(@_);
};
my $err;
my $get = sub {
my $res;
while (1) {
$res = Gearman::Util::read_res_packet($sock, \$err);
return undef unless $res;
return $res unless $res->{type} eq "noop";
}
};
$send->("can_do", "dmap");
while (1) {
$send->("grab_job");
my $res = $get->();
die "ERROR: $err\n" unless $res;
print " res.type = $res->{type}\n";
if ($res->{type} eq "error") {
print "ERROR: " . Dumper($res);
exit 0;
}
if ($res->{type} eq "no_job") {
$send->("pre_sleep");
print "Sleeping.\n";
my $rin;
vec($rin, fileno($sock), 1) = 1;
my $nfound = select($rin, undef, undef, 2.0);
print " select returned = $nfound\n";
next;
}
if ($res->{type} eq "job_assign") {
my $ar = $res->{blobref};
die "uh, bogus res" unless
$$ar =~ s/^(.+?)\0(.+?)\0//;
my ($handle, $func) = ($1, $2);
print "GOT JOB: $handle -- $func\n";
if ($func eq "dmap") {
my $rq = Storable::thaw($$ar);
my $code = $rq->[0];
my @val = map { &$code; } $rq->[1];
print "VALS: [@val]\n";
$send->("work_complete", join("\0", $handle, Storable::freeze(\@val)));
}
next;
}
print "RES: ", Dumper($res);
}

13
wcmtools/gearman/dev/dmap.pl Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/perl
use strict;
use DMap;
DMap::set_job_servers("localhost", "sammy", "kenny");
my @foo = dmap { "$_ = " . `hostname` } (1..10);
print "dmap says:\n @foo";

View File

@@ -0,0 +1,23 @@
#!/usr/bin/perl
use strict;
use Gearman::Util;
use IO::Socket::INET;
my $sock = IO::Socket::INET->new(PeerAddr => "localhost:7003")
or die "no socket.";
print $sock "gibberish_cmd\r\n";
my $res = <$sock>;
die "bogus response" unless $res =~ /^ERR unknown_command /;
my $cmd;
my $echo_val = "The time is " . time() . " \r\n and a null\0 is fun.";
print $sock Gearman::Util::pack_req_command("echo_req", $echo_val);
my $err;
my $res = Gearman::Util::read_res_packet($sock, \$err);
use Data::Dumper;
print "ERROR: $err\n";
print Dumper($res);

76
wcmtools/gearman/dev/worker.pl Executable file
View File

@@ -0,0 +1,76 @@
#!/usr/bin/perl
use strict;
use Gearman::Util;
use IO::Socket::INET;
use Data::Dumper;
my $sock = IO::Socket::INET->new(PeerAddr => "localhost:7003")
or die "no socket.";
my $send = sub {
print $sock Gearman::Util::pack_req_command(@_);
};
my $err;
my $get = sub {
my $res;
while (1) {
$res = Gearman::Util::read_res_packet($sock, \$err);
return undef unless $res;
return $res unless $res->{type} eq "noop";
}
};
$send->("can_do", "frob");
$send->("cant_do", "frob");
$send->("can_do", "bar");
$send->("reset_abilities");
$send->("can_do", "add");
while (1) {
$send->("grab_job");
my $res = $get->();
die "ERROR: $err\n" unless $res;
print " res.type = $res->{type}\n";
if ($res->{type} eq "error") {
print "ERROR: " . Dumper($res);
exit 0;
}
if ($res->{type} eq "no_job") {
$send->("pre_sleep");
print "Sleeping.\n";
my $rin;
vec($rin, fileno($sock), 1) = 1;
my $nfound = select($rin, undef, undef, 2.0);
print " select returned = $nfound\n";
next;
}
if ($res->{type} eq "job_assign") {
my $ar = $res->{blobref};
die "uh, bogus res" unless
$$ar =~ s/^(.+)\0(.+)\0//;
my ($handle, $func) = ($1, $2);
print " GOT: handle=$handle, func=$func, args=($$ar)\n";
if ($func eq "add") {
for (1..10) {
$send->("work_status", join("\0", $handle, $_, 10));
select undef, undef, undef, 0.5;
}
my ($n1, $n2) = split(/,/, $$ar);
$send->("work_complete", join("\0", $handle, $n1+$n2));
}
next;
}
print "RES: ", Dumper($res);
}