MySQL5/PubSub.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 171 CC 21 SCAR 44.3
CC
21
Cov
0%
_subscriber_pid
182.0
notify
12.0
unlisten
12.0
CRAP 83.5
1 package Mojo::MySQL5::PubSub;
2 use Mojo::Base 'Mojo::EventEmitter';
3  
4 use Scalar::Util 'weaken';
5  
6 use constant DEBUG => $ENV{MOJO_PUBSUB_DEBUG} || 0;
7  
8 has 'mysql';
9  
10 sub DESTROY {
11 my $self = shift;
12 return unless $self->{db} and $self->mysql;
13 $self->mysql->db->query('delete from mojo_pubsub_subscribe where pid = ?', $self->_subscriber_pid);
14 }
15  
16 sub listen {
17 my ($self, $channel, $cb) = @_;
18 my $pid = $self->_subscriber_pid;
19 warn "listen channel:$channel subscriber:$pid\n" if DEBUG;
20 $self->mysql->db->query('replace mojo_pubsub_subscribe(pid, channel, ts) values (?, ?, current_timestamp)',
21 $pid, $channel);
22 push @{$self->{chans}{$channel}}, $cb;
23 return $cb;
24 }
25  
26 sub notify {
27 my ($self, $channel, $payload) = @_;
28 my $db = $self->mysql->db;
29 $payload //= '';
30 warn "notify channel:$channel $payload\n" if DEBUG;
31 $self->_init($db) unless $self->{init}++;
32 $db->query('insert into mojo_pubsub_notify(channel, payload) values (?, ?)', $channel, $payload);
33 return $self;
34 }
35  
36 sub unlisten {
37 my ($self, $channel, $cb) = @_;
38 my $pid = $self->_subscriber_pid;
39 warn "unlisten channel:$channel subscriber:$pid\n" if DEBUG;
40 my $chan = $self->{chans}{$channel};
41 @$chan = grep { $cb ne $_ } @$chan;
42 return $self if @$chan;
43 $self->mysql->db->query('delete from mojo_pubsub_subscribe where pid = ? and channel = ?', $pid, $channel);
44 delete $self->{chans}{$channel};
45 return $self;
46 }
47  
48 sub _notifications {
49 my $self = shift;
50 my $result = $self->{db}->query('select id, channel, payload from mojo_pubsub_notify where id > ?', $self->{last_id});
51  
52 while (my $row = $result->array) {
53 my ($id, $channel, $payload) = @$row;
54 $self->{last_id} = $id;
55 next unless exists $self->{chans}{$channel};
56 warn "received $id on $channel: $payload\n" if DEBUG;
57 for my $cb (@{$self->{chans}{$channel}}) { $self->$cb($payload) }
58 }
59 }
60  
61 sub _init {
62 my ($self, $db) = @_;
63  
64 $self->mysql->migrations->name('pubsub')->from_data->migrate;
65  
66 # cleanup old subscriptions and notifications
67 $db->query('delete from mojo_pubsub_notify where ts < date_add(current_timestamp, interval -10 minute)');
68 $db->query('delete from mojo_pubsub_subscribe where ts < date_add(current_timestamp, interval -1 hour)');
69 }
70  
71 sub _subscriber_pid {
72 my $self = shift;
73  
74 # Fork-safety
75 if (($self->{pid} //= $$) ne $$) {
76 my $pid = $self->{db}->pid if $self->{db};
77 warn 'forked subscriber pid:' . ($pid || 'N/A') . "\n" if DEBUG;
78 $self->{db}->disconnect if $pid;
79 delete @$self{qw(chans init pid db)};
80 }
81  
82 return $self->{db}->pid if $self->{db};
83  
84 $self->{db} = $self->mysql->db;
85 my $pid = $self->{db}->pid;
86 $self->{db}->connection->url->options->{query_timeout} = 610;
87  
88 $self->_init($self->{db}) unless $self->{init}++;
89  
90 if (defined $self->{last_id}) {
91  
92 # read unread notifications
93 $self->_notifications;
94 }
95 else {
96 # get id of the last message
97 my $array = $self->{db}->query('select id from mojo_pubsub_notify order by id desc limit 1')->array;
98 $self->{last_id} = defined $array ? $array->[0] : 0;
99 }
100  
101 # re-subscribe
102 $self->{db}->query('replace mojo_pubsub_subscribe(pid, channel) values (?, ?)', $pid, $_)
103 for keys %{$self->{chans}};
104  
105 weaken $self->{db}->{mysql};
106 weaken $self;
107  
108 my $cb;
109 $cb = sub {
110 my ($db, $err, $result) = @_;
111 if ($err) {
112 warn "wake up error: $err" if DEBUG;
113 eval { $db->disconnect };
114 delete $self->{db};
115 eval { $self->_subscriber_pid };
116 }
117 elsif ($self and $self->{db}) {
118 $self->_notifications;
119 $db->query('update mojo_pubsub_subscribe set ts = current_timestamp where pid = ?', $pid);
120 $db->query('select sleep(600)', $cb);
121 }
122 };
123 $self->{db}->query('select sleep(600)', $cb);
124  
125 warn "reconnect subscriber pid: $pid\n" if DEBUG;
126 $self->emit(reconnect => $self->{db});
127  
128 return $pid;
129 }
130  
131 1;
132  
133 =encoding utf8
134  
135 =head1 NAME
136  
137 Mojo::MySQL5::PubSub - Publish/Subscribe
138  
139 =head1 SYNOPSIS
140  
141 use Mojo::MySQL5::PubSub;
142  
143 my $pubsub = Mojo::MySQL5::PubSub->new(mysql => $mysql);
144 my $cb = $pubsub->listen(foo => sub {
145 my ($pubsub, $payload) = @_;
146 say "Received: $payload";
147 });
148 $pubsub->notify(foo => 'bar');
149 $pubsub->unlisten(foo => $cb);
150  
151 =head1 DESCRIPTION
152  
153 L<Mojo::MySQL5::PubSub> is implementation of the publish/subscribe
154 pattern used by L<Mojo::MySQL5>.
155  
156 Although MySQL does not have C<SUBSCRIBE/NOTIFY> like PostgreSQL and other RDBMs,
157 this module implements similar feature.
158  
159 Single Database connection waits for notification by executing C<SLEEP> on server.
160 C<connection_id> and subscribed channels in stored in C<mojo_pubsub_subscribe> table.
161 Inserting new row in C<mojo_pubsub_notify> table triggers C<KILL QUERY> for
162 all connections waiting for notification.
163  
164 C<PROCESS> privilege is needed for MySQL user to see other users processes.
165 C<SUPER> privilege is needed to be able to execute C<KILL QUERY> for statements
166 started by other users.
167 C<SUPER> privilege may be needed to be able to define trigger.
168  
169 If your applications use this module using different MySQL users it is important
170 the migration script to be executed by user having C<SUPER> privilege on the database.
171  
172  
173 =head1 EVENTS
174  
175 L<Mojo::MySQL5::PubSub> inherits all events from L<Mojo::EventEmitter> and can
176 emit the following new ones.
177  
178 =head2 reconnect
179  
180 $pubsub->on(reconnect => sub {
181 my ($pubsub, $db) = @_;
182 ...
183 });
184  
185 Emitted after switching to a new database connection for sending and receiving
186 notifications.
187  
188 =head1 ATTRIBUTES
189  
190 L<Mojo::MySQL5::PubSub> implements the following attributes.
191  
192 =head2 mysql
193  
194 my $mysql = $pubsub->mysql;
195 $pubsub = $pubsub->mysql(Mojo::MySQL5->new);
196  
197 L<Mojo::MySQL5> object this publish/subscribe container belongs to.
198  
199 =head1 METHODS
200  
201 L<Mojo::MySQL5::PubSub> inherits all methods from L<Mojo::EventEmitter> and
202 implements the following new ones.
203  
204 =head2 listen
205  
206 my $cb = $pubsub->listen(foo => sub {...});
207  
208 Subscribe to a channel, there is no limit on how many subscribers a channel can
209 have.
210  
211 # Subscribe to the same channel twice
212 $pubsub->listen(foo => sub {
213 my ($pubsub, $payload) = @_;
214 say "One: $payload";
215 });
216 $pubsub->listen(foo => sub {
217 my ($pubsub, $payload) = @_;
218 say "Two: $payload";
219 });
220  
221 =head2 notify
222  
223 $pubsub = $pubsub->notify('foo');
224 $pubsub = $pubsub->notify(foo => 'bar');
225  
226 Notify a channel.
227  
228 =head2 unlisten
229  
230 $pubsub = $pubsub->unlisten(foo => $cb);
231  
232 Unsubscribe from a channel.
233  
234 =head1 DEBUGGING
235  
236 You can set the C<MOJO_PUBSUB_DEBUG> environment variable to get some
237 advanced diagnostics information printed to C<STDERR>.
238  
239 MOJO_PUBSUB_DEBUG=1
240  
241 =head1 SEE ALSO
242  
243 L<Mojo::MySQL5>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
244  
245 =cut
246  
247 __DATA__