added include_docs=true, use Mojo::JSON
[angular-mojolicious.git] / couchdb-trigger.pl
1 #!/usr/bin/perl
2
3 # back-end trigger server for CouchDB monitoring changes feed:
4 #
5 # http://wiki.apache.org/couchdb/HTTP_database_API#Changes
6 #
7 # implements state machine using document which you cen put with:
8 #
9 # curl -X PUT http://localhost:5984/monitor/df -d '{"trigger":{"command":"df -P","format":"table"}}'
10 #
11 # DEFAULT TRIGGER EXECUTE SHELL COMMANDS. IT IS NOT SECURE IF YOUR COUCHDB ISN'T SECURE!
12
13 use warnings;
14 use strict;
15
16 use lib 'common/mojo/lib';
17
18 use Mojo::Client;
19 use Mojo::JSON;
20 use Time::HiRes qw(time);
21 use Data::Dump qw(dump);
22
23 my ( $url, $trigger_path ) = @ARGV;
24
25 $url          ||= 'http://localhost:5984/monitor';
26 $trigger_path ||= 'trigger/shell.pm' ;
27
28 sub commit { warn "# commit ignored\n"; }
29 require $trigger_path if -e $trigger_path;
30
31 my $seq = 0;
32
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] }
37 my $error;
38
39 $client->keep_alive_timeout(90); # couchdb timeout is 60s
40
41 while( ! $error ) {
42
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 );
46         $tx->res->body(sub{
47                 my ( $content, $body ) = @_;
48
49                 return if length($body) == 0; # empty chunk, heartbeat?
50
51                 debug 'BODY' => $body;
52
53                 foreach ( split(/\r?\n/, $body) ) { # we can get multiple documents in one chunk
54
55                         my $change = $json->decode($_);
56
57                         if ( exists $change->{error} ) {
58                                 $error = $change;
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} ) {
64
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?";
68
69                                 debug 'change' => $change;
70
71                                 if ( filter($change) ) {
72                                         if ( exists $change->{doc}->{trigger}->{active} ) {
73                                                 debug 'trigger.active',  $change->{doc}->{trigger}->{active};
74                                         } else {
75                                                 $change->{doc}->{trigger}->{active} = [ time() ];
76
77                                                 debug 'TRIGGER start PUT ', $change->{doc};
78                                                 $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
79                                                         my ($client,$tx) = @_;
80                                                         if ($tx->error) {
81                                                                 if ( $tx->res->code == 409 ) {
82                                                                         info "TRIGGER ABORTED started on another worker? ", $tx->error;
83                                                                 } else {
84                                                                         info "ERROR ", $tx->error;
85                                                                 }
86                                                         } else {
87                                                                 my $res = $tx->res->json;
88                                                                 $change->{doc}->{_rev} = $res->{rev};
89
90                                                                 debug "TRIGGER execute ", $change->{doc};
91                                                                 trigger( $change );
92
93                                                                 push @{ $change->{doc}->{trigger}->{active} }, time(), 0; # last timestamp
94
95                                                                 $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
96                                                                         my ($client,$tx) = @_;
97                                                                         if ($tx->error) {
98                                                                                 info "ERROR", $tx->error;
99                                                                         } else {
100                                                                                 my $res = $tx->res->json;
101                                                                                 $change->{doc}->{_rev} = $res->{rev};
102                                                                                 info "TRIGGER finish ", $change->{doc};
103                                                                         }
104                                                                 })->process;
105                                                         }
106                                                 })->process;
107                                         }
108                                 }
109                         } else {
110                                 warn "UNKNOWN", $json->encode($change);
111                         }
112
113                 }
114
115                 commit;
116
117         });
118         $client->start($tx);
119
120 }
121
122 die "ERROR ", $json->encode($error) if $error;