#!/usr/bin/env perl
use strict;
use warnings;
use Gearman::Driver;
use lib 'lib';

my $dir = $0;
$dir =~ s{/[^/]+$}{};
chdir $dir || die "can't chdir into $dir";

$SIG{__WARN__} = sub { print STDERR @_ if $ENV{DEBUG} };

my $driver = Gearman::Driver->new(
	namespaces => [qw(APKPM)],
	configfile => 'config.yaml',
	loglevel   => 'DEBUG',
	logfile    => 'log/gearman.log',
	server     => $ENV{GEARMAN} || 'localhost:4730',
	interval   => 3,
	job_runtime_attributes => {
		'APKPM::Poll::poll_prefix' => {
			max_processes => 1,
			min_processes => 0,
		},
		'APKPM::Poll::poll_ip_username' => {
			max_processes => 40,
			min_processes => 0,
		},
		'APKPM::CPE::poll' => {
			max_processes => 1,
			min_processes => 0,
		},
		'APKPM::CPE::ping' => {
			max_processes => 40,
			min_processes => 0,
		},
		'APKPM::Ping::ping' => {
			max_processes => 1,
			min_processes => 0,
		},
		'APKPM::Davolink::info' => {
			max_processes => 30,
			min_processes => 0,
		},
		'APKPM::EasyGateway::info' => {
			max_processes => 5,
			min_processes => 0,
		},
		'APKPM::Store::insert' => {
			max_processes => 2,
			min_processes => 0,
		},

		'APKPM::DSLAM::search' => {
			max_processes => 10,
			min_processes => 0,
		},
		'APKPM::CRM::search' => {
			max_processes => 3,
			min_processes => 0,
		},
	},
);

open(my $pid, '>', '/tmp/apkpm.driver.pid');
print $pid "$$\n";
close $pid;

$driver->run;
