Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: File cache no check for the last modified returned from the server #520

Merged
merged 8 commits into from
Jul 5, 2017
Next Next commit
File cache now include the last modified timestamp
  • Loading branch information
timotheeguerin committed Jun 27, 2017
commit bcf5477eabf7ab4b4aaccefdb8f9afc0eeb6070d
46 changes: 36 additions & 10 deletions app/services/file/file-loader.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import * as path from "path";
import { Observable } from "rxjs";

import { File } from "app/models";
import { log } from "app/utils";
import { FileSystemService } from "../fs.service";

export type PropertiesFunc = () => Observable<File>;
@@ -44,6 +45,7 @@ export class FileLoader {
private _fs: FileSystemService;
private _properties: PropertiesFunc;
private _content: ContentFunc;
private _cachedProperties: File;

constructor(config: FileLoaderConfig) {
this.filename = config.filename;
@@ -54,8 +56,20 @@ export class FileLoader {
this._fs = config.fs;
}

public properties(): Observable<File> {
return this._properties();
public properties(forceNew = false): Observable<File> {
if (!forceNew && this._cachedProperties) {
return Observable.of(this._cachedProperties);
}
const obs = this._properties();
obs.subscribe({
next: (file) => {
this._cachedProperties = file;
},
error: (error) => {
log.error("Error getting the file properties!", error);
},
});
return obs;
}

public content(options: FileLoadOptions = {}): Observable<FileLoadResult> {
@@ -75,13 +89,25 @@ export class FileLoader {
* @returns observable that resolve the path of the cached file when done caching
*/
public cache(): Observable<string> {
const destination = path.join(this._fs.commonFolders.temp, this.source, this.groupId, this.filename);
return Observable.fromPromise(this._fs.exists(destination)).flatMap((exists) => {
if (exists) {
return Observable.of(destination);
} else {
return this.download(destination);
}
}).share();
return this.properties().cascade((file: File) => {
const filename = this._hashFilename(file);
const destination = path.join(this._fs.commonFolders.temp, this.source, this.groupId, filename);
console.log("Destination will be", destination);
return Observable.fromPromise(this._fs.exists(destination)).flatMap((exists) => {
if (exists) {
return Observable.of(destination);
} else {
return this.download(destination);
}
}).share();
});
}

private _hashFilename(file: File) {
const hash = file.properties.lastModified.getTime().toString(36);
const segements = file.name.split(/[\\\/]/);
const filename = segements.pop();
segements.push(`${hash}.${filename}`);
return path.join(...segements);
}
}
41 changes: 17 additions & 24 deletions src/client/api/batch-client-proxy/fileProxy.ts
Original file line number Diff line number Diff line change
@@ -37,18 +37,8 @@ export default class FileProxy {
this.client.file.getPropertiesFromComputeNode(poolId, nodeId, filename, wrapOptions(options),
(error, result, request, response) => {
if (error) { return reject(error); }
const headers = response.headers;
const out = {
name: filename,
isDirectory: headers["ocp-batch-file-isdirectory"],
url: headers["ocp-batch-file-url"],
properties: {
contentLength: parseInt(headers["content-length"] as string, 10),
contentType: headers["content-type"],
creationTime: headers["ocp-creation-time"],
lastModified: headers["lastModified"],
},
};
const out = this._parseHeadersToFile(response.headers, filename);

resolve({
data: out,
});
@@ -86,18 +76,7 @@ export default class FileProxy {
this.client.file.getPropertiesFromTask(jobId, taskId, filename, wrapOptions(options),
(error, result, request, response) => {
if (error) { return reject(error); }
const headers = response.headers;
const out = {
name: filename,
isDirectory: headers["ocp-batch-file-isdirectory"],
url: headers["ocp-batch-file-url"],
properties: {
contentLength: parseInt(headers["content-length"] as string, 10),
contentType: headers["content-type"],
creationTime: headers["ocp-creation-time"],
lastModified: headers["lastModified"],
},
};
const out = this._parseHeadersToFile(response.headers, filename);
resolve({
data: out,
});
@@ -140,4 +119,18 @@ export default class FileProxy {
[poolId, nodeId],
wrapOptions({ recursive, fileListFromComputeNodeOptions: options }));
}

private _parseHeadersToFile(headers, filename: string) {
return {
name: filename,
isDirectory: headers["ocp-batch-file-isdirectory"],
url: headers["ocp-batch-file-url"],
properties: {
contentLength: parseInt(headers["content-length"] as string, 10),
contentType: headers["content-type"],
creationTime: headers["ocp-creation-time"],
lastModified: headers["last-modified"],
},
};
}
}