Skip to content
This repository was archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
refactor(): add ipfs junk files handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshyx committed Apr 28, 2017
1 parent 0f83d4a commit c19427a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ addons:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
- golang-go
- g++-4.8
4 changes: 3 additions & 1 deletion src/IpfsConnector.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export declare class IpfsConnector extends EventEmitter {
process: boolean;
version: string;
};
private _isRetry;
private _callbacks;
private _api;
constructor(enforcer: any);
Expand All @@ -38,10 +39,11 @@ export declare class IpfsConnector extends EventEmitter {
checkExecutable(): Promise<any>;
start(): Promise<{}>;
private _start(binPath);
private _cleanupFile(filePath);
private _attachStartingEvents();
private _flushStartingEvents();
private _pipeStd();
stop(): this;
stop(): Promise<this>;
private _init();
staticGetPorts(retry?: boolean): any;
staticSetPorts(ports: {
Expand Down
60 changes: 56 additions & 4 deletions src/IpfsConnector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="../typings/main.d.ts"/>
import { homedir } from 'os';
import { stat, unlink } from 'fs';
import * as Promise from 'bluebird';
import { IpfsBin, version as requiredVersion } from './IpfsBin';
import { IpfsApiHelper } from './IpfsApiHelper';
Expand All @@ -14,6 +15,8 @@ options.extra.env = Object.assign(process.env, { IPFS_PATH: path.join(homedir(),
const symbolEnforcer = Symbol();
const symbol = Symbol();
const ROOT_OPTION = 'Addresses';
const LOCK_FILE = 'repo.lock';
const API_FILE = 'api';

export class IpfsConnector extends EventEmitter {
private process: childProcess.ChildProcess;
Expand All @@ -25,6 +28,7 @@ export class IpfsConnector extends EventEmitter {
api: false,
version: ''
};
private _isRetry = false;
private _callbacks = new Map();
private _api: IpfsApiHelper;

Expand All @@ -48,6 +52,16 @@ export class IpfsConnector extends EventEmitter {
return this.emit(events.IPFS_INITING);
}

if (data.includes('acquire lock') && !this._isRetry) {
return this
.stop()
.then(() => this._cleanupFile(path.join(this.options.extra.env.IPFS_PATH, LOCK_FILE)))
.then(() => {
this._isRetry = true;
return this.start();
})
}

this.serviceStatus.process = false;
/**
* @event IpfsConnector#SERVICE_FAILED
Expand Down Expand Up @@ -225,6 +239,7 @@ export class IpfsConnector extends EventEmitter {
);
this.once(events.ERROR, reject);
this.once(events.SERVICE_STARTED, () => {
this._isRetry = false;
this._flushStartingEvents();
this.removeListener(events.ERROR, reject);
resolve(this.api);
Expand All @@ -234,6 +249,35 @@ export class IpfsConnector extends EventEmitter {
});
}

/**
*
* @param filePath
* @returns {Bluebird}
* @private
*/
private _cleanupFile(filePath: string) {
return new Promise((resolve, reject) => {
return stat(filePath, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
return resolve(true);
}
return reject(err);
}

if (stats.isFile()) {
return unlink(filePath, (error) => {
if (error) {
return reject(error);
}
return resolve(true);
})
}
return resolve(true);
})
});
}

/**
* Filter daemon startup log
* @private
Expand Down Expand Up @@ -278,8 +322,8 @@ export class IpfsConnector extends EventEmitter {
}

/**
* Stop ipfs daemon
* @returns {IpfsConnector}
*
* @returns {Bluebird<IpfsConnector>}
*/
public stop() {
this.emit(events.SERVICE_STOPPING);
Expand All @@ -290,10 +334,10 @@ export class IpfsConnector extends EventEmitter {
this.process.kill();
this.process = null;
this.serviceStatus.process = false;
return this;
return Promise.delay(1000).then(() => this);
}
this.emit(events.SERVICE_STOPPED);
return this;
return Promise.delay(1000).then(() => this);
}

/**
Expand Down Expand Up @@ -327,8 +371,16 @@ export class IpfsConnector extends EventEmitter {
Gateway: string,
Swarm: string []
};
let apiFile: string;
if (error) {
this.logger.error(error);
if (!retry) {
apiFile = path.join(this.options.extra.env.IPFS_PATH, API_FILE);
return resolve(
Promise.delay(10).then(() => this._cleanupFile(apiFile))
.then(() => this.staticGetPorts(true))
)
}
return reject(error);
}
if (stderr.includes('ipfs init')) {
Expand Down
11 changes: 6 additions & 5 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ describe('IpfsConnector', function () {
});

it('gets ports without an api', function () {
instance.stop();
return instance.staticGetPorts().then((ports) => {

return instance.stop().then(() => instance.staticGetPorts()).then((ports) => {
expect(ports.api).to.exist;
});
});
Expand All @@ -336,9 +336,10 @@ describe('IpfsConnector', function () {
});
});
after(function (done) {
instance.stop();
rimraf(binTarget, function () {
done();
instance.stop().then(() => {
rimraf(binTarget, function () {
done();
});
});
});
});
Expand Down

0 comments on commit c19427a

Please sign in to comment.