package APKPM;

use base qw(Gearman::Driver::Worker);
use Moose;
use Time::HiRes;
use JSON::XS;
use Data::Dump qw(dump);
use Net::Ping;

our $VERSION = '0.00';

sub prefix { '' }

sub process_name {
	my ( $self, $orig, $job_name ) = @_;
	warn "# process_name $orig $job_name\n";
	return "$orig ($job_name)";
}

my $p = Net::Ping->new;
$p->hires;

sub ping : Job : MinProcesses(1) : MaxProcesses(5) : Encode {
	my ( $self, $job, $workload ) = @_;
	warn "# ping $job $workload\n";
	my ( $ok, $rtt, $ip ) = $p->ping( $workload );
	return { error => $workload } unless $ok;
	warn "# ping $ip $rtt $ok";
	return { ip => $ip, rtt => $rtt };
}

sub inc_foo : Job : MinProcesses(1) : MaxProcesses(5) : Encode : Decode {
	my ( $self, $job, $workload ) = @_;
	warn "# inc_foo $job ", dump($workload);
	$workload->{foo}++;
	return $workload;
}

sub encode {
	my ( $self, $result ) = @_;
	warn "# encode ",dump $result;
	return JSON::XS::encode_json($result);
}

sub decode {
	my ( $self, $workload ) = @_;
	warn "# decode ", dump $workload;
	return JSON::XS::decode_json($workload);
}

1;
