From: Dobrica Pavlinusic Date: Tue, 30 Nov 2010 21:49:53 +0000 (+0100) Subject: extract couchdb triggers into own pm files X-Git-Url: http://git.rot13.org/?p=angular-mojolicious.git;a=commitdiff_plain;h=f6f884c13f556ab5e6e834d609fd6d72f6adf4db extract couchdb triggers into own pm files this allows to specify database and trigger file to load on start as arguments, e.g: ./couchdb-trigger.pl http://localhost:5984/drzb2011 trigger/email.pm --- diff --git a/couchdb-trigger.pl b/couchdb-trigger.pl index d39f007..02f5c32 100755 --- a/couchdb-trigger.pl +++ b/couchdb-trigger.pl @@ -18,21 +18,14 @@ 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; -} +require $trigger_path if -e $trigger_path; my $seq = 0; @@ -77,11 +70,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 { @@ -97,9 +90,9 @@ while( ! $error ) { $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) = @_; diff --git a/trigger/email.pm b/trigger/email.pm new file mode 100644 index 0000000..e9739f9 --- /dev/null +++ b/trigger/email.pm @@ -0,0 +1,12 @@ +sub filter { + my $change = shift; + return 1 if $change->{doc}->{person}->{email}; +} + +sub trigger { + my $change = shift; + warn "# send_email ",dump($change->{doc}->{person}); + $change->{doc}->{email_sent}++; +} + +1; diff --git a/trigger/shell.pm b/trigger/shell.pm new file mode 100644 index 0000000..1c89f8b --- /dev/null +++ b/trigger/shell.pm @@ -0,0 +1,20 @@ +sub filter { + my $change = shift; + return 1 if $change->{doc}->{trigger}->{command}; +} + + +sub trigger { + my $change = shift; + my $trigger = $change->{doc}->{trigger}; + if ( my $command = $trigger->{command} ) { + # FIXME SECURITY HOLE + my $output = $trigger->{output} = `$command`; + + $trigger->{output} = + [ map { [ split (/\s+/,$_) ] } split(/\n/,$output) ] + if $trigger->{format} =~ m/table/i; + } +} + +1;