IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 8, 2009, 11:23:24 AM (17 years ago)
Author:
jhoblitt
Message:

improved error handling and logging of exceptions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r24350 r24354  
    8686
    8787    my $db_index = 0;
    88     die "key not defined" unless defined $key;
     88    $log->logdie("key not defined") unless defined $key;
    8989
    9090    # hash the key to select the correct database instance
     
    126126    # lookup database info
    127127    my $db_config = $config->db($db_index);
    128     die "can't find database configuration info for database # $db_index"
     128    $log->logdie("can't find database configuration info for db # $db_index")
    129129        unless $db_config;
    130130
     
    168168    my $self = shift;
    169169
     170    my $log     = $self->log;
     171    $log->debug( "entered - @_" );
     172
    170173    my ($key) = validate_pos(@_,
    171174        {
     
    174177    );
    175178
    176     my $log     = $self->log;
    177179    my $sql     = $self->sql;
    178180    my $config  = $self->config;
    179181
    180     die "key not defined" unless defined $key;
     182    $log->logdie("key not defined") unless defined $key;
    181183    my $db_index = $self->_db_index_for_key($key);
    182184
     
    229231            $vol_name = undef;
    230232        } else {
    231             die "$vol_name is not a valid volume name"
     233           $log->logdie("$vol_name is not a valid volume name");
    232234        }
    233235    }
     
    317319
    318320    # add new key to the cache
    319     $self->cache->set(id => $key->path, value => 1);
     321    $self->cache->set(id => $key->path, value => 1) if defined $self->cache;
    320322    $log->debug( "key added to cache" );
    321323
     
    559561    my $dbidx1 = $self->_db_index_for_key($key1);
    560562    my $dbidx2 = $self->_db_index_for_key($key2);
    561     die "cannot swap keys not stored on the same database" unless ($dbidx1 == $dbidx2);
     563    $log->logdie("cannot swap keys not stored on the same database")
     564        unless ($dbidx1 == $dbidx2);
    562565
    563566    my $dbh1 = $self->_db_for_index($dbidx1);
    564567    my $dbh2 = $self->_db_for_index($dbidx2);
    565     die "different db handles for the same db?" unless ($dbh1 == $dbh2);
     568    $log->logdie("different db handles for the same db?")
     569        unless ($dbh1 == $dbh2);
    566570
    567571    # order of operations for the swap with a single db is:
     
    703707            $vol_name = undef;
    704708        } else {
    705             die "$vol_name is not a valid volume name"
     709           $log->logdie("$vol_name is not a valid volume name");
    706710        }
    707711    }
     
    15861590            if ( $instances == 1 ) {
    15871591                # remove key from cache
    1588                 $self->cache->delete($key->path);
     1592                $self->cache->delete($key->path) if defined $self->cache;
    15891593
    15901594                # we just removed the last instance
     
    17471751        my $path = URI->new($inst)->path;
    17481752
    1749         _retry(sub { chmod($mode, $path) })
     1753        $self->_retry(sub { chmod($mode, $path) })
    17501754            or $log->logdie("can not chmod() $path: $!");
    17511755
     
    17541758        # write permissions.
    17551759        my $fh;
    1756         _retry(sub { open($fh, '<', $path) })
     1760        $self->_retry(sub { open($fh, '<', $path) })
    17571761            or $log->logdie("can not open() $path: $!");
    17581762
    17591763        # fsync(3c)
    1760         _retry(sub { $fh->sync() })
     1764        $self->_retry(sub { $fh->sync() })
    17611765            or $log->logdie("can not sync() $path: $!");
    17621766
    1763         _retry(sub { close($fh) })
     1767        $self->_retry(sub { close($fh) })
    17641768            or $log->logdie("can not close() $path: $!");
    17651769    }
     
    19071911
    19081912    # check cache first
    1909     my $cached = $self->cache->get($key->path);
     1913    my $cached = $self->cache->get($key->path) if defined $self->cache;
    19101914    if (defined $cached) {
    19111915        $log->debug( "key $key found in cache" );
     
    19341938        $log->debug( "key found in db" );
    19351939        # add key to cache
    1936         $self->cache->set(id => $key->path, value => 1);
     1940        $self->cache->set(id => $key->path, value => 1) if defined $self->cache;
    19371941        $log->debug( "key added to cache" );
    19381942        $log->debug( "leaving" );
     
    20132017        my $storage_filename = $self->_generate_storage_filename($key->path, $ins_id);
    20142018        unless (-d $storage_path) {
    2015             _retry(sub { mkpath([$storage_path], 0, 0775) })
    2016                 or die "can't create storage path: $storage_path";
     2019            $self->_retry(sub { mkpath([$storage_path], 0, 0775) })
     2020                or $log->logdie("can't create storage path: $storage_path");
    20172021        }
    20182022        # check to make sure at least the parent directory has the proper
    20192023        # permissions
    2020         my $mode = [_retry(sub { stat($storage_path) } )]->[2] & 07777;
     2024        my $mode = [$self->_retry(sub { stat($storage_path) } )]->[2] & 07777;
    20212025        unless ($mode == 0775) {
    20222026            $log->error("$storage_path has the wrong permissions of: 0", sprintf("%o", $mode));
    2023             _retry(sub { chmod(0775, $storage_path) })
     2027            $self->_retry(sub { chmod(0775, $storage_path) })
    20242028                or $log->logdie("can not chmod() $storage_path: $!");
    20252029        }
     
    20362040    if ($xattr) {
    20372041        my $path = $uri->file;
    2038         die "can not set xattr on $path: $!"
     2042        $log->logdie("can not set xattr on $path: $!")
    20392043            unless (setfattr($path, 'user.nebulous_key', $key->path));
    20402044    }
     
    20592063
    20602064    my $fh;
    2061     _retry(sub { open($fh, '>', $path) })
     2065    $self->_retry(sub { open($fh, '>', $path) })
    20622066        or $log->logdie("can not open() $path: $!");
    20632067
    20642068    # chmod before fsync() to make sure the changed perms hit the disk too
    2065     _retry(sub { chmod(0664, $path) })
     2069    $self->_retry(sub { chmod(0664, $path) })
    20662070        or $log->logdie("can not chmod() $path: $!");
    20672071
    2068     _retry(sub { $fh->sync() })
     2072    $self->_retry(sub { $fh->sync() })
    20692073        or $log->logdie("can not sync() $path: $!");
    20702074
    2071     _retry(sub { close($fh) })
     2075    $self->_retry(sub { close($fh) })
    20722076        or $log->logdie("can not close() $path: $!");
    20732077
     
    21072111sub _retry
    21082112{
     2113    my $self = shift;
     2114
     2115    my $log = $self->log;
     2116    $log->debug( "entered - @_" );
     2117
    21092118    my $func = shift;
    21102119
     
    21152124        };
    21162125        if ($@) {
    2117             die $@;
     2126            $log->logdie($@);
    21182127            sleep 1;
    21192128            next;
     
    21252134    # if the loop ended and $@ is set, rethrow the error
    21262135    if ($@) {
    2127         die $@;
     2136        $log->logdie($@);
    21282137    }
    21292138
Note: See TracChangeset for help on using the changeset viewer.