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

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

sub insert {
	my ($table,$job) = @_;

	my $hash = decode_json $job->arg;

	print "insert $table ",dump($hash),$/;

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

	my @k = keys %$hash;

	my $sql = sprintf 'INSERT INTO %s (%s) values (%s)', $table,
		join(',',@k), join(',', map { '?' } @k);

	warn "# $sql";
	my $sth = $dbh->prepare($sql);
	$sth->execute( map { $hash->{$_} } @k );
};

use Gearman::Worker;
my $worker = Gearman::Worker->new;
$worker->job_servers('127.0.0.1:4730');
$worker->register_function( rsync_transfer => sub { insert 'rsync_transfer' => shift } );

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

