File: FileStatus.pm

package info (click to toggle)
mha4mysql-manager 0.58-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,116 kB
  • sloc: perl: 10,103; sh: 2,012; makefile: 9; sql: 4
file content (124 lines) | stat: -rw-r--r-- 3,075 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

package MHA::FileStatus;

use strict;
use warnings FATAL => 'all';

use English qw(-no_match_vars);
use Carp qw(croak);
use File::Basename;
use MHA::ManagerConst;
use MHA::NodeUtil;

sub new {
  my $class = shift;
  my $self  = {
    conffile    => undef,
    dir         => undef,
    status_file => undef,
    basename    => undef,
    master_host => undef,
    @_,
  };
  return bless $self, $class;
}

sub init($) {
  my $self = shift;
  unless ( $self->{basename} ) {
    $self->{basename} = $self->get_basename();
  }
  unless ( $self->{status_file} ) {
    $self->{status_file} =
      "$self->{dir}/" . $self->{basename} . ".master_status.health";
  }
}

sub set_master_host($$) {
  my $self        = shift;
  my $master_host = shift;
  $self->{master_host} = $master_host;
}

# utility function. getting basename from conf file
sub get_basename($) {
  my $self     = shift;
  my $basename = basename( $self->{conffile} );
  $basename =~ s/(.*)\..*?$/$1/;
  if ( $basename eq '' ) {
    $basename = $self->{conffile};
  }
  return $basename;
}

sub update_status {
  my $self          = shift;
  my $status_string = shift;
  my $file          = $self->{status_file};
  my $master_host   = $self->{master_host};
  my $out;
  if ( -f $file ) {
    open( $out, "+<", $file ) or croak "$!:$file";
    flock( $out, 2 );
    truncate( $out, 0 );
    seek( $out, 0, 0 );
  }
  else {
    open( $out, ">", $file ) or croak "$!:$file";
  }
  print $out "$$\t$status_string";
  print $out "\tmaster:$master_host" if ($master_host);
  close($out);
}

sub read_status {
  my $self = shift;
  my $file = $self->{status_file};
  open( my $in, "+<", $file ) or croak "$!:$file";
  flock( $in, 2 );
  my $line          = readline($in);
  my @values        = split( /\t/, $line );
  my $pid           = $values[0];
  my $status_string = $values[1];
  my $master_info;

  if ( $values[2] ) {
    $master_info = $values[2];
  }
  close($in);
  return ( $pid, $status_string, $master_info );
}

sub update_status_time {
  my $self          = shift;
  my $status_string = shift;
  my $file          = $self->{status_file};
  my $master_host   = $self->{master_host};
  if ( !-f $file ) {
    $self->update_status($status_string);
  }
  else {
    my $ftime = time;
    utime $ftime, $ftime, $file;
  }
}

1;