| 1 | package Disque; | ||
| 2 | |||
| 3 | # ABSTRACT: Perl binding for Disque message broker | ||
| 4 | # VERSION | ||
| 5 | # AUTHORITY | ||
| 6 | |||
| 7 | our $VERSION = '0.04'; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use IO::Socket::INET; | ||
| 13 | use IO::Socket::UNIX; | ||
| 14 | use List::Util qw(shuffle); | ||
| 15 | use Carp qw( croak confess ); | ||
| 16 | #use Data::Dump qw(dump); | ||
| 17 | |||
| 18 | use Redis; | ||
| 19 | |||
| 20 | use constant DEBUG => $ENV{DISQUE_DEBUG}; | ||
| 21 | use constant BUFSIZE => 4096; | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my ($class, %args) = @_; | ||
| 25 | my $self = bless {}, $class; | ||
| 26 | my $default = '127.0.0.1:7711'; | ||
| 27 | my $servers = $args{servers} || [$default]; | ||
| 28 | my $len = scalar @{$servers}; | ||
| 29 | my @my_servers = ''; | ||
| 30 | my $hello = ''; | ||
| 31 | my $n = 1; | ||
| 32 | |||
| 33 | $self->{sock_timeout} = $args{sock_timeout} || 0; | ||
| 34 | $self->{conn_timeout} = $args{conn_timeout} || 0; | ||
| 35 | $self->{read_timeout} = $args{read_timeout} || 0; | ||
| 36 | $self->{write_timeout} = $args{write_timeout} || 0; | ||
| 37 | $self->{debug} = $args{debug} || $ENV{DISQUE_DEBUG}; | ||
| 38 | $self->{unixsock} = $args{unixsock} || undef; | ||
| 39 | $self->{num_servers} = $len; | ||
| 40 | $self->{disable_random_connect} = $args{disable_random_connect} || 0; | ||
| 41 | if ($self->{disable_random_connect}) { @my_servers = @{$servers};} | ||
| 42 | else { @my_servers = shuffle(@{$servers}); } | ||
| 43 | |||
| 44 | $self->{arr_servers} = \@my_servers; | ||
| 45 | |||
| 46 | foreach my $server (@my_servers) { | ||
| 47 | $self->{server} = $server || $default; | ||
| 48 | |||
| 49 | if (exists $args{unixsock}) { | ||
| 50 | $self->{conn} = sub { | ||
| 51 | my ($self) = @_; | ||
| 52 | $self->{sock} = IO::Socket::UNIX->new( | ||
| 53 | Type => SOCK_STREAM, | ||
| 54 | Peer => $self->{unixsock}, | ||
| 55 | Timeout => $self->{sock_timeout}, | ||
| 56 | ); | ||
| 57 | }; | ||
| 58 | } else { | ||
| 59 | $self->{conn} = sub { | ||
| 60 | my ($self) = @_; | ||
| 61 | $self->{sock} = IO::Socket::INET->new( | ||
| 62 | PeerAddr => $self->{server}, | ||
| 63 | Timeout => $self->{sock_timeout}, | ||
| 64 | Proto => 'tcp', | ||
| 65 | ); | ||
| 66 | }; | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->__init; | ||
| 70 | last if $self->{sock}; | ||
| 71 | |||
| 72 | if ($n == $len) { | ||
| 73 | croak "there isn't any Disque instance available to connect"; | ||
| 74 | } | ||
| 75 | $n++; | ||
| 76 | } | ||
| 77 | |||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub __sock_end { | ||
| 82 | my ($self) = @_; | ||
| 83 | |||
| 84 | if (not defined($self->{sock})) { | ||
| 85 | croak "the socket we are trying to close is not set"; | ||
| 86 | } | ||
| 87 | |||
| 88 | return close(delete $self->{sock}); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub __init { | ||
| 92 | my ($self) = @_; | ||
| 93 | |||
| 94 | delete $self->{sock}; | ||
| 95 | $self->{pid} = $$; | ||
| 96 | |||
| 97 | $self->__sock_connect; | ||
| 98 | |||
| 99 | if (defined $self->{unixsock}) { | ||
| 100 | $self->{disque} = Redis->new( | ||
| 101 | server => $self->{server}, | ||
| 102 | debug => $self->{debug}, | ||
| 103 | sock => $self->{unixsock}, | ||
| 104 | ) if $self->{sock}; | ||
| 105 | } else { | ||
| 106 | $self->{disque} = Redis->new( | ||
| 107 | server => $self->{server}, | ||
| 108 | debug => $self->{debug}, | ||
| 109 | read_timeout => $self->{read_timeout}, | ||
| 110 | write_timeout => $self->{write_timeout}, | ||
| 111 | cnx_timeout => $self->{sock_timeout}, | ||
| 112 | ) if $self->{sock}; | ||
| 113 | } | ||
| 114 | |||
| 115 | $self->__hello if $self->{sock}; | ||
| 116 | |||
| 117 | return $self; | ||
| 118 | } | ||
| 119 | |||
| 120 | # The current connection has failed | ||
| 121 | # and we must reconnect to another Disque instance | ||
| 122 | sub __disque_reconnect { | ||
| 123 | my $self = shift; | ||
| 124 | my $errn = shift; | ||
| 125 | my $from = shift; | ||
| 126 | my $flag = 0; | ||
| 127 | |||
| 128 | if ($errn =~ m/Not connected to any server/) { $flag = 1; } | ||
| 129 | elsif ($errn =~ m/Error while reading from Redis/) { $flag = 1; } | ||
| 130 | |||
| 131 | if ($flag == 0) { | ||
| 132 | croak $errn; | ||
| 133 | } else { | ||
| 134 | warn "lost connection from $self->{server} $!\n"; | ||
| 135 | } | ||
| 136 | |||
| 137 | if ($self->{num_servers} <= 1) { | ||
| 138 | croak "there isn't any Disque instance available to connect"; | ||
| 139 | } | ||
| 140 | |||
| 141 | shift @{$self->{arr_servers}}; | ||
| 142 | $self->{num_servers}--; # Decrease available servers | ||
| 143 | $self->{server} = @{$self->{arr_servers}}[0]; | ||
| 144 | $self->__init; | ||
| 145 | $self->$from(@_); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub __hello { | ||
| 149 | my $self = shift; | ||
| 150 | $self->__sock_connect unless exists $self->{sock}; | ||
| 151 | $self->{hello} = $self->{disque}->__std_cmd('HELLO'); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub qlen { | ||
| 155 | my $self = shift; | ||
| 156 | my $resp = eval {$self->{disque}->__std_cmd('QLEN', @_)}; | ||
| 157 | $self->__disque_reconnect($@, 'qlen', @_) if $@; | ||
| 158 | return $resp; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub qpeek { | ||
| 162 | my $self = shift; | ||
| 163 | my $resp = eval {$self->{disque}->__std_cmd('QPEEK', @_)}; | ||
| 164 | $self->__disque_reconnect($@, 'qpeek', @_) if $@; | ||
| 165 | return $resp; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub enqueue { | ||
| 169 | my $self = shift; | ||
| 170 | my $resp = eval {$self->{disque}->__std_cmd('ENQUEUE', @_)}; | ||
| 171 | $self->__disque_reconnect($@, 'enqueue', @_) if $@; | ||
| 172 | return $resp; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub dequeue { | ||
| 176 | my $self = shift; | ||
| 177 | my $resp = eval {$self->{disque}->__std_cmd('DEQUEUE', @_)}; | ||
| 178 | $self->__disque_reconnect($@, 'dequeue', @_) if $@; | ||
| 179 | return $resp; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub qscan { | ||
| 183 | my $self = shift; | ||
| 184 | my $resp = eval {$self->{disque}->__std_cmd('QSCAN', @_)}; | ||
| 185 | $self->__disque_reconnect($@, 'qscan', @_) if $@; | ||
| 186 | return $resp; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub qstat { | ||
| 190 | my $self = shift; | ||
| 191 | my $resp = eval {$self->{disque}->__std_cmd('QSTAT', @_)}; | ||
| 192 | $self->__disque_reconnect($@, 'qstat', @_) if $@; | ||
| 193 | return $resp; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub jscan { | ||
| 197 | my $self = shift; | ||
| 198 | my $resp = eval {$self->{disque}->__std_cmd('JSCAN', @_)}; | ||
| 199 | $self->__disque_reconnect($@, 'jscan', @_) if $@; | ||
| 200 | return $resp; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub show { | ||
| 204 | my $self = shift; | ||
| 205 | my @resp = eval {$self->{disque}->__std_cmd('SHOW', @_)}; | ||
| 206 | $self->__disque_reconnect($@, 'show', @_) if $@; | ||
| 207 | return @resp; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub del_job { | ||
| 211 | my $self = shift; | ||
| 212 | my $resp = eval {$self->{disque}->__std_cmd('DELJOB', @_)}; | ||
| 213 | $self->__disque_reconnect($@, 'del_job', @_) if $@; | ||
| 214 | return $resp; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub add_job { | ||
| 218 | my $self = shift; | ||
| 219 | my $resp = eval {$self->{disque}->__std_cmd('ADDJOB', @_)}; | ||
| 220 | $self->__disque_reconnect($@, 'add_job', @_) if $@; | ||
| 221 | return $resp; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub get_job { | ||
| 225 | my $self = shift; | ||
| 226 | my @resp = eval {$self->{disque}->__std_cmd('GETJOB', 'FROM', @_)}; | ||
| 227 | $self->__disque_reconnect($@, 'get_job', @_) if $@; | ||
| 228 | return @resp; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub ack_job { | ||
| 232 | my $self = shift; | ||
| 233 | my $resp = eval {$self->{disque}->__std_cmd('ACKJOB', @_)}; | ||
| 234 | $self->__disque_reconnect($@, 'ack_job', @_) if $@; | ||
| 235 | return $resp; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub fast_ack { | ||
| 239 | my $self = shift; | ||
| 240 | my $resp = eval {$self->{disque}->__std_cmd('FASTACK', @_)}; | ||
| 241 | $self->__disque_reconnect($@, 'fast_ack', @_) if $@; | ||
| 242 | return $resp; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub __sock_connect { | ||
| 246 | my ($self) = @_; | ||
| 247 | $self->{conn}->($self) || warn "unable to connect to disque server at $self->{server}: $!\n"; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub __end_command { | ||
| 251 | my ($self, $cmd) = @_; | ||
| 252 | $self->__sock_connect unless exists $self->{sock}; | ||
| 253 | $self->__run_command($cmd); | ||
| 254 | $self->__sock_end(); | ||
| 255 | $self->{disque}->__close_sock(); | ||
| 256 | return; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub shutdown { | ||
| 260 | my $self = shift; | ||
| 261 | $self->__end_command('SHUTDOWN'); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub quit { | ||
| 265 | my $self = shift; | ||
| 266 | $self->__end_command('QUIT'); | ||
| 267 | } | ||
| 268 | |||
| 269 | sub info { | ||
| 270 | my $self = shift; | ||
| 271 | |||
| 272 | $self->__sock_connect unless exists $self->{sock}; | ||
| 273 | |||
| 274 | my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef; | ||
| 275 | my $info = $self->{disque}->__run_cmd('INFO', 0, $cb, @_) | ||
| 276 | unless $self->{disque}->{reconnect}; | ||
| 277 | |||
| 278 | return $info; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub ping { | ||
| 282 | my $self = shift; | ||
| 283 | $self->__sock_connect unless exists $self->{sock}; | ||
| 284 | $self->{disque}->__std_cmd('PING'); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub __run_command { | ||
| 288 | my ($self, $command, $handle_recv, @args) = @_; | ||
| 289 | $self->__send_command($command, @args); | ||
| 290 | return $self->__recv_response($self->{sock}) unless !defined $handle_recv; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub __send_command { | ||
| 294 | my $self = shift; | ||
| 295 | my $command = shift; | ||
| 296 | |||
| 297 | if ($self->{pid} != $$) { | ||
| 298 | warn "process id is different from main"; | ||
| 299 | } | ||
| 300 | |||
| 301 | warn "[SEND CMD] $command\n" if DEBUG; | ||
| 302 | |||
| 303 | my @command = split /_/, $command; | ||
| 304 | my $n_elems = scalar(@_) + scalar(@command); | ||
| 305 | my $buffer = "\*$n_elems\r\n"; | ||
| 306 | |||
| 307 | for my $bin (@command, @_) { | ||
| 308 | $buffer .= defined($bin) ? '$' . length($bin) . "\r\n$bin\r\n" : "\$-1\r\n"; | ||
| 309 | } | ||
| 310 | |||
| 311 | while ($buffer) { | ||
| 312 | my $len = syswrite $self->{sock}, $buffer, length $buffer; | ||
| 313 | substr $buffer, 0, $len, ""; | ||
| 314 | } | ||
| 315 | |||
| 316 | return; | ||
| 317 | } | ||
| 318 | |||
| 319 | sub __recv_response { | ||
| 320 | my ($self, $sock) = @_; | ||
| 321 | my $data = ''; | ||
| 322 | |||
| 323 | my $len = sysread($sock, $data, BUFSIZE, length($data)); | ||
| 324 | my $recv = substr($data, 0, $len, ''); | ||
| 325 | chomp($recv); | ||
| 326 | warn "[RECV RAW] $recv\n" if DEBUG; | ||
| 327 | return $recv; | ||
| 328 | } | ||
| 329 | |||
| 330 | 1; | ||
| 331 | |||
| 332 | __END__ |