Use main:: namespace to preserve telegram and redis context

This commit is contained in:
eleksir
2021-12-19 13:54:28 +03:00
parent 186b112eba
commit e74a3e46c1
4 changed files with 58 additions and 14 deletions

View File

@@ -144,6 +144,9 @@ local $SIG{TERM} = \&__signal_handler;
# set correct proctitle # set correct proctitle
$PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::RequireLocalizedPunctuationVars) $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 # run bot, at last
RunTelegramBot (); RunTelegramBot ();

View File

@@ -75,5 +75,6 @@
"redis_port" : "6379", "redis_port" : "6379",
"redis_channels" : [ "redis_channels" : [
"telegram" "telegram"
] ],
"redis_router_channel" : "misc"
} }

View File

@@ -30,10 +30,10 @@ sub RunCli {
$send_to => { $send_to => {
from => 'redis_msg_gen.pl', from => 'redis_msg_gen.pl',
userid => '0', userid => '0',
chatid => 'some_chat_id', chatid => '0',
plugin => 'redis_msg_gen.pl_plugin', plugin => 'redis_msg_gen.pl_plugin',
message => 'ping?', message => 'ping?',
mode => 'mode', mode => 'private',
} }
); );

View File

@@ -14,6 +14,7 @@ use Log::Any qw ($log);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use Mojo::Base 'Teapot::Bot::Brain'; use Mojo::Base 'Teapot::Bot::Brain';
use Mojo::Redis; use Mojo::Redis;
use Mojo::Redis::Connection;
use BotLib::Admin qw (FortuneToggleList); use BotLib::Admin qw (FortuneToggleList);
use BotLib qw (RandomCommonPhrase Command Highlight BotSleep IsCensored); use BotLib qw (RandomCommonPhrase Command Highlight BotSleep IsCensored);
use BotLib::Conf qw (LoadConf); use BotLib::Conf qw (LoadConf);
@@ -33,7 +34,6 @@ my $myusername;
my $myfirst_name; my $myfirst_name;
my $mylast_name; my $mylast_name;
my $myfullname; my $myfullname;
my $redis;
has token => $c->{telegrambot}->{token}; has token => $c->{telegrambot}->{token};
@@ -372,22 +372,59 @@ sub __on_msg {
} }
sub __redisListener { 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} sprintf 'redis://%s:%s/1', $c->{redis_server}, $c->{redis_port}
); );
my $pubsub = $redis->pubsub; # Don't forget to update global ref to our redis context
my $sub; $main::REDIS = $r;
foreach my $channel (@{$c->{redis_channels}}) { $r->on (
$log->info ("Subscribing to $channel"); error => sub {
my ($conn, $error) = @_;
$sub->{$channel} = $pubsub->json ($channel)->listen ( $log->error ("Redis connection error: $error");
$channel => sub { \&redis_parse_message->(@_); }
); # 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; return;
} }
@@ -395,6 +432,9 @@ sub __redisListener {
# setup our bot # setup our bot
sub init { sub init {
my $self = shift; my $self = shift;
# Don't forget to update global ref to our telegram context
$main::TGM = $self;
my $braindir = $c->{telegrambot}->{braindir}; my $braindir = $c->{telegrambot}->{braindir};
unless (-d $braindir) { unless (-d $braindir) {