| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Ubic::Service::Memcached; | 
| 2 |  |  |  |  |  |  | BEGIN { | 
| 3 | 1 |  |  | 1 |  | 22593 | $Ubic::Service::Memcached::VERSION = '2.02'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 8 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 7 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | # ABSTRACT: memcached as ubic service | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  | 1 |  | 646 | use parent qw(Ubic::Service::Skeleton); | 
|  | 1 |  |  |  |  | 309 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 13 |  |  |  |  |  |  | use Ubic::Daemon qw(:all); | 
| 14 |  |  |  |  |  |  | use Ubic::Result qw(result); | 
| 15 |  |  |  |  |  |  | use Cache::Memcached; | 
| 16 |  |  |  |  |  |  | use Carp; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | use Params::Validate qw(:all); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | use Morpheus '/module/Ubic/Service/Memcached' => [ | 
| 21 |  |  |  |  |  |  | 'pid_dir' => '?$PID_DIR', | 
| 22 |  |  |  |  |  |  | ]; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub new { | 
| 25 |  |  |  |  |  |  | my $class = shift; | 
| 26 |  |  |  |  |  |  | my $params = validate(@_, { | 
| 27 |  |  |  |  |  |  | port => { type => SCALAR, regex => qr/^\d+$/ }, | 
| 28 |  |  |  |  |  |  | pidfile => { type => SCALAR, optional => 1 }, | 
| 29 |  |  |  |  |  |  | maxsize => { type => SCALAR, regex => qr/^\d+$/, default => 640 }, | 
| 30 |  |  |  |  |  |  | verbose => { type => SCALAR, optional => 1 }, | 
| 31 |  |  |  |  |  |  | max_connections => { type => SCALAR, optional => 1 }, | 
| 32 |  |  |  |  |  |  | logfile => { type => SCALAR, optional => 1 }, | 
| 33 |  |  |  |  |  |  | ubic_log => { type => SCALAR, optional => 1 }, | 
| 34 |  |  |  |  |  |  | user => { type => SCALAR, default => 'root' }, | 
| 35 |  |  |  |  |  |  | group => { type => SCALAR, optional => 1}, | 
| 36 |  |  |  |  |  |  | other_argv => { type => SCALAR, optional => 1 }, | 
| 37 |  |  |  |  |  |  | }); | 
| 38 |  |  |  |  |  |  | if (not defined $params->{pidfile}) { | 
| 39 |  |  |  |  |  |  | unless (defined $PID_DIR) { | 
| 40 |  |  |  |  |  |  | croak "pidfile parameter not defined, define it or set /module/Ubic/Service/Memcached/pid_dir configuration option"; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | $params->{pidfile} = "$PID_DIR/$params->{port}.pid"; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | return bless $params => $class; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub start_impl { | 
| 48 |  |  |  |  |  |  | my $self = shift; | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | my $params = []; | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | push @$params, "-u $self->{user}" if $self->{user} eq 'root'; | 
| 53 |  |  |  |  |  |  | push @$params, "-p $self->{port}"; | 
| 54 |  |  |  |  |  |  | push @$params, "-m $self->{maxsize}"; | 
| 55 |  |  |  |  |  |  | push @$params, "-c $self->{max_connections}" if defined $self->{max_connections}; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | my $verbose = $self->{verbose}; | 
| 58 |  |  |  |  |  |  | if (defined $verbose) { | 
| 59 |  |  |  |  |  |  | if ($verbose == 1) { | 
| 60 |  |  |  |  |  |  | push @$params, "-v"; | 
| 61 |  |  |  |  |  |  | } elsif ($verbose > 1) { | 
| 62 |  |  |  |  |  |  | push @$params, "-vv"; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | push @$params, $self->{other_argv} if defined $self->{other_argv}; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | my $params_str = join " ", @$params; | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | start_daemon({ | 
| 71 |  |  |  |  |  |  | bin => "/usr/bin/memcached $params_str", | 
| 72 |  |  |  |  |  |  | pidfile => $self->{pidfile}, | 
| 73 |  |  |  |  |  |  | ($self->{logfile} ? | 
| 74 |  |  |  |  |  |  | ( | 
| 75 |  |  |  |  |  |  | stdout => $self->{logfile}, | 
| 76 |  |  |  |  |  |  | stderr => $self->{logfile}, | 
| 77 |  |  |  |  |  |  | ) : () | 
| 78 |  |  |  |  |  |  | ), | 
| 79 |  |  |  |  |  |  | ($self->{ubic_log} ? (ubic_log => $self->{ubic_log}) : ()), | 
| 80 |  |  |  |  |  |  | }); | 
| 81 |  |  |  |  |  |  | return result('starting'); | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub stop_impl { | 
| 85 |  |  |  |  |  |  | my $self = shift; | 
| 86 |  |  |  |  |  |  | stop_daemon($self->{pidfile}); | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub timeout_options { | 
| 90 |  |  |  |  |  |  | { start => { step => 0.1, trials => 10 } }; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | sub _is_available { | 
| 94 |  |  |  |  |  |  | my $self = shift; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | # using undocumented function here; Cache::Memcached caches unavailable hosts, | 
| 97 |  |  |  |  |  |  | # so without this call restart fails (at least on debian etch) | 
| 98 |  |  |  |  |  |  | Cache::Memcached->forget_dead_hosts(); | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | # TODO - this can fail if memcached binds only to specific interface | 
| 101 |  |  |  |  |  |  | my $client = Cache::Memcached->new({ servers => ["127.0.0.1:$self->{port}"] }); | 
| 102 |  |  |  |  |  |  | my $key = 'Ubic::Service::Memcached-testkey'; | 
| 103 |  |  |  |  |  |  | $client->set($key, 1); | 
| 104 |  |  |  |  |  |  | my $value = $client->get($key); | 
| 105 |  |  |  |  |  |  | $client->disconnect_all; # Cache::Memcached tries to reuse dead socket otherwise | 
| 106 |  |  |  |  |  |  | return $value; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub status_impl { | 
| 110 |  |  |  |  |  |  | my $self = shift; | 
| 111 |  |  |  |  |  |  | if (check_daemon($self->{pidfile})) { | 
| 112 |  |  |  |  |  |  | if ($self->_is_available) { | 
| 113 |  |  |  |  |  |  | return 'running'; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  | else { | 
| 116 |  |  |  |  |  |  | return 'broken'; | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  | else { | 
| 120 |  |  |  |  |  |  | return 'not running'; | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub user { | 
| 125 |  |  |  |  |  |  | my $self = shift; | 
| 126 |  |  |  |  |  |  | return $self->{user}; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | sub group { | 
| 130 |  |  |  |  |  |  | my $self = shift; | 
| 131 |  |  |  |  |  |  | my $groups = $self->{group}; | 
| 132 |  |  |  |  |  |  | return $self->SUPER::group() if not defined $groups; | 
| 133 |  |  |  |  |  |  | return @$groups if ref $groups eq 'ARRAY'; | 
| 134 |  |  |  |  |  |  | return $groups; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | sub port { | 
| 138 |  |  |  |  |  |  | my $self = shift; | 
| 139 |  |  |  |  |  |  | return $self->{port}; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | 1; | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | __END__ |