From 181b62cd434cc4bcbe832ecc2af759f247a171ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz> Date: Thu, 4 Nov 2021 23:36:34 +0100 Subject: [PATCH] Prepnuti pubsub s postgres na redis --- .gitlab-ci.yml | 2 +- Dockerfile | 2 +- lib/PiTube.pm | 7 +++++-- lib/PiTube/Controller/Nginx.pm | 4 +--- lib/PiTube/Controller/Websockets.pm | 22 +++++++++++----------- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b1d388d..8820f86 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,7 +2,7 @@ image: docker:20.10.9 variables: DOCKER_TLS_CERTDIR: "/certs" - IMAGE_VER: 2.1.2 + IMAGE_VER: 2.2.0 services: - docker:20.10.9-dind diff --git a/Dockerfile b/Dockerfile index fb83dbc..01b3d7f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,6 +26,7 @@ RUN cpanm \ Media::Info \ Mojolicious@8.73 \ Mojo::Pg \ + Mojo::Redis \ Mojo::JWT \ Mojolicious::Plugin::Authentication \ Mojolicious::Plugin::Authorization @@ -39,5 +40,4 @@ RUN npm update && npm install && npm run build && rm -rf mode_modules USER nobody EXPOSE 3000 -#CMD hypnotoad -f /opt/PiTube/script/pitube CMD /opt/PiTube/script/pitube daemon diff --git a/lib/PiTube.pm b/lib/PiTube.pm index 4517d6b..5c605a9 100644 --- a/lib/PiTube.pm +++ b/lib/PiTube.pm @@ -2,10 +2,10 @@ package PiTube; use Mojo::Base 'Mojolicious'; use Mojo::Pg; +use Mojo::Redis; use Mojolicious::Plugin::Authentication; use Mojolicious::Plugin::Authorization; use Net::OAuth2::Profile::WebServer; -use Redis; use PiTube::Schema; sub startup { @@ -24,7 +24,10 @@ sub startup { $self->plugin('PiTube::Helpers::OAuth2'); # Pripojeni na redis - my $redis = Redis->new( %{ $cfg->{redis} } ); +# my $redis = Redis->new( %{ $cfg->{redis} } ); +# $self->helper( redis => sub { return $redis; } ); + + my $redis = Mojo::Redis->new( 'redis://' . $cfg->{redis}{server} ); $self->helper( redis => sub { return $redis; } ); # migrace schematu diff --git a/lib/PiTube/Controller/Nginx.pm b/lib/PiTube/Controller/Nginx.pm index 28f791d..78ef824 100644 --- a/lib/PiTube/Controller/Nginx.pm +++ b/lib/PiTube/Controller/Nginx.pm @@ -1,7 +1,6 @@ package PiTube::Controller::Nginx; use Mojo::Base 'Mojolicious::Controller'; -use Mojo::Pg::PubSub; use feature 'signatures'; no warnings qw{ experimental::signatures }; @@ -9,7 +8,6 @@ no warnings qw{ experimental::signatures }; use constant HLS => qr/hls\/([a-z0-9\-]+)(_\w+)?(\/\w+)?\.(m3u8|ts)$/i; sub callback_rtmp($c) { - my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg); if ( $c->param('call') =~ /publish/ ) { my $name = $c->param('name'); @@ -43,7 +41,7 @@ sub callback_rtmp($c) { is_live => ( $c->param('call') =~ /done/ ) ? 'f':'t', }); - $pubsub->json('streams')->notify( streams => { + $c->redis->pubsub->json('streams')->notify( streams => { call => $c->param('call'), stream_id => $stream->id, }); diff --git a/lib/PiTube/Controller/Websockets.pm b/lib/PiTube/Controller/Websockets.pm index 0dbc907..4267ff4 100644 --- a/lib/PiTube/Controller/Websockets.pm +++ b/lib/PiTube/Controller/Websockets.pm @@ -1,7 +1,7 @@ package PiTube::Controller::Websockets; use Mojo::Base 'Mojolicious::Controller'; -use Mojo::Pg::PubSub; +use Mojo::JSON qw(encode_json); use feature 'signatures'; no warnings qw{ experimental::signatures }; @@ -10,7 +10,7 @@ use constant SOCKET_INACTIVITY_TIMEOUT => 300; sub main { my $c = shift; - my $ip = $c->tx->remote_address; +# my $ip = $c->tx->remote_address; my $key = $c->req->headers->header('Sec-WebSocket-Key'); $c->inactivity_timeout(SOCKET_INACTIVITY_TIMEOUT); @@ -20,25 +20,25 @@ sub main { $c->res->headers->add('Sec-WebSocket-Extensions' => 'permessage-deflate'); } - my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg); - - $pubsub->listen(streams => sub($pubsub, $payload) { - $c->send($payload); - }); + $c->redis->pubsub->json('streams')->listen( + streams => sub($pubsub, $payload) { + $c->send({json => $payload}); + } + ); $c->on(json => sub( $c, $message ) { if ( $message->{stream} ) { - $c->redis->set( + $c->redis->db->set( join (':', ('live', $message->{stream}, $key)), 'live', 'EX', 16 ); - my $count = $c->redis->keys( 'live:' . $message->{stream} . ':*' ); - $c->send({json => { watchers => $count }}); + my $keys = $c->redis->db->keys( 'live:' . $message->{stream} . ':*' ); + $c->send(encode_json({ watchers => scalar @{$keys} })); } }); $c->on(finish => sub ($c, $code, $reason = undef) { - $pubsub->unlisten('streams'); + $c->redis->pubsub->unlisten('streams'); $c->app->log->debug("WebSocket closed with status $code"); }); } -- GitLab