Ubic

 view release on metacpan or  search on metacpan

lib/Ubic/Daemon/PidState.pm  view on Meta::CPAN

package Ubic::Daemon::PidState;
$Ubic::Daemon::PidState::VERSION = '1.60';
use strict;
use warnings;

# ABSTRACT: internal object representing process info stored on disk


use Params::Validate qw(:all);
use Ubic::Lockf;
use Ubic::AtomicFile;

use overload '""' => sub {
    my $self = shift;
    return $self->{dir};
};

sub new {
    my $class = shift;
    my ($dir) = validate_pos(@_, { type => SCALAR });
    return bless { dir => $dir } => $class;
}

sub is_empty {
    my ($self) = validate_pos(@_, 1);
    my $dir = $self->{dir};

    return if not -d $dir and -s $dir; # old-style pidfile
    return if -d $dir and -s "$dir/pid"; # non-empty new-style pidfile

    return 1;
}

sub init {
    my ($self) = validate_pos(@_, 1);
    my $dir = $self->{dir};
    if (-e $dir and not -d $dir) {
        print "converting $dir to dir\n";
        unlink $dir or die "Can't unlink $dir: $!";
    }
    unless (-d $dir) {
        mkdir $dir or die "Can't create $dir: $!";
    }

}

sub read {
    my ($self) = validate_pos(@_, 1);

    my $dir = $self->{dir};

    my $content;
    my $parse_content = sub {
        if ($content =~ /\A pid \s+ (\d+) \n guid \s+ (.+) (?: \n daemon \s+ (\d+) )? \Z/x) {
            # new format
            return { pid => $1, guid => $2, daemon => $3, format => 'new' };
        }
        elsif ($content eq '') {
            # file is empty, probably lost after reboot - we didn't sync() pidfile to disk in old versions
            return;
        }
        else {
            # We consider invalid pidfile content as the fatal, unrecoverable error.
            # Please report all cases of invalid pidfile as critical issues.
            #
            # (This policy will be changed if ubic become popular enough for this to annoy enough people,
            # but collecting bugreports about weird stuff happening is more important by now.)
            die "invalid pidfile content in pidfile $dir";
        }
    };
    if (-d $dir) {
        # pidfile as dir
        my $open_success = open my $fh, '<', "$dir/pid";
        unless ($open_success) {
            if ($!{ENOENT}) {
                return; # pidfile not found, daemon is not running
            }
            else {
                die "Failed to open '$dir/pid': $!";
            }
        }

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.564 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )