3 # back-end trigger server for CouchDB monitoring changes feed:
5 # http://wiki.apache.org/couchdb/HTTP_database_API#Changes
7 # implements state machine using document which you cen put with:
9 # curl -X PUT http://localhost:5984/monitor/df -d '{"trigger":{"command":"df -P","format":"table"}}'
11 # DEFAULT TRIGGER EXECUTE SHELL COMMANDS. IT IS NOT SECURE IF YOUR COUCHDB ISN'T SECURE!
16 use lib 'common/mojo/lib';
20 use Time::HiRes qw(time);
21 use Data::Dump qw(dump);
23 my ( $url, $trigger_path ) = @ARGV;
25 $url ||= 'http://localhost:5984/monitor';
26 $trigger_path ||= 'trigger/shell.pm' ;
28 sub commit { warn "# commit ignored\n"; }
29 require $trigger_path if -e $trigger_path;
33 my $client = Mojo::Client->new;
34 our $json = Mojo::JSON->new;
35 sub info { warn $_[0], " ",$json->encode($_[1]),$/ }
36 sub debug { info "# $_[0]", $_[1] }
39 $client->keep_alive_timeout(90); # couchdb timeout is 60s
43 my $changes_feed = "$url/_changes?feed=continuous;include_docs=true;since=$seq";
44 info 'GET' => $changes_feed;
45 my $tx = $client->build_tx( GET => $changes_feed );
47 my ( $content, $body ) = @_;
49 return if length($body) == 0; # empty chunk, heartbeat?
51 debug 'BODY' => $body;
53 foreach ( split(/\r?\n/, $body) ) { # we can get multiple documents in one chunk
55 my $change = $json->decode($_);
57 if ( exists $change->{error} ) {
59 } elsif ( exists $change->{last_seq} ) {
60 $seq = $change->{last_seq};
61 } elsif ( $change->{seq} <= $seq ) {
62 info "ERROR: stale" => $change;
63 } elsif ( exists $change->{changes} ) {
65 my $id = $change->{id} || warn "no id?";
66 my $rev = $change->{changes}->[0]->{rev} || warn "no rev?";
67 $seq = $change->{seq} || warn "no seq?";
69 debug 'change' => $change;
71 if ( filter($change) ) {
72 if ( exists $change->{doc}->{trigger}->{active} ) {
73 debug 'trigger.active', $change->{doc}->{trigger}->{active};
75 $change->{doc}->{trigger}->{active} = [ time() ];
77 debug 'TRIGGER start PUT ', $change->{doc};
78 $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
79 my ($client,$tx) = @_;
81 if ( $tx->res->code == 409 ) {
82 info "TRIGGER ABORTED started on another worker? ", $tx->error;
84 info "ERROR $url/$id ", $tx->error;
87 my $res = $tx->res->json;
88 $change->{doc}->{_rev} = $res->{rev};
90 debug "TRIGGER execute ", $change->{doc};
93 push @{ $change->{doc}->{trigger}->{active} }, time(), 0; # last timestamp
95 $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
96 my ($client,$tx) = @_;
98 info "ERROR $url/$id", $tx->error;
100 my $res = $tx->res->json;
101 $change->{doc}->{_rev} = $res->{rev};
102 info "TRIGGER finish ", $change->{doc};
110 warn "UNKNOWN", $json->encode($change);
122 die "ERROR ", $json->encode($error) if $error;