| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # server::redis Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Server::Redis; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::System::Process); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | datadir => [ qw(datadir) ], | ||
| 20 | version => [ qw(version) ], | ||
| 21 | conf_file => [ qw(file) ], | ||
| 22 | listen => [ qw(address) ], | ||
| 23 | port => [ qw(port) ], | ||
| 24 | pidfile => [ qw(pidfile) ], | ||
| 25 | log_file => [ qw(log_file) ], | ||
| 26 | }, | ||
| 27 | attributes_default => { | ||
| 28 | listen => '127.0.0.1', | ||
| 29 | port => 6379, | ||
| 30 | }, | ||
| 31 | commands => { | ||
| 32 | install => [ ], # Inherited | ||
| 33 | generate_conf => [ qw(conf|OPTIONAL port|OPTIONAL listen|OPTIONAL) ], | ||
| 34 | start => [ qw(port|OPTIONAL listen|OPTIONAL) ], | ||
| 35 | stop => [ ], | ||
| 36 | status => [ ], | ||
| 37 | }, | ||
| 38 | require_modules => { | ||
| 39 | 'Metabrik::File::Text' => [ ], | ||
| 40 | 'Metabrik::System::File' => [ ], | ||
| 41 | }, | ||
| 42 | require_binaries => { | ||
| 43 | 'redis-server' => [ ], | ||
| 44 | }, | ||
| 45 | need_packages => { | ||
| 46 | ubuntu => [ qw(redis-server) ], | ||
| 47 | debian => [ qw(redis-server) ], | ||
| 48 | kali => [ qw(redis-server) ], | ||
| 49 | freebsd => [ qw(redis) ], | ||
| 50 | }, | ||
| 51 | }; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub brik_use_properties { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | my $datadir = $self->datadir; | ||
| 58 | my $conf_file = $datadir.'/redis.conf'; | ||
| 59 | my $log_file = $datadir.'/redis.log'; | ||
| 60 | my $pidfile = $datadir.'/redis.pid'; | ||
| 61 | |||
| 62 | return { | ||
| 63 | attributes_default => { | ||
| 64 | conf_file => $conf_file, | ||
| 65 | log_file => $log_file, | ||
| 66 | pidfile => $pidfile, | ||
| 67 | }, | ||
| 68 | }; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub generate_conf { | ||
| 72 | my $self = shift; | ||
| 73 | my ($conf_file, $port, $listen) = @_; | ||
| 74 | |||
| 75 | $conf_file ||= $self->conf_file; | ||
| 76 | $port ||= $self->port; | ||
| 77 | $listen ||= $self->listen; | ||
| 78 | |||
| 79 | my $datadir = $self->datadir; | ||
| 80 | my $log_file = $self->log_file; | ||
| 81 | my $pidfile = $self->pidfile; | ||
| 82 | |||
| 83 | my $lib_dir = 'var/lib/redis'; | ||
| 84 | |||
| 85 | my $sf = Metabrik::System::File->new_from_brik_init($self) or return; | ||
| 86 | $sf->mkdir($datadir.'/'.$lib_dir) or return; | ||
| 87 | |||
| 88 | my $dir = $self->datadir.'/'.$lib_dir; | ||
| 89 | |||
| 90 | my $params = { | ||
| 91 | "daemonize" => "yes", | ||
| 92 | "bind" => $listen, | ||
| 93 | "dir" => $dir, | ||
| 94 | "logfile" => $log_file, | ||
| 95 | "pidfile" => $pidfile, | ||
| 96 | "port" => $port, | ||
| 97 | "client-output-buffer-limit" => [ | ||
| 98 | 'normal 0 0 0', | ||
| 99 | 'slave 256mb 64mb 60', | ||
| 100 | 'pubsub 32mb 8mb 60', | ||
| 101 | ], | ||
| 102 | "databases" => 16, | ||
| 103 | "activerehashing" => "yes", | ||
| 104 | "aof-load-truncated" => "yes", | ||
| 105 | "aof-rewrite-incremental-fsync" => "yes", | ||
| 106 | "appendfilename" => "\"appendonly.aof\"", | ||
| 107 | "appendfsync" => "everysec", | ||
| 108 | "appendonly" => "no", | ||
| 109 | "auto-aof-rewrite-min-size" => "64mb", | ||
| 110 | "auto-aof-rewrite-percentage" => 100, | ||
| 111 | "dbfilename" => "dump.rdb", | ||
| 112 | "hash-max-ziplist-entries" => 512, | ||
| 113 | "hash-max-ziplist-value" => 64, | ||
| 114 | "hll-sparse-max-bytes" => 3000, | ||
| 115 | "hz" => 10, | ||
| 116 | "latency-monitor-threshold" => 0, | ||
| 117 | "list-max-ziplist-entries" => 512, | ||
| 118 | "list-max-ziplist-value" => 64, | ||
| 119 | "loglevel" => "notice", | ||
| 120 | "lua-time-limit" => 5000, | ||
| 121 | "no-appendfsync-on-rewrite" => "no", | ||
| 122 | "notify-keyspace-events" => "\"\"", | ||
| 123 | "rdbchecksum" => "yes", | ||
| 124 | "rdbcompression" => "yes", | ||
| 125 | "repl-disable-tcp-nodelay" => "no", | ||
| 126 | "repl-diskless-sync" => "no", | ||
| 127 | "repl-diskless-sync-delay" => 5, | ||
| 128 | "save" => 60, | ||
| 129 | "set-max-intset-entries" => 512, | ||
| 130 | "slave-priority" => 100, | ||
| 131 | "slave-read-only" => "yes", | ||
| 132 | "slave-serve-stale-data" => "yes", | ||
| 133 | "slowlog-log-slower-than" => 10000, | ||
| 134 | "slowlog-max-len" => 128, | ||
| 135 | "stop-writes-on-bgsave-error" => "yes", | ||
| 136 | "tcp-backlog" => 511, | ||
| 137 | "tcp-keepalive" => 0, | ||
| 138 | "timeout" => 0, | ||
| 139 | "zset-max-ziplist-entries" => 128, | ||
| 140 | "zset-max-ziplist-value" => 64, | ||
| 141 | }; | ||
| 142 | |||
| 143 | my @lines = (); | ||
| 144 | for my $k (keys %$params) { | ||
| 145 | if (ref($params->{$k}) eq 'ARRAY') { | ||
| 146 | for my $this (@{$params->{$k}}) { | ||
| 147 | push @lines, "$k $this"; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | push @lines, "$k ".$params->{$k}; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 156 | $ft->append(0); | ||
| 157 | $ft->overwrite(1); | ||
| 158 | $ft->write(\@lines, $conf_file) or return; | ||
| 159 | |||
| 160 | return $conf_file; | ||
| 161 | } | ||
| 162 | |||
| 163 | # | ||
| 164 | # redis-server --port 9999 --slaveof 127.0.0.1 6379 | ||
| 165 | # redis-server /etc/redis/6379.conf --loglevel debug | ||
| 166 | # | ||
| 167 | sub start { | ||
| 168 | my $self = shift; | ||
| 169 | my ($port, $listen) = @_; | ||
| 170 | |||
| 171 | if ($self->status) { | ||
| 172 | return $self->info_process_is_running; | ||
| 173 | } | ||
| 174 | |||
| 175 | $port ||= $self->port; | ||
| 176 | $listen ||= $self->listen; | ||
| 177 | |||
| 178 | my $conf_file = $self->conf_file; | ||
| 179 | $self->brik_help_run_file_not_found('start', $conf_file) or return; | ||
| 180 | |||
| 181 | my $cmd = "redis-server $conf_file"; | ||
| 182 | if ($port) { | ||
| 183 | $cmd .= " --port $port"; | ||
| 184 | } | ||
| 185 | if ($listen) { | ||
| 186 | $cmd .= " --bind $listen"; | ||
| 187 | } | ||
| 188 | if ($self->log->level > 2) { | ||
| 189 | $cmd .= " --loglevel debug"; | ||
| 190 | } | ||
| 191 | |||
| 192 | return $self->system($cmd); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub stop { | ||
| 196 | my $self = shift; | ||
| 197 | |||
| 198 | if (! $self->status) { | ||
| 199 | return $self->info_process_is_not_running; | ||
| 200 | } | ||
| 201 | |||
| 202 | my $pidfile = $self->pidfile; | ||
| 203 | |||
| 204 | return $self->kill_from_pidfile($pidfile); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub status { | ||
| 208 | my $self = shift; | ||
| 209 | |||
| 210 | my $pidfile = $self->pidfile; | ||
| 211 | |||
| 212 | if ($self->is_running_from_pidfile($pidfile)) { | ||
| 213 | $self->verbose_process_is_running; | ||
| 214 | return 1; | ||
| 215 | } | ||
| 216 | |||
| 217 | $self->verbose_process_is_not_running; | ||
| 218 | return 0; | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; | ||
| 222 | |||
| 223 | __END__ | ||