Skip to content
Snippets Groups Projects
Verified Commit 976f22c3 authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Prechod na redis pub/sub

parent b7f70132
No related branches found
No related tags found
No related merge requests found
Pipeline #6473 passed
......@@ -2,7 +2,7 @@ image: docker:20.10.9
variables:
DOCKER_TLS_CERTDIR: "/certs"
IMAGE_VER: 2.4.1
IMAGE_VER: 2.4.2
services:
- docker:20.10.9-dind
......
......@@ -9,7 +9,6 @@ sub create ($c) {
$c->openapi->valid_input or return;
my $args = $c->req->json;
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $announcement = $c->schema->resultset('Announcement')->create({
user_id => $c->user->{id},
......@@ -23,7 +22,7 @@ sub create ($c) {
id => $announcement->id,
});
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'announcement_created',
payload => $announcement->format(),
});
......@@ -67,12 +66,11 @@ sub update ($c) {
return $c->error(404, 'Announcement entry not found') if ! $entry;
my $update = $c->prepare_update_data( $entry, $c->req->json );
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$entry->update( $update );
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'announcement_changed',
payload => {
id => $entry->id,
......@@ -91,12 +89,11 @@ sub delete ($c) {
my $entry = $c->schema->resultset('Announcement')->find( $c->stash->{id} );
return $c->error(404, 'Announcement entry not found') if ! $entry;
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$entry->update( { deleted => \'now()'} );
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'announcement_deleted',
payload => {
id => $entry->id,
......
......@@ -30,12 +30,11 @@ sub update ($c) {
return $c->error(403, 'Config entry protected') if $entry->is_readonly;
my $update = $c->prepare_update_data( $entry, $c->req->json );
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$entry->update( $update );
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'config_entry_changed',
payload => {
id => $entry->id,
......
......@@ -58,9 +58,7 @@ sub create ($c) {
content => $args->{content},
});
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'post_created',
payload => $post->view->format(),
});
......@@ -202,7 +200,6 @@ sub update ($c) {
}
my $update = $c->prepare_update_data( $post, $c->req->json );
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
if ( $update->{state} && $post->state != $update->{state} ) {
......@@ -227,7 +224,7 @@ sub update ($c) {
id => $announcement->id
});
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'announcement_created',
payload => $announcement->format(),
});
......@@ -245,7 +242,7 @@ sub update ($c) {
changed => \'now()',
});
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'post_changed',
payload => {
id => $post->id,
......@@ -263,12 +260,11 @@ sub delete ($c) {
my $post = $c->schema->resultset('Post')->find($c->stash->{id});
return $c->error(404, 'Post not found') if ! $post;
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$post->update({ deleted => \'now()', });
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'post_deleted',
payload => {
id => $post->id,
......@@ -322,7 +318,6 @@ sub ranking ($c) {
$update->{ranking_dislikes}++ if $args->{ranking} == -1;
}
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$post->update( $update );
......@@ -337,7 +332,7 @@ sub ranking ($c) {
});
}
$pubsub->json('notify')->notify(notify => {
$c->redis->pubsub->json('notify')->notify(notify => {
event => 'post_ranked',
payload => {
id => $post->id,
......
......@@ -34,8 +34,7 @@ sub update ($c) {
return $c->error(404, 'Program entry not found') if ! $entry;
my $update = $c->prepare_update_data( $entry, $c->req->json );
# PG_PUBSUB
# my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
# muze byt jen jeden aktualni bod programu
......@@ -47,21 +46,13 @@ sub update ($c) {
$entry->update( $update );
$c->redis->pubsub->notify(
program_entry_changed => {
$c->redis->pubsub->json("notify")->notify(notify => {
event => 'program_entry_changed',
payload => {
id => $entry->id,
%{ $update },
}
);
# PG_PUBSUB
# $pubsub->json('notify')->notify( notify => {
# event => 'program_entry_changed',
# payload => {
# id => $entry->id,
# %{ $update },
# }
# });
});
$guard->commit;
......
......@@ -29,7 +29,6 @@ sub me ($c){
sub ban ($c){
my $user = $c->_get( $c->stash->{id} ) // return;
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$user->update({ banned_until => \"now()+'8 hour'", });
......@@ -50,12 +49,12 @@ sub ban ($c){
id => $announcement->id,
});
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'announcement_created',
payload => $announcement->format(),
});
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'user_banned',
payload => $c->spec_filter($user->formatted, 'User'),
});
......@@ -66,14 +65,12 @@ sub ban ($c){
sub unban ($c){
my $user = $c->_get( $c->stash->{id} ) // return;
my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
my $guard = $c->schema->txn_scope_guard;
$user->update({ banned_until => undef });
#TODO: ANN
$pubsub->json('notify')->notify( notify => {
$c->redis->pubsub->json('notify')->notify( notify => {
event => 'user_unbanned',
payload => $c->spec_filter($user->formatted, 'User'),
});
......
......@@ -22,16 +22,11 @@ sub main {
$c->res->headers->add('Sec-WebSocket-Extensions' => 'permessage-deflate');
}
$c->redis->pubsub->json('notify')->listen(
$c->redis->pubsub->listen(
notify => sub($pubsub, $payload) {
$c->send($payload);
}
);
# PG_PUBSUB
# my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
# $pubsub->listen(notify => sub($pubsub, $payload) {
# $c->send($payload);
# });
$c->on(json => sub( $c, $message ) {
if ( $message->{event} eq 'KEEPALIVE' ) {
......@@ -103,8 +98,6 @@ sub main {
});
$c->on(finish => sub ($c, $code, $reason = undef) {
# PG_PUBSUB
# $pubsub->unlisten('notify');
$c->redis->pubsub->unlisten('notify');
$c->app->log->debug("WebSocket closed with status $code");
});
......
......@@ -18,15 +18,4 @@ create table "events" (
foreign key ("owner_id") references "users" ("id") on update cascade on delete restrict
);
create table "events_acl" (
"id" integer not null default nextval('uid_seq'),
"event_id" integer not null,
"subject_class" varchar(8) not null,
"subject" text not null,
"role" text,
primary key("id"),
unique("event_id", "subject_class", "subject"),
foreign key ("event_id") references "events" ("id") on update cascade on delete cascade
);
alter table "program" add column "event_id" integer;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment