Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/MessageBroker/NoBroker.pm

raw
1 package Lemonldap::NG::Common::MessageBroker::NoBroker;
2
3 # This pseudo message broker dispatches messages into current node only.
4 # It also inserts periodically "newConf" into llng_events to permit to
5 # detect configuration changes using the checkTime parameter.
6
7 use strict;
8 use Lemonldap::NG::Common::Conf::Constants;
9
10 our $VERSION = '2.23.0';
11 our $lastCheck = time;
12 our $channels = {};
13
14 sub new {
15 my ( $class, $conf, $logger ) = @_;
16 $channels->{ $conf->{eventQueueName} } //= [];
17 $channels->{ $conf->{statusQueueName} } = 'no';
18 return bless {
19 checkTime => $conf->{checkTime},
20 eventQueueName => $conf->{eventQueueName},
21 logger => $logger,
22 }, $class;
23 }
24
25 sub publish {
26 my ( $self, $channel, $msg ) = @_;
27 die 'eventStatus not authorized without broker'
28 if $channels->{$channel} eq 'no' and $ENV{FORCE_STATUS} ne 'force status';
29 die unless $channel and $msg;
30 $channels->{$channel} = []
31 unless ref( $channels->{$channel} );
32 push @{ $channels->{$channel} }, $msg;
33 }
34
35 sub subscribe { }
36
37 sub getNextMessage {
38 my ( $self, $channel, $delay ) = @_;
39 if ( time >= $lastCheck + $self->{checkTime} ) {
40 $self->publish( $self->{eventQueueName}, { action => 'newConf' } );
41 $lastCheck = time;
42 }
43 if ( ref( $channels->{$channel} )
44 and @{ $channels->{$channel} } )
45 {
46 return shift @{ $channels->{$channel} };
47 }
48 }
49
50 sub waitForNextMessage {
51 my ( $self, $channel ) = @_;
52 while (1) {
53 if ( my $msg = $self->getNextMessage($channel) ) {
54 return $msg;
55 }
56 sleep 1;
57 }
58 }
59
60 1;