remove broken cache files if found, fixes #27
This commit is contained in:
		
							parent
							
								
									44ce942d97
								
							
						
					
					
						commit
						7ab6453b1f
					
				@ -145,13 +145,12 @@ TritonApi.prototype._cacheGetJson = function _cacheGetJson(key, cb) {
 | 
				
			|||||||
    assert.func(cb, 'cb');
 | 
					    assert.func(cb, 'cb');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var keyPath = path.resolve(this.cacheDir, key);
 | 
					    var keyPath = path.resolve(this.cacheDir, key);
 | 
				
			||||||
    fs.exists(keyPath, function (exists) {
 | 
					 | 
				
			||||||
        if (!exists) {
 | 
					 | 
				
			||||||
            self.log.trace({keyPath: keyPath}, 'cache file does not exist');
 | 
					 | 
				
			||||||
            return cb();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    fs.readFile(keyPath, 'utf8', function (err, data) {
 | 
					    fs.readFile(keyPath, 'utf8', function (err, data) {
 | 
				
			||||||
            if (err) {
 | 
					        if (err && err.code === 'ENOENT') {
 | 
				
			||||||
 | 
					            self.log.trace({keyPath: keyPath},
 | 
				
			||||||
 | 
					                'cache file does not exist');
 | 
				
			||||||
 | 
					            return cb();
 | 
				
			||||||
 | 
					        } else if (err) {
 | 
				
			||||||
            self.log.warn({err: err, keyPath: keyPath},
 | 
					            self.log.warn({err: err, keyPath: keyPath},
 | 
				
			||||||
                'error reading cache file');
 | 
					                'error reading cache file');
 | 
				
			||||||
            return cb();
 | 
					            return cb();
 | 
				
			||||||
@ -160,13 +159,19 @@ TritonApi.prototype._cacheGetJson = function _cacheGetJson(key, cb) {
 | 
				
			|||||||
        try {
 | 
					        try {
 | 
				
			||||||
            obj = JSON.parse(data);
 | 
					            obj = JSON.parse(data);
 | 
				
			||||||
        } catch (dataErr) {
 | 
					        } catch (dataErr) {
 | 
				
			||||||
                self.log.warn({err: dataErr, keyPath: keyPath},
 | 
					            self.log.trace({err: dataErr, keyPath: keyPath},
 | 
				
			||||||
                    'error parsing JSON cache file');
 | 
					                'error parsing JSON cache file, removing');
 | 
				
			||||||
                return cb();
 | 
					            fs.unlink(keyPath, function (err2) {
 | 
				
			||||||
 | 
					                if (err2) {
 | 
				
			||||||
 | 
					                    self.log.warn({err: err2},
 | 
				
			||||||
 | 
					                        'failed to remove JSON cache file');
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                cb(err2);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        cb(null, obj);
 | 
					        cb(null, obj);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -252,7 +257,7 @@ TritonApi.prototype.getImage = function getImage(name, cb) {
 | 
				
			|||||||
    assert.string(name, 'name');
 | 
					    assert.string(name, 'name');
 | 
				
			||||||
    assert.func(cb, 'cb');
 | 
					    assert.func(cb, 'cb');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (common.UUID_RE.test(name)) {
 | 
					    if (common.isUUID(name)) {
 | 
				
			||||||
        this.cloudapi.getImage({id: name}, function (err, img) {
 | 
					        this.cloudapi.getImage({id: name}, function (err, img) {
 | 
				
			||||||
            if (err) {
 | 
					            if (err) {
 | 
				
			||||||
                cb(err);
 | 
					                cb(err);
 | 
				
			||||||
@ -313,7 +318,7 @@ TritonApi.prototype.getPackage = function getPackage(name, cb) {
 | 
				
			|||||||
    assert.string(name, 'name');
 | 
					    assert.string(name, 'name');
 | 
				
			||||||
    assert.func(cb, 'cb');
 | 
					    assert.func(cb, 'cb');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (common.UUID_RE.test(name)) {
 | 
					    if (common.isUUID(name)) {
 | 
				
			||||||
        this.cloudapi.getPackage({id: name}, function (err, pkg) {
 | 
					        this.cloudapi.getPackage({id: name}, function (err, pkg) {
 | 
				
			||||||
            if (err) {
 | 
					            if (err) {
 | 
				
			||||||
                cb(err);
 | 
					                cb(err);
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user