From f33e05a91cf0cd2c6398e4155c663d7a1e391ca0 Mon Sep 17 00:00:00 2001 From: eleksir Date: Sat, 11 Dec 2021 15:38:58 +0300 Subject: [PATCH] Add test client for redis --- bin/redis_msg_gen.pl | 39 +++++++++++++++++++++++++++++++++++++++ lib/Cli.pm | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/TelegramBot.pm | 8 ++++---- 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 bin/redis_msg_gen.pl create mode 100644 lib/Cli.pm diff --git a/bin/redis_msg_gen.pl b/bin/redis_msg_gen.pl new file mode 100644 index 0000000..a7fe2c0 --- /dev/null +++ b/bin/redis_msg_gen.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +use 5.018; +use strict; +use warnings; +use utf8; +use open qw (:std :utf8); +use English qw ( -no_match_vars ); + +use version; our $VERSION = qw (1.0); + +my $workdir; + +# before we run, change working dir +BEGIN { + use Cwd qw (chdir abs_path); + my @CWD = split /\//xms, abs_path ($PROGRAM_NAME); + + if ($#CWD > 1) { + $#CWD = $#CWD - 1; + } + + $workdir = join '/', @CWD; + chdir $workdir; +} + +use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5"); +use Cli qw (RunCli); + +# set correct proctitle +$PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::RequireLocalizedPunctuationVars) + +# run cli, at last +RunCli (); + +exit 0; + +__END__ +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/lib/Cli.pm b/lib/Cli.pm new file mode 100644 index 0000000..e8571ed --- /dev/null +++ b/lib/Cli.pm @@ -0,0 +1,44 @@ +package Cli; + +# общие модули - синтаксис, кодировки итд +use 5.018; +use strict; +use warnings; +use utf8; +use open qw (:std :utf8); + +# модули для работы приложения +use Mojo::Redis; + +use Conf qw (LoadConf); + +use version; our $VERSION = qw (1.0); +use Exporter qw (import); +our @EXPORT_OK = qw (RunCli); + +my $c = LoadConf (); + +sub RunCli { + my $redis = Mojo::Redis->new ( + sprintf 'redis://%s:%s/1', $c->{redis_server}, $c->{redis_port} + ); + + my $send_to = 'telegram'; + + $redis->json ($send_to)->notify ( + $send_to => { + from => 'redis_msg_gen.pl', + userid => '0', + chatid => 'some_chat_id', + plugin => 'redis_msg_gen.pl_plugin', + message => 'ping?', + mode => 'mode', + } + ); + + return; +} + +1; + +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/lib/TelegramBot.pm b/lib/TelegramBot.pm index 41a25ac..d6d0c37 100644 --- a/lib/TelegramBot.pm +++ b/lib/TelegramBot.pm @@ -13,6 +13,7 @@ use Hailo; use Log::Any qw ($log); use Math::Random::Secure qw (irand); use Mojo::Base 'Teapot::Bot::Brain'; +use Mojo::Redis; use BotLib::Admin qw (FortuneToggleList); use BotLib qw (RandomCommonPhrase Command Highlight BotSleep IsCensored); use BotLib::Conf qw (LoadConf); @@ -32,7 +33,7 @@ my $myusername; my $myfirst_name; my $mylast_name; my $myfullname; -my $redis_mutex; +my $redis; has token => $c->{telegrambot}->{token}; @@ -359,9 +360,9 @@ sub __on_msg { } sub __redisListener { - return if ($redis_mutex); + return if (defined $redis); - my $redis = Mojo::Redis->new ( + $redis = Mojo::Redis->new ( sprintf 'redis://%s:%s/1', $c->{redis_server}, $c->{redis_port} ); @@ -376,7 +377,6 @@ sub __redisListener { ); } - $redis_mutex = 1; return; }