From e74a3e46c1b645ca295f167f56b77231ef4028a6 Mon Sep 17 00:00:00 2001 From: eleksir Date: Sun, 19 Dec 2021 13:54:28 +0300 Subject: [PATCH] Use main:: namespace to preserve telegram and redis context --- bin/aleesa-telegram-bot | 3 ++ data/sample_config.json | 3 +- lib/Cli.pm | 4 +-- lib/TelegramBot.pm | 62 +++++++++++++++++++++++++++++++++-------- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/bin/aleesa-telegram-bot b/bin/aleesa-telegram-bot index b4f00eb..f3bf8b9 100755 --- a/bin/aleesa-telegram-bot +++ b/bin/aleesa-telegram-bot @@ -144,6 +144,9 @@ local $SIG{TERM} = \&__signal_handler; # set correct proctitle $PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::RequireLocalizedPunctuationVars) +# make some global vars for telegram and redis context +our ($TGM $REDIS); + # run bot, at last RunTelegramBot (); diff --git a/data/sample_config.json b/data/sample_config.json index ef3edc8..837b417 100644 --- a/data/sample_config.json +++ b/data/sample_config.json @@ -75,5 +75,6 @@ "redis_port" : "6379", "redis_channels" : [ "telegram" - ] + ], + "redis_router_channel" : "misc" } diff --git a/lib/Cli.pm b/lib/Cli.pm index a18d01f..c9de387 100644 --- a/lib/Cli.pm +++ b/lib/Cli.pm @@ -30,10 +30,10 @@ sub RunCli { $send_to => { from => 'redis_msg_gen.pl', userid => '0', - chatid => 'some_chat_id', + chatid => '0', plugin => 'redis_msg_gen.pl_plugin', message => 'ping?', - mode => 'mode', + mode => 'private', } ); diff --git a/lib/TelegramBot.pm b/lib/TelegramBot.pm index 01af484..d5fb470 100644 --- a/lib/TelegramBot.pm +++ b/lib/TelegramBot.pm @@ -14,6 +14,7 @@ use Log::Any qw ($log); use Math::Random::Secure qw (irand); use Mojo::Base 'Teapot::Bot::Brain'; use Mojo::Redis; +use Mojo::Redis::Connection; use BotLib::Admin qw (FortuneToggleList); use BotLib qw (RandomCommonPhrase Command Highlight BotSleep IsCensored); use BotLib::Conf qw (LoadConf); @@ -33,7 +34,6 @@ my $myusername; my $myfirst_name; my $mylast_name; my $myfullname; -my $redis; has token => $c->{telegrambot}->{token}; @@ -372,22 +372,59 @@ sub __on_msg { } sub __redisListener { - return if (defined $redis); + # If we already connected there is no point to disconnect + if (defined $main::REDIS) { + my $connected = eval { Mojo::Redis::Connection->is_connected ($main::REDIS); }; - $redis = Mojo::Redis->new ( + if ($connected) { + return; + } else { + $main::REDIS = undef; + } + } + + my $r = Mojo::Redis->new ( sprintf 'redis://%s:%s/1', $c->{redis_server}, $c->{redis_port} ); - my $pubsub = $redis->pubsub; - my $sub; + # Don't forget to update global ref to our redis context + $main::REDIS = $r; - foreach my $channel (@{$c->{redis_channels}}) { - $log->info ("Subscribing to $channel"); + $r->on ( + error => sub { + my ($conn, $error) = @_; - $sub->{$channel} = $pubsub->json ($channel)->listen ( - $channel => sub { \&redis_parse_message->(@_); } - ); - } + $log->error ("Redis connection error: $error"); + + # TODO: maintain list of callbacks and clean them up? + + my $redis = Mojo::Redis->new ( + sprintf 'redis://%s:%s/1', $c->{redis_server}, $c->{redis_port} + ); + + # Don't forget to update global ref to our redis context + $main::REDIS = $redis; + return; + } + ); + + $r->on ( + connection => sub { + my ($redis, $connection) = @_; + my $pubsub = $redis->pubsub; + my $sub; + + foreach my $channel (@{$c->{redis_channels}}) { + $log->info ("Subscribing to $channel"); + + $sub->{$channel} = $pubsub->json ($channel)->listen ( + $channel => sub { \&redis_parse_message->(@_); } + ); + } + + return; + } + ); return; } @@ -395,6 +432,9 @@ sub __redisListener { # setup our bot sub init { my $self = shift; + + # Don't forget to update global ref to our telegram context + $main::TGM = $self; my $braindir = $c->{telegrambot}->{braindir}; unless (-d $braindir) {