invoke poll_ZTEDSLAM and poll_ZTEMSAN for presistant workers
[APKPM.git] / lib / APKPM / Store.pm
1 package APKPM::Store;
2
3 use base qw(Gearman::Driver::Worker);
4 use Moose;
5 use Time::HiRes;
6 use Data::Dump qw(dump);
7 use DBD::Pg;
8
9 with 'APKPM::Gearman';
10
11 sub prefix { 'Store_' }
12
13 sub process_name {
14         my ( $self, $orig, $job_name ) = @_;
15         warn "# process_name $orig $job_name\n";
16         return "$orig ($job_name)";
17 }
18
19 sub dbh {
20         DBI->connect_cached('DBI:Pg:dbname=apkpm','','', {
21                 RaiseError => 1,
22                 AutoCommit => 1,
23         });
24 }
25
26 sub pg_insert {
27         my ( $self, $table, $h ) = @_;
28
29
30         my @c = keys %$h;
31
32         my $sql = "INSERT INTO $table (" . join(',',@c) . ') values (' . join(',', map { '?' } 0 .. $#c) . ')';
33         warn $sql;
34         my $sth = $self->dbh->prepare($sql);
35         $sth->execute( map { $h->{$_} } @c );
36 }
37
38 sub insert : Job : Decode(d_json) {
39         my ( $self, $job, $workload ) = @_;
40         my $table = delete $workload->{_table} || die "no _table";
41         $self->pg_insert($table => $workload);
42 }
43
44 sub sql : Job : Encode(e_json) {
45         my ( $self, $job, $workload ) = @_;
46
47         my $sth = $self->dbh->prepare($workload);
48         my $rows = eval { $sth->execute };
49         return { error => $@ } if $@;
50
51         warn "# $rows rows get_username_table $workload\n";
52
53         return {
54                 columns => $sth->{NAME},
55                 rows => $sth->fetchall_arrayref,
56         };
57 }
58
59 1;