package APKPM;

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

our $VERSION = '0.00';

with 'APKPM::Gearman';
with 'APKPM::Gearman::Client';

sub prefix { '' }

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

sub poll : Job : MinProcesses(1) : MaxProcesses(5) : Encode(e_json) {
	my ( $self, $job, $workload ) = @_;
	warn "# poll $job ", dump($workload);

	my $res = { workload => $workload };

	my $taskset = $self->gc->new_task_set;
	$taskset->add_task('LDAP_search', $workload, {
		on_complete => sub { $res->{ldap} = $self->d_json( ${$_[0]} ) }
	});
	$taskset->add_task('CRM_username', $workload, {
		on_complete => sub { $res->{crm} = $self->d_json( ${$_[0]} ) }
	});
	warn "# wait";
	$taskset->wait;
	return $res;
}

1;
