Skip to content

Commit

Permalink
don't depend on version when computing cached data dir for node
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Dec 21, 2016
1 parent cac9aa4 commit fc66ce1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
27 changes: 11 additions & 16 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,16 @@ function getNodeCachedDataDir() {
return Promise.resolve();
}

function ensureDir(dir) {
return new Promise(function (resolve, reject) {
fs.mkdir(dir, function (err) {
if (err && err.code !== 'EEXIST') {
reject(err);
} else {
resolve(dir);
}
});
});
}
var dir = path.join(app.getPath('userData'), 'CachedData');

var basedir = path.join(app.getPath('userData'), 'CachedData');
return ensureDir(basedir).then(function () {
var version = require(path.join(__dirname, '../package.json')).version;
var cachedDataDir = path.join(basedir, version);
return ensureDir(cachedDataDir);
return new Promise(function (resolve, reject) {
fs.mkdir(dir, function (err) {
if (err && err.code !== 'EEXIST') {
reject(err);
} else {
resolve(dir);
}
});
});
}

Expand Down Expand Up @@ -177,6 +170,8 @@ global.getOpenUrls = function () {
};


// use '<UserData>/CachedData'-directory to store
// node/v8 cached data.
var nodeCachedDataDir = getNodeCachedDataDir().then(function (value) {
process.env['VSCODE_NODE_CACHED_DATA_DIR_' + process.pid] = value;
});
Expand Down
2 changes: 1 addition & 1 deletion src/vs/platform/environment/node/environmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class EnvironmentService implements IEnvironmentService {
get sharedIPCHandle(): string { return `${getIPCHandlePrefix()}-${pkg.version}-shared${getIPCHandleSuffix()}`; }

@memoize
get nodeCachedDataDir(): string { return path.join(this.userDataPath, 'CachedData', pkg.version); }
get nodeCachedDataDir(): string { return path.join(this.userDataPath, 'CachedData'); }

constructor(private _args: ParsedArgs, private _execPath: string) { }
}
Expand Down
26 changes: 15 additions & 11 deletions src/vs/workbench/electron-browser/nodeCachedDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { onUnexpectedError } from 'vs/base/common/errors';
import { TPromise } from 'vs/base/common/winjs.base';
import { dirname, basename, join } from 'vs/base/common/paths';
import { readdir, rimraf } from 'vs/base/node/pfs';
import { join } from 'vs/base/common/paths';
import { readdir, rimraf, stat } from 'vs/base/node/pfs';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';

Expand Down Expand Up @@ -53,9 +53,9 @@ export class NodeCachedDataManager {
}

private _manageCachedDataSoon(): void {
// Cached data is stored as user data in directories like `CachedData/1.8.0`.
// This function makes sure to delete cached data from previous versions,
// like `CachedData/1.7.2`.
// Cached data is stored as user data and we run a cleanup task everytime
// the editor starts. The strategy is to delete all files that are older than
// 3 months

const {nodeCachedDataDir} = this._environmentService;
if (!nodeCachedDataDir) {
Expand All @@ -65,15 +65,19 @@ export class NodeCachedDataManager {
let handle = setTimeout(() => {
handle = undefined;

const nodeCachedDataDirname = dirname(nodeCachedDataDir);
const nodeCachedDataBasename = basename(nodeCachedDataDir);
readdir(nodeCachedDataDir).then(entries => {

readdir(nodeCachedDataDirname).then(entries => {
const now = Date.now();
const limit = 1000 * 60 * 60 * 24 * 30 * 3; // roughly 3 months

const deletes = entries.map(entry => {
if (entry !== nodeCachedDataBasename) {
return rimraf(join(nodeCachedDataDirname, entry));
}
const path = join(nodeCachedDataDir, entry);
return stat(path).then(stats => {
const diff = now - stats.mtime.getTime();
if (diff > limit) {
return rimraf(path);
}
});
});

return TPromise.join(deletes);
Expand Down

0 comments on commit fc66ce1

Please sign in to comment.