package APKPM::Ping;

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

with 'APKPM::Gearman';

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(50) : Encode(e_json) {
	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 };
}

1;
