| 1 | package Ubic::Service::MongoDB; | ||||||||||||||||||||||||||||||||||
| 2 | 1 | $Ubic::Service::MongoDB::VERSION = '0.02'; | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: running MongoDB as Ubic service | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use parent qw(Ubic::Service::Common); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::Copy qw(move); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use File::Spec::Functions qw(catfile); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use MongoDB 0.502; # MonboDB::MongoClient instead of MongoDB::Connection | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Ubic::Daemon qw(:all); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Ubic::Result qw(:all); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 21 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 0 | my $opt_str = { type => SCALAR, optional => 1 }; | |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 0 | my $params = validate(@_, { | |||||||||||||||||||||||||||||||||
| 26 | config => { type => HASHREF }, | ||||||||||||||||||||||||||||||||||
| 27 | daemon => { type => SCALAR, | ||||||||||||||||||||||||||||||||||
| 28 | regex => qr/^mongo(d|s)$/, | ||||||||||||||||||||||||||||||||||
| 29 | default => 'mongod' }, | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | status => { type => CODEREF, optional => 1 }, | ||||||||||||||||||||||||||||||||||
| 32 | user => $opt_str, | ||||||||||||||||||||||||||||||||||
| 33 | ubic_log => $opt_str, | ||||||||||||||||||||||||||||||||||
| 34 | stdout => $opt_str, | ||||||||||||||||||||||||||||||||||
| 35 | stderr => $opt_str, | ||||||||||||||||||||||||||||||||||
| 36 | pidfile => $opt_str, | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | gen_cfg => $opt_str, | ||||||||||||||||||||||||||||||||||
| 39 | }); | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | 0 | my $self = $params; | |||||||||||||||||||||||||||||||||
| 42 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $self->{port} = $self->{config}->{port} || 27017; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'config'}{'port'} || 27017
MC/DC0%0/1
$self->{'config'}{'port'} || 27017
$self->{'config'}{'port'}
Truth table0%0/2
A
A= $self->{'config'}{'port'}
| |||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if (!$params->{pidfile}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 0 | $params->{pidfile} = "/tmp/$self->{daemon}." . $self->{port} . '.pid'; | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (!$params->{gen_cfg}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 50 | 0 | $params->{gen_cfg} = "/tmp/$self->{daemon}." . $self->{port} . '.conf'; | |||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 0 | return bless $params => $class; | |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub bin { | ||||||||||||||||||||||||||||||||
|
Subroutine
bin: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | 0 | my @cmd = ($self->{daemon}, '--config', $self->{gen_cfg}); | |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 0 | return \@cmd; | |||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub create_cfg_file { | ||||||||||||||||||||||||||||||||
|
Subroutine
create_cfg_file: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 65 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | 0 | my $fname = $self->{gen_cfg}; | |||||||||||||||||||||||||||||||||
| 68 | 0 | my $tmp_fname = $fname . ".tmp"; | |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | open(my $tmp_fh, '>', $tmp_fname) or die "Can't open file [$tmp_fname]: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 0 | foreach my $k (keys %{$self->{config}}) { | |||||||||||||||||||||||||||||||||
| 73 | 0 | my $v = $self->{config}->{$k}; | |||||||||||||||||||||||||||||||||
| 74 | 0 | print $tmp_fh "$k=$v\n"; | |||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | close($tmp_fh) or die "Can't close file [$tmp_fname]: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | move($tmp_fname, $fname) or die "Can't mova file [${tmp_fname}] to [$fname]: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub pidfile { | ||||||||||||||||||||||||||||||||
|
Subroutine
pidfile: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 82 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 0 | return $self->{pidfile}; | |||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub user { | ||||||||||||||||||||||||||||||||
|
Subroutine
user: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return $self->{user} if defined $self->{user}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 0 | return $self->SUPER::user; | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub timeout_options { | ||||||||||||||||||||||||||||||||
|
Subroutine
timeout_options: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 95 | return { | ||||||||||||||||||||||||||||||||||
| 96 | 0 | start => { trials => 15, step => 0.1 }, | |||||||||||||||||||||||||||||||||
| 97 | stop => { trials => 15, step => 0.1 } | ||||||||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub start_impl { | ||||||||||||||||||||||||||||||||
|
Subroutine
start_impl: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 102 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 0 | $self->create_cfg_file; | |||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | 0 | my $daemon_opts = { | |||||||||||||||||||||||||||||||||
| 107 | bin => $self->bin, | ||||||||||||||||||||||||||||||||||
| 108 | term_timeout => 5 | ||||||||||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | 0 | for (qw/ubic_log stdout stderr pidfile/) { | |||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $daemon_opts->{$_} = $self->{$_} if defined $self->{$_}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | 0 | start_daemon($daemon_opts); | |||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub stop_impl { | ||||||||||||||||||||||||||||||||
|
Subroutine
stop_impl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | 0 | return stop_daemon($self->pidfile, { timeout => 7 }); | |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub status_impl { | ||||||||||||||||||||||||||||||||
|
Subroutine
status_impl: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 124 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | 0 | my $running = check_daemon($self->pidfile); | |||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | return result('not running') unless ($running); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | 0 | my $status; | |||||||||||||||||||||||||||||||||
| 130 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 131 | 0 | my $client = MongoDB::MongoClient->new( | |||||||||||||||||||||||||||||||||
| 132 | host => "mongodb://localhost:$self->{port}", | ||||||||||||||||||||||||||||||||||
| 133 | timeout => 2000, | ||||||||||||||||||||||||||||||||||
| 134 | query_timeout => 2000, | ||||||||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||||||||
| 136 | 0 | $client->connect; | |||||||||||||||||||||||||||||||||
| 137 | 0 | my $db = $client->get_database('admin'); | |||||||||||||||||||||||||||||||||
| 138 | 0 | $status = $db->run_command({ serverStatus => 1 }); | |||||||||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 142 | 0 | return result('broken', $@); | |||||||||||||||||||||||||||||||||
| 143 | ▶ | } elsif (!$status || !$status->{ok} || $status->{ok} != 1) { | |||||||||||||||||||||||||||||||||
|
Condition0%0/6
not $status && $status->{"ok"} or $status->{"ok"} != 1
$status && $status->{'ok'}
MC/DC0%0/3
not $status && $status->{"ok"} or $status->{"ok"} != 1
$status
$status->{'ok'}
$status->{"ok"} != 1
Truth table0%0/4
not(A && B) or C
A= $statusB= $status->{'ok'}C= $status->{"ok"} != 1
| |||||||||||||||||||||||||||||||||||
| 144 | 0 | return result('broken'); | |||||||||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||||||||
| 146 | 0 | return result('running'); | |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||||||||