Use main:: namespace to preserve telegram and redis context
This commit is contained in:
@@ -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 ();
|
||||
|
||||
|
||||
@@ -75,5 +75,6 @@
|
||||
"redis_port" : "6379",
|
||||
"redis_channels" : [
|
||||
"telegram"
|
||||
]
|
||||
],
|
||||
"redis_router_channel" : "misc"
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -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,12 +372,45 @@ 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}
|
||||
);
|
||||
|
||||
# Don't forget to update global ref to our redis context
|
||||
$main::REDIS = $r;
|
||||
|
||||
$r->on (
|
||||
error => sub {
|
||||
my ($conn, $error) = @_;
|
||||
|
||||
$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;
|
||||
|
||||
@@ -391,10 +424,17 @@ sub __redisListener {
|
||||
|
||||
return;
|
||||
}
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
# 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) {
|
||||
|
||||
Reference in New Issue
Block a user