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) {
	my ( $self, $job, $workload ) = @_;
	warn "# ping $job $workload\n";
	my ( $ok, $t, $ip ) = $p->ping( $workload );
	warn "# $ok $t $ip\n";
	return $t;
}

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 ) = @_;
	return JSON::XS::encode_json($result);
}

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

1;
