line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Redis2::Server; |
2
|
34
|
|
|
34
|
|
842679
|
use feature 'state'; |
|
34
|
|
|
|
|
127
|
|
|
34
|
|
|
|
|
2336
|
|
3
|
34
|
|
|
34
|
|
16718
|
use Mojo::Asset::File; |
|
34
|
|
|
|
|
203577
|
|
|
34
|
|
|
|
|
301
|
|
4
|
34
|
|
|
34
|
|
1385
|
use Mojo::Base -base; |
|
34
|
|
|
|
|
92
|
|
|
34
|
|
|
|
|
153
|
|
5
|
34
|
|
|
34
|
|
5880
|
use Mojo::IOLoop; |
|
34
|
|
|
|
|
462650
|
|
|
34
|
|
|
|
|
225
|
|
6
|
34
|
|
|
34
|
|
1042
|
use Time::HiRes (); |
|
34
|
|
|
|
|
101
|
|
|
34
|
|
|
|
|
1235
|
|
7
|
34
|
|
50
|
34
|
|
193
|
use constant SERVER_DEBUG => $ENV{MOJO_REDIS_SERVER_DEBUG} || 0; |
|
34
|
|
|
|
|
69
|
|
|
34
|
|
|
|
|
19229
|
|
8
|
|
|
|
|
|
|
|
9
|
0
|
0
|
|
0
|
1
|
0
|
sub config { \%{shift->{config} || {}}; } |
|
0
|
|
|
|
|
0
|
|
10
|
|
|
|
|
|
|
has configure_environment => 1; |
11
|
78
|
100
|
|
78
|
1
|
2036
|
sub pid { shift->{pid} || 0; } |
12
|
1881
|
50
|
|
1881
|
1
|
29347
|
sub url { shift->{url} || ''; } |
13
|
|
|
|
|
|
|
|
14
|
27
|
|
|
27
|
1
|
345
|
sub singleton { state $server = shift->new; } |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub start { |
17
|
35
|
|
|
35
|
1
|
9113
|
my $self = _instance(shift); |
18
|
35
|
|
|
|
|
354
|
my %config = @_; |
19
|
35
|
|
|
|
|
80
|
my $cfg; |
20
|
|
|
|
|
|
|
|
21
|
35
|
50
|
33
|
|
|
148
|
return $self if $self->pid and kill 0, $self->pid; |
22
|
|
|
|
|
|
|
|
23
|
35
|
|
50
|
|
|
312
|
$config{bind} ||= '127.0.0.1'; |
24
|
35
|
|
50
|
|
|
361
|
$config{daemonize} ||= 'no'; |
25
|
35
|
|
50
|
|
|
251
|
$config{databases} ||= 16; |
26
|
35
|
|
50
|
|
|
210
|
$config{loglevel} ||= SERVER_DEBUG ? 'verbose' : 'warning'; |
27
|
35
|
|
33
|
|
|
675
|
$config{port} ||= Mojo::IOLoop::Server->generate_port; |
28
|
35
|
|
50
|
|
|
38241
|
$config{rdbchecksum} ||= 'no'; |
29
|
35
|
|
100
|
|
|
262
|
$config{requirepass} ||= ''; |
30
|
35
|
|
50
|
|
|
311
|
$config{stop_writes_on_bgsave_error} ||= 'no'; |
31
|
35
|
|
50
|
|
|
237
|
$config{syslog_enabled} ||= 'no'; |
32
|
|
|
|
|
|
|
|
33
|
35
|
|
|
|
|
424
|
$cfg = Mojo::Asset::File->new; |
34
|
35
|
|
100
|
|
|
1240
|
$self->{bin} = $ENV{REDIS_SERVER_BIN} || 'redis-server'; |
35
|
|
|
|
|
|
|
|
36
|
35
|
|
|
|
|
293
|
while (my ($key, $value) = each %config) { |
37
|
315
|
|
|
|
|
53457
|
$key =~ s!_!-!g; |
38
|
315
|
|
|
|
|
474
|
warn "[Redis::Server] config $key $value\n" if SERVER_DEBUG; |
39
|
315
|
100
|
|
|
|
1457
|
$cfg->add_chunk("$key $value\n") if length $value; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
35
|
|
|
|
|
3396
|
require Mojo::Redis2; |
43
|
|
|
|
|
|
|
|
44
|
35
|
|
|
|
|
173
|
$self->{parent_pid} = $$; |
45
|
35
|
100
|
|
|
|
54620
|
if ($self->{pid} = fork) { # parent |
46
|
19
|
|
|
|
|
945
|
$self->{config} = \%config; |
47
|
19
|
|
50
|
|
|
1050
|
$self->{url} = sprintf 'redis://x:%s@%s:%s/', map { $_ // '' } @config{qw( requirepass bind port )}; |
|
57
|
|
|
|
|
2562
|
|
48
|
19
|
|
|
|
|
853
|
$self->_wait_for_server_to_start; |
49
|
0
|
0
|
0
|
|
|
0
|
$ENV{MOJO_REDIS_URL} //= $self->{url} if $self->configure_environment; |
50
|
0
|
|
|
|
|
0
|
return $self; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# child |
54
|
34
|
|
|
34
|
|
308
|
no warnings; |
|
34
|
|
|
|
|
93
|
|
|
34
|
|
|
|
|
19827
|
|
55
|
16
|
|
|
|
|
2714
|
exec $self->{bin}, $cfg->path; |
56
|
0
|
|
|
|
|
0
|
exit $!; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub stop { |
60
|
5
|
|
|
5
|
1
|
49
|
my $self = _instance(shift); |
61
|
5
|
|
|
|
|
16
|
my $guard = 10; |
62
|
5
|
50
|
|
|
|
19
|
my $pid = $self->pid or return $self; |
63
|
|
|
|
|
|
|
|
64
|
5
|
|
|
|
|
36
|
while (--$guard > 0) { |
65
|
5
|
50
|
|
|
|
75
|
kill 15, $pid or last; |
66
|
0
|
|
|
|
|
0
|
Time::HiRes::usleep(100e3); |
67
|
0
|
|
|
|
|
0
|
waitpid $self->pid, 0; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
5
|
50
|
|
|
|
50
|
die "Could not kill $pid ($guard)" if kill 0, $pid; |
71
|
5
|
|
|
|
|
445
|
return $self; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
40
|
100
|
|
40
|
|
287
|
sub _instance { ref $_[0] ? $_[0] : $_[0]->singleton; } |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _wait_for_server_to_start { |
77
|
19
|
|
|
19
|
|
238
|
my $self = shift; |
78
|
19
|
|
|
|
|
168
|
my $guard = 100; |
79
|
19
|
|
|
|
|
180
|
my $e; |
80
|
|
|
|
|
|
|
|
81
|
19
|
|
|
|
|
405
|
while (--$guard) { |
82
|
1881
|
|
|
|
|
4405
|
local $@; |
83
|
1881
|
|
|
|
|
19063690
|
Time::HiRes::usleep(10e3); |
84
|
1881
|
50
|
|
|
|
15508
|
return if eval { Mojo::Redis2->new(url => $self->url)->ping }; |
|
1881
|
|
|
|
|
11773
|
|
85
|
1881
|
|
50
|
|
|
8468
|
$e = $@ || 'No idea why we cannot connect to Mojo::Redis2::Server'; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
19
|
50
|
33
|
|
|
308
|
if ($self->pid and waitpid $self->pid, 0) { |
89
|
19
|
|
|
|
|
379
|
my ($x, $s, $d) = ($? >> 8, $? & 127, $? & 128); |
90
|
19
|
|
|
|
|
1165
|
die "Failed to start $self->{bin}: exit=$x, signal=$s, dump=$d"; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
0
|
die $e; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
5
|
50
|
50
|
5
|
|
11431
|
sub DESTROY { $_[0]->stop if ($_[0]{parent_pid} // 0) == $$; } |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=encoding utf8 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 NAME |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Mojo::Redis2::Server - Start a test server |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 DESCRIPTION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
L is a class for starting an instances of the Redis |
109
|
|
|
|
|
|
|
server. The server is stopped when the instance of this class goes out of |
110
|
|
|
|
|
|
|
scope. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Note: This module is only meant for unit testing. It is not good enough for |
113
|
|
|
|
|
|
|
keeping a production server up and running at this point. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 SYNOPSIS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
use Mojo::Redis2::Server; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
{ |
120
|
|
|
|
|
|
|
my $server = Mojo::Redis2::Server->new; |
121
|
|
|
|
|
|
|
$server->start; |
122
|
|
|
|
|
|
|
# server runs here |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# server is stopped here |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 config |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
$hash_ref = $self->config; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Contains the full configuration of the Redis server. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head2 configure_environment |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$bool = $self->configure_environment; |
138
|
|
|
|
|
|
|
$self = $self->configure_environment($bool); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
L will set the C environment variable unless |
141
|
|
|
|
|
|
|
this attribute is set to false. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 pid |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$int = $self->pid; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
The pid of the Redis server. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 url |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
$str = $self->url; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Contains a value suitable for L. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 METHODS |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 singleton |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
$self = $class->singleton; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Returns the singleton which is used when L and L is called |
162
|
|
|
|
|
|
|
as class methods, instead of instance methods. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 start |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
$self = $self->start(%config); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
This method will try to start an instance of the Redis server or C |
169
|
|
|
|
|
|
|
trying. The input config is a key/value structure with valid Redis config |
170
|
|
|
|
|
|
|
file settings. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 stop |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$self = $self->stop; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Will stop a running Redis server or die trying. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Copyright (C) 2014, Jan Henning Thorsen |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
183
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 AUTHOR |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |