| 1 | package Ubic::Ping::Service; | ||
| 2 | $Ubic::Ping::Service::VERSION = '1.59'; | ||
| 3 | # ABSTRACT: ubic.ping service | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Ubic::Service::Common; | ||
| 9 | use Ubic::Daemon qw(:all); | ||
| 10 | use Ubic::Result qw(result); | ||
| 11 | use Ubic::UA; | ||
| 12 | use POSIX; | ||
| 13 | use Time::HiRes qw(sleep); | ||
| 14 | |||
| 15 | use Ubic::Settings; | ||
| 16 | |||
| 17 | use Config; | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my $port = $ENV{UBIC_SERVICE_PING_PORT} || 12345; | ||
| 21 | my $pidfile = Ubic::Settings->data_dir."/ubic-ping.pid"; | ||
| 22 | my $log = $ENV{UBIC_SERVICE_PING_LOG} || '/dev/null'; | ||
| 23 | |||
| 24 | my $perl = $Config{perlpath}; | ||
| 25 | |||
| 26 | Ubic::Service::Common->new({ | ||
| 27 | start => sub { | ||
| 28 | my $pid; | ||
| 29 | start_daemon({ | ||
| 30 | bin => qq{$perl -MUbic::Ping -e 'Ubic::Ping->new($port)->run;'}, | ||
| 31 | name => 'ubic.ping', | ||
| 32 | pidfile => $pidfile, | ||
| 33 | stdout => $log, | ||
| 34 | stderr => $log, | ||
| 35 | ubic_log => $log, | ||
| 36 | }); | ||
| 37 | }, | ||
| 38 | stop => sub { | ||
| 39 | stop_daemon($pidfile); | ||
| 40 | }, | ||
| 41 | status => sub { | ||
| 42 | my $daemon = check_daemon($pidfile); | ||
| 43 | unless ($daemon) { | ||
| 44 | return 'not running'; | ||
| 45 | } | ||
| 46 | my $ua = Ubic::UA->new(timeout => 1); | ||
| 47 | my $response = $ua->get("http://127.0.0.1:$port/ping"); | ||
| 48 | if ($response->{error}) { | ||
| 49 | return result('broken', $response->{error}); | ||
| 50 | } | ||
| 51 | if ($response->{body} =~ /^ok$/ and $response->{code} == 200) { | ||
| 52 | return result('running', "pid ".$daemon->pid); | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | return result('broken', $response->{body}); | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | port => $port, | ||
| 59 | timeout_options => { start => { step => 0.1, trials => 8 }}, | ||
| 60 | }); | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | __END__ |