#!/usr/bin/perl
use warnings;
use strict;

use Data::Dump qw(dump);
use DBI;
use JSON::XS;

my $log = sub {
	my $job = shift;

	my $json = decode_json $job->arg;
	warn "## log ",dump( $json );

	my $dbh = DBI->connect_cached('DBI:Pg:dbname=cloudstore','','', {
		RaiseError => 1,
		AutoCommit => 1,
	});

	my @k = keys %$json;

	my $sql = sprintf 'INSERT INTO log (%s) values (%s)',
		join(',',@k), join(',', map { '?' } @k);
	warn "# $sql";
	my $sth = $dbh->prepare($sql);
	$sth->execute( map { $json->{$_} } @k );
};

use Gearman::Worker;
my $worker = Gearman::Worker->new;
$worker->job_servers('127.0.0.1:4730');
$worker->register_function( log => $log );

warn "$0 pid $$ waitng for jobs\n";
$worker->work while 1;

