| 1 | package Mojolicious::Plugin::RedisSubscriber; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Cache::RedisDB; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Mojo::Redis; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use YAML::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | our $VERSION = "0.0.1"; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Mojolicious::Plugin::RedisSubscriber | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Mojolicious::Plugin::RedisSubscriber; | ||||||||||||||||||||||||||||
| 21 | my $redis = Mojolicious::Plugin::RedisSubscriber->new; | ||||||||||||||||||||||||||||
| 22 | my $cb = $redis->subscribe("feed::frxEURUSD", sub { ... }); | ||||||||||||||||||||||||||||
| 23 | ...; | ||||||||||||||||||||||||||||
| 24 | $redis->unsubscribe("feed::frxEURUSD", $cb); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Module subscribes to specified channels and emits events when there are messages. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | has channel_hash => sub { return {'Mojo::RedisSubscriber' => 1} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | has redis => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 36 | 0 | my $_subscribe; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | $_subscribe = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 0 | my $channels = shift; | |||||||||||||||||||||||||||
| 39 | 0 | my $subs = Mojo::Redis::Subscription->new( | |||||||||||||||||||||||||||
| 40 | server => Cache::RedisDB->redis_server_info, | ||||||||||||||||||||||||||||
| 41 | channels => $self->channels, | ||||||||||||||||||||||||||||
| 42 | encoding => '', | ||||||||||||||||||||||||||||
| 43 | timeout => 300, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $subs->on(close => sub { $_subscribe->($self->channels) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $subs->on(error => sub { $_subscribe->($self->channels) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $subs->on(timeout => sub { $_subscribe->($self->channels) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | $subs->on( | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | message => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 50 | 0 | my (undef, $message, $channel) = @_; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ($message && $message =~ /^--/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$message and $message =~ /^--/u
MC/DC0%0/2
$message and $message =~ /^--/u
$message
$message =~ /^--/u
Truth table0%0/3
A and B
A= $messageB= $message =~ /^--/u
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $message = try { Load($message) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 0 | $self->emit($channel => $message); | |||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | 0 | ); | |||||||||||||||||||||||||||
| 57 | 0 | $subs->connect; | |||||||||||||||||||||||||||
| 58 | 0 | $self->redis($subs); | |||||||||||||||||||||||||||
| 59 | 0 | return $subs; | |||||||||||||||||||||||||||
| 60 | 0 | }; | |||||||||||||||||||||||||||
| 61 | 0 | return $_subscribe->($self->channels); | |||||||||||||||||||||||||||
| 62 | 3 | }; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 METHODS | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Module provides the following methods: | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head2 $self->channel_hash | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Returns the underlying subscriber object. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 $self->channels | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Returns reference to list of channel names to which redis is subscribed. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub channels { | ||||||||||||||||||||||||||
|
Subroutine
channels: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 0 | return [keys %{$self->channel_hash}]; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head2 $self->redis | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | returns the underlying redis server object. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 $self->subscribe($channel => $callback) | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Ensures that module subscribed to specified redis channels and adds | ||||||||||||||||||||||||||||
| 92 | I<$callback> to the list invoked when there's a message in channel | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub subscribe { | ||||||||||||||||||||||||||
|
Subroutine
subscribe: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 0 | my ($self, $channel, $callback) = @_; | |||||||||||||||||||||||||||
| 98 | 0 | my $redis = $self->redis; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | unless ($self->channel_hash->{$channel}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $redis->execute([subscribe => "$channel"]); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | return $self->on($channel => $callback); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head2 $self->unsubscribe($channel => $callback) | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Unsubscribes I<$callback> from the I<$channel> | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub unsubscribe { | ||||||||||||||||||||||||||
|
Subroutine
unsubscribe: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | my ($self, $channel, $callback) = @_; | |||||||||||||||||||||||||||
| 113 | 0 | my $redis = $self->redis; | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | return unless $self->channel_hash->{$channel}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | unless (--$self->channel_hash->{$channel}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | $redis->execute([unsubscribe => "$channel"]); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 0 | return $self->SUPER::unsubscribe($channel => $callback); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 FEATURES NOT YET SUPPORTED | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Configuration vs config yml files, particularly regarding default channels | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | Copyright (C) 2015 Regent Markets. This may be distributed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3 | 1; | |||||||||||||||||||||||||||