| 1 |
package Lemonldap::NG::Common::MessageBroker::Pg; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use JSON; |
| 5 |
use POSIX qw(:signal_h); |
| 6 |
use Lemonldap::NG::Common::Lib::DBI qw(check_dbh); |
| 7 |
|
| 8 |
our $VERSION = '2.23.0'; |
| 9 |
|
| 10 |
sub new { |
| 11 |
my ( $class, $conf, $logger ) = @_; |
| 12 |
require DBI; |
| 13 |
my $args = $conf->{messageBrokerOptions}; |
| 14 |
unless ($args |
| 15 |
and $args->{dbiChain} |
| 16 |
and $args->{dbiUser} ) |
| 17 |
{ |
| 18 |
$logger->error('MISSING OPTIONS FOR PG PUB/SUB'); |
| 19 |
return undef; |
| 20 |
} |
| 21 |
my $self = bless { %{$args}, logger => $logger }, $class; |
| 22 |
return $self; |
| 23 |
} |
| 24 |
|
| 25 |
sub publish { |
| 26 |
my ( $self, $channel, $msg ) = @_; |
| 27 |
die 'Not a hash msg' unless ref $msg eq 'HASH'; |
| 28 |
my $j = eval { JSON::to_json($msg) }; |
| 29 |
die "MessageBroker publish only hashes! $@" if $@; |
| 30 |
$self->_dbh->do( "NOTIFY $channel, ?", undef, $j ); |
| 31 |
} |
| 32 |
|
| 33 |
sub subscribe { |
| 34 |
my ( $self, $channel ) = @_; |
| 35 |
$self->{messages}{$channel} = []; |
| 36 |
$self->_dbh->do("LISTEN $channel"); |
| 37 |
} |
| 38 |
|
| 39 |
sub getNextMessage { |
| 40 |
my ( $self, $channel, $delay ) = @_; |
| 41 |
return undef |
| 42 |
unless $self->{messages}{$channel}; |
| 43 |
if ( my $notify = $self->_dbh->pg_notifies ) { |
| 44 |
my ( $name, $pid, $payload ) = @$notify; |
| 45 |
$payload = eval { JSON::from_json($payload) }; |
| 46 |
if ($@) { |
| 47 |
$self->{logger}->error("Bad message from Pg: $@"); |
| 48 |
} |
| 49 |
else { |
| 50 |
push @{ $self->{messages}{$name} }, $payload; |
| 51 |
} |
| 52 |
} |
| 53 |
return shift( @{ $self->{messages}{$channel} } ) |
| 54 |
if @{ $self->{messages}{$channel} }; |
| 55 |
} |
| 56 |
|
| 57 |
sub waitForNextMessage { |
| 58 |
my ( $self, $channel ) = @_; |
| 59 |
return undef unless $self->{messages}{$channel}; |
| 60 |
|
| 61 |
# Infinite loop until one message is seen |
| 62 |
my $res; |
| 63 |
while ( not( $res = $self->getNextMessage($channel) ) ) { |
| 64 |
sleep 1; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
sub _dbh { |
| 69 |
my ($self) = @_; |
| 70 |
my $dbh = check_dbh( $self->{_dbh} ); |
| 71 |
return $dbh if $dbh; |
| 72 |
$self->{_dbh} = undef; |
| 73 |
|
| 74 |
# This timeout is inspired from example given by DBI(3) |
| 75 |
my $mask = POSIX::SigSet->new(SIGALRM); |
| 76 |
my $action = |
| 77 |
POSIX::SigAction->new( sub { die "connect timeout\n" }, $mask ); |
| 78 |
my $oldaction = POSIX::SigAction->new(); |
| 79 |
sigaction( SIGALRM, $action, $oldaction ); |
| 80 |
eval { |
| 81 |
eval { |
| 82 |
alarm(10); |
| 83 |
$self->{_dbh} = |
| 84 |
DBI->connect( $self->{dbiChain}, $self->{dbiUser}, |
| 85 |
$self->{dbiPassword}, { RaiseError => 1, AutoCommit => 1, } ); |
| 86 |
|
| 87 |
# Re subscribe to all channels when reconnecting |
| 88 |
$self->subscribe($_) foreach ( keys %{ $self->{messages} || {} } ); |
| 89 |
}; |
| 90 |
alarm(0); |
| 91 |
}; |
| 92 |
return $self->{_dbh} ||= bless {}, 'Lemonldap::NG::DBI::Failed'; |
| 93 |
} |
| 94 |
|
| 95 |
package Lemonldap::NG::DBI::Failed; |
| 96 |
|
| 97 |
sub AUTOLOAD { |
| 98 |
print STDERR "MsgBroker: Unable to load DBI\n"; |
| 99 |
return undef; |
| 100 |
} |
| 101 |
|
| 102 |
1; |