X-Git-Url: http://git.rot13.org/?p=angular-mojolicious.git;a=blobdiff_plain;f=couchdb-trigger.pl;h=db5acb0955e6a0501ccf1a79c7c2bf2c3a65b0bf;hp=d39f0079e6e26b4aa2736a69788220b7116feedd;hb=e05cae6cdb7a0c9a792a2d9b2e688e62748bfbb3;hpb=d44d1dd771e6c733ee519b6ee7fae239e5b17566 diff --git a/couchdb-trigger.pl b/couchdb-trigger.pl index d39f007..db5acb0 100755 --- a/couchdb-trigger.pl +++ b/couchdb-trigger.pl @@ -18,21 +18,15 @@ use lib 'common/mojo/lib'; use Mojo::Client; use Mojo::JSON; use Time::HiRes qw(time); +use Data::Dump qw(dump); -my $url = 'http://localhost:5984/monitor'; +my ( $url, $trigger_path ) = @ARGV; -sub _trigger { - my $trigger = $_[0]->{trigger}; - if ( my $command = $trigger->{command} ) { - # FIXME SECURITY HOLE - my $output = $trigger->{output} = `$command`; +$url ||= 'http://localhost:5984/monitor'; +$trigger_path ||= 'trigger/shell.pm' ; - $trigger->{output} = - [ map { [ split (/\s+/,$_) ] } split(/\n/,$output) ] - if $trigger->{format} =~ m/table/i; - } - return $trigger; -} +sub commit { warn "# commit ignored\n"; } +require $trigger_path if -e $trigger_path; my $seq = 0; @@ -52,12 +46,9 @@ while( ! $error ) { $tx->res->body(sub{ my ( $content, $body ) = @_; - debug 'BODY' => $body; + return if length($body) == 0; # empty chunk, heartbeat? - if ( length($body) == 0 ) { - warn "# empty chunk, heartbeat?\n"; - return; - } + debug 'BODY' => $body; foreach ( split(/\r?\n/, $body) ) { # we can get multiple documents in one chunk @@ -77,11 +68,11 @@ while( ! $error ) { debug 'change' => $change; - if ( my $trigger = $change->{doc}->{trigger} ) { - if ( exists $trigger->{active} ) { + if ( filter($change) ) { + if ( exists $change->{doc}->{trigger}->{active} ) { debug 'trigger.active', $change->{doc}->{trigger}->{active}; } else { - $trigger->{active} = [ time() ]; + $change->{doc}->{trigger}->{active} = [ time() ]; debug 'TRIGGER start PUT ', $change->{doc}; $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub { @@ -90,21 +81,21 @@ while( ! $error ) { if ( $tx->res->code == 409 ) { info "TRIGGER ABORTED started on another worker? ", $tx->error; } else { - info "ERROR ", $tx->error; + info "ERROR $url/$id ", $tx->error; } } else { my $res = $tx->res->json; $change->{doc}->{_rev} = $res->{rev}; debug "TRIGGER execute ", $change->{doc}; - _trigger( $change->{doc} ); + trigger( $change ); - push @{ $trigger->{active} }, time(), 0; # last timestamp + push @{ $change->{doc}->{trigger}->{active} }, time(), 0; # last timestamp $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub { my ($client,$tx) = @_; if ($tx->error) { - info "ERROR", $tx->error; + info "ERROR $url/$id", $tx->error; } else { my $res = $tx->res->json; $change->{doc}->{_rev} = $res->{rev}; @@ -121,6 +112,8 @@ while( ! $error ) { } + commit; + }); $client->start($tx);