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

BEGIN {
	$ENV{NAME} ||= $1 if $0 =~ m{([^/]+)$};
	die "no SLICE in enviroment" unless $ENV{SLICE};
}

our $log = "$ENV{SLICE}/log/$ENV{NAME}.log",

my $driver = Gearman::Driver->new(
	namespaces => [qw(CloudStore::Gearman)],
	configfile => 'config.yaml',
	loglevel   => 'DEBUG',
	logfile    => $log,
	server     => $ENV{GEARMAN} || 'localhost:4730',
	interval   => 3,
	job_runtime_attributes => {
		'CloudStore::Gearman::API::create_user' => {
			max_processes => 1,
			min_processes => 1,
		},
	},
);

open(my $pid, '>', "/tmp/$ENV{NAME}.pid");
print $pid "$$\n";
close $pid;

warn localtime() . " STARTED $ENV{NAME} pid $$ log $log\n";
$driver->run;
