| 1 | package Daemonise::Plugin::Redis; | ||
| 2 | |||
| 3 | use Mouse::Role; | ||
| 4 | |||
| 5 | # ABSTRACT: Daemonise Redis plugin | ||
| 6 | |||
| 7 | use Redis; | ||
| 8 | use Try::Tiny; | ||
| 9 | use MIME::Base64 qw(encode_base64 decode_base64); | ||
| 10 | use Storable qw/nfreeze thaw/; | ||
| 11 | use Data::MessagePack; | ||
| 12 | |||
| 13 | |||
| 14 | has 'redis_host' => ( | ||
| 15 | is => 'rw', | ||
| 16 | isa => 'Str', | ||
| 17 | lazy => 1, | ||
| 18 | default => sub { 'localhost' }, | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | has 'redis_port' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'Int', | ||
| 25 | lazy => 1, | ||
| 26 | default => sub { 6379 }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | |||
| 30 | has 'redis_connect_timeout' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Int', | ||
| 33 | lazy => 1, | ||
| 34 | default => sub { 2 }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | |||
| 38 | has 'redis_connect_rate' => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Int', | ||
| 41 | lazy => 1, | ||
| 42 | default => sub { 500 }, | ||
| 43 | ); | ||
| 44 | |||
| 45 | |||
| 46 | has 'cache_default_expire' => ( | ||
| 47 | is => 'rw', | ||
| 48 | isa => 'Int', | ||
| 49 | lazy => 1, | ||
| 50 | default => sub { 600 }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | |||
| 54 | has 'redis' => ( | ||
| 55 | is => 'rw', | ||
| 56 | isa => 'Redis', | ||
| 57 | required => 1, | ||
| 58 | ); | ||
| 59 | |||
| 60 | |||
| 61 | has 'mp' => ( | ||
| 62 | is => 'rw', | ||
| 63 | isa => 'Data::MessagePack', | ||
| 64 | required => 1, | ||
| 65 | ); | ||
| 66 | |||
| 67 | |||
| 68 | after 'configure' => sub { | ||
| 69 | my ($self, $reconfig) = @_; | ||
| 70 | |||
| 71 | if ($reconfig) { | ||
| 72 | $self->log("closing redis connection") if $self->debug; | ||
| 73 | $self->redis->quit; | ||
| 74 | } | ||
| 75 | |||
| 76 | $self->log("configuring Redis plugin") if $self->debug; | ||
| 77 | |||
| 78 | if (ref($self->config->{redis}) eq 'HASH') { | ||
| 79 | foreach my $conf_key ('host', 'port', 'connect_timeout', 'connect_rate') | ||
| 80 | { | ||
| 81 | my $attr = "redis_" . $conf_key; | ||
| 82 | $self->$attr($self->config->{redis}->{$conf_key}) | ||
| 83 | if defined $self->config->{redis}->{$conf_key}; | ||
| 84 | } | ||
| 85 | $self->cache_default_expire($self->config->{redis}->{default_expire}) | ||
| 86 | if defined $self->config->{redis}->{default_expire}; | ||
| 87 | } | ||
| 88 | |||
| 89 | $self->redis( | ||
| 90 | Redis->new( | ||
| 91 | server => $self->redis_host . ':' . $self->redis_port, | ||
| 92 | reconnect => $self->redis_connect_timeout, | ||
| 93 | every => $self->redis_connect_rate, | ||
| 94 | debug => $self->debug, | ||
| 95 | )); | ||
| 96 | |||
| 97 | $self->mp( | ||
| 98 | Data::MessagePack->new( | ||
| 99 | prefer_integer => 1, | ||
| 100 | utf8 => 1, | ||
| 101 | )); | ||
| 102 | |||
| 103 | # don't try to lock() again when reconfiguring | ||
| 104 | return if $reconfig; | ||
| 105 | |||
| 106 | # lock cron for 24 hours | ||
| 107 | if ($self->is_cron) { | ||
| 108 | my $expire = $self->cache_default_expire; | ||
| 109 | $self->cache_default_expire(24 * 60 * 60); | ||
| 110 | die 'locking failed' unless $self->lock; | ||
| 111 | $self->cache_default_expire($expire); | ||
| 112 | |||
| 113 | $self->graph('cron.' . $self->name, 'started', 1) | ||
| 114 | if $self->can('graph'); | ||
| 115 | } | ||
| 116 | |||
| 117 | return; | ||
| 118 | }; | ||
| 119 | |||
| 120 | |||
| 121 | sub cache_get { | ||
| 122 | my ($self, $key) = @_; | ||
| 123 | |||
| 124 | my $value = $self->redis->get($key); | ||
| 125 | |||
| 126 | return unless defined $value; | ||
| 127 | |||
| 128 | my $data = try { | ||
| 129 | |||
| 130 | # first try decode using MessagePack | ||
| 131 | $self->mp->unpack($value); | ||
| 132 | } | ||
| 133 | catch { | ||
| 134 | $self->log("unpacking using MessagePack failed: $_") if $self->debug; | ||
| 135 | |||
| 136 | # then decode and thaw if it looks like a BASE64 encoding | ||
| 137 | if ($value =~ m~^[a-zA-Z0-9+/\n]+={0,2}\n$~s) { | ||
| 138 | return thaw(decode_base64($value)); | ||
| 139 | } | ||
| 140 | |||
| 141 | # finally assume it's plain text | ||
| 142 | else { | ||
| 143 | return $value; | ||
| 144 | } | ||
| 145 | }; | ||
| 146 | |||
| 147 | return $data; | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | sub cache_set { | ||
| 152 | my ($self, $key, $data, $expire) = @_; | ||
| 153 | |||
| 154 | my $scalar = $data; | ||
| 155 | |||
| 156 | # always use MessagePack because it's faster and more compact | ||
| 157 | # but leave plain scalars as is | ||
| 158 | if (ref $data) { | ||
| 159 | try { | ||
| 160 | $scalar = $self->mp->pack($data); | ||
| 161 | } | ||
| 162 | catch { | ||
| 163 | # TODO: convert/compile/do whatever necessary to not have perl | ||
| 164 | # objects in the $data hash. | ||
| 165 | # Data::MessagePack does not allow perl objects, but we appear | ||
| 166 | # to have Types::Serialiser in JSON structures | ||
| 167 | $scalar = encode_base64(nfreeze($data)); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | $self->redis->set($key => $scalar); | ||
| 172 | $self->redis->expire($key, ($expire || $self->cache_default_expire)); | ||
| 173 | |||
| 174 | return 1; | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | sub cache_del { | ||
| 179 | my ($self, $key); | ||
| 180 | |||
| 181 | return $self->redis->del($key); | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | sub lock { ## no critic (ProhibitBuiltinHomonyms) | ||
| 186 | my ($self, $key, $lock_value) = @_; | ||
| 187 | |||
| 188 | unless (ref \$key eq 'SCALAR') { | ||
| 189 | $self->log("locking failed: first argument is not of type SCALAR"); | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | |||
| 193 | if (defined $lock_value) { | ||
| 194 | unless (ref \$lock_value eq 'SCALAR') { | ||
| 195 | $self->log('locking failed: second argument is not of type SCALAR'); | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | my $lock = 'lock:' . ($key || $self->name); | ||
| 201 | |||
| 202 | # fallback to host:PID for the lock value | ||
| 203 | $lock_value //= $self->hostname . ':' . $$; | ||
| 204 | |||
| 205 | if (my $value = $self->redis->get($lock)) { | ||
| 206 | if ($value eq $lock_value) { | ||
| 207 | $self->redis->expire($lock, $self->cache_default_expire); | ||
| 208 | $self->log("$lock locking time extended for $value") | ||
| 209 | if $self->debug; | ||
| 210 | return 1; | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | $self->notify("$lock cannot acquire lock hold by $value") | ||
| 214 | if $self->can('notify'); | ||
| 215 | return; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | else { | ||
| 219 | $self->redis->set($lock => $lock_value); | ||
| 220 | $self->redis->expire($lock, $self->cache_default_expire); | ||
| 221 | $self->log("$lock lock acquired") if $self->debug; | ||
| 222 | return 1; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | sub unlock { | ||
| 228 | my ($self, $key, $lock_value) = @_; | ||
| 229 | |||
| 230 | unless (ref \$key eq 'SCALAR') { | ||
| 231 | $self->log("locking failed: first argument is not of type SCALAR"); | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | |||
| 235 | if (defined $lock_value) { | ||
| 236 | unless (ref \$lock_value eq 'SCALAR') { | ||
| 237 | $self->log('locking failed: second argument is not of type SCALAR'); | ||
| 238 | return; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | my $lock = 'lock:' . ($key || $self->name); | ||
| 243 | |||
| 244 | # fallback to PID for the lock value | ||
| 245 | $lock_value //= $self->hostname . ':' . $$; | ||
| 246 | |||
| 247 | if (my $value = $self->redis->get($lock)) { | ||
| 248 | if ($value eq $lock_value) { | ||
| 249 | $self->redis->del($lock); | ||
| 250 | $self->log("$lock lock released") if $self->debug; | ||
| 251 | return 1; | ||
| 252 | } | ||
| 253 | else { | ||
| 254 | $self->log("$lock lock hold by $value, permission denied"); | ||
| 255 | return; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | else { | ||
| 259 | $self->log("$lock lock was already released") if $self->debug; | ||
| 260 | return 1; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | sub DESTROY { | ||
| 265 | my ($self) = @_; | ||
| 266 | |||
| 267 | return unless ref $self; | ||
| 268 | return unless $self->{is_cron}; | ||
| 269 | |||
| 270 | # the Redis object was already destroyed to we need to create | ||
| 271 | # a new one with the existing config | ||
| 272 | $self->redis( | ||
| 273 | Redis->new( | ||
| 274 | server => $self->redis_host . ':' . $self->redis_port, | ||
| 275 | reconnect => $self->redis_connect_timeout, | ||
| 276 | every => $self->redis_connect_rate, | ||
| 277 | debug => $self->debug, | ||
| 278 | )); | ||
| 279 | |||
| 280 | $self->unlock; | ||
| 281 | |||
| 282 | $self->graph('cron.' . $self->name, 'stopped', 1) | ||
| 283 | if $self->can('graph'); | ||
| 284 | |||
| 285 | return; | ||
| 286 | } | ||
| 287 | |||
| 288 | 1; | ||
| 289 | |||
| 290 | __END__ |