Signed-off-by: bnechyporenko <bnechyporenko@bol.com>
This commit is contained in:
bnechyporenko
2024-06-25 11:43:45 +02:00
committed by blam
parent a190944647
commit 47b7ac5ade
4 changed files with 30 additions and 13 deletions
@@ -55,7 +55,7 @@ export class GcpBucketWorkspaceProvider implements WorkspaceProvider {
const fileCloud = this.storage
.bucket(this.getGcpBucketName())
.file(options.taskId);
const workspace = await serializeWorkspace(options.path);
const { contents: workspace } = await serializeWorkspace(options);
try {
await fileCloud.save(workspace, {
contentType: 'application/x-tar',
@@ -80,8 +80,8 @@ export class GcpBucketWorkspaceProvider implements WorkspaceProvider {
const file = bucket.file(options.taskId);
const result = await file.exists();
if (result[0]) {
const workspace = getRawBody(file.createReadStream());
await restoreWorkspace(options.targetPath, await workspace);
const workspace = await getRawBody(file.createReadStream());
await restoreWorkspace({ path: options.targetPath, buffer: workspace });
}
}
@@ -520,7 +520,10 @@ export class DatabaseTaskStore implements TaskStore {
.where({ id: options.taskId })
.select('workspace');
await restoreWorkspace(options.targetPath, result.workspace);
await restoreWorkspace({
path: options.targetPath,
buffer: result.workspace,
});
}
async cleanWorkspace({ taskId }: { taskId: string }): Promise<void> {
@@ -537,7 +540,7 @@ export class DatabaseTaskStore implements TaskStore {
await this.db<RawDbTaskRow>('tasks')
.where({ id: options.taskId })
.update({
workspace: await serializeWorkspace(options.path),
workspace: (await serializeWorkspace(options)).contents,
});
}
}
@@ -72,8 +72,11 @@ describe('serializer', () => {
const restoredWorkspaceDir = createMockDirectory();
it('should be able to archive and restore the workspace', async () => {
const workspaceBuffer = await serializeWorkspace(workspaceDir.path);
await restoreWorkspace(restoredWorkspaceDir.path, workspaceBuffer);
const workspaceBuffer = await serializeWorkspace(workspaceDir);
await restoreWorkspace({
path: restoredWorkspaceDir.path,
buffer: workspaceBuffer.contents,
});
expect(
fs.existsSync(`${restoredWorkspaceDir.path}/\$\{ESCAPE_ME\}.txt`),
@@ -23,20 +23,31 @@ const pipeline = promisify(pipelineCb);
/**
* Serializes provided path into tar archive
*
* @public
* @alpha
*/
export const serializeWorkspace = async (path: string): Promise<Buffer> => {
return new Promise<Buffer>(async resolve => {
await pipeline(tar.create({ cwd: path }, ['']), concatStream(resolve));
export const serializeWorkspace = async (opts: {
path: string;
}): Promise<{ contents: Buffer }> => {
return new Promise<{ contents: Buffer }>(async resolve => {
await pipeline(
tar.create({ cwd: opts.path }, ['']),
concatStream(buffer => {
return resolve({ contents: buffer });
}),
);
});
};
/**
* Rehydrates the provided buffer of tar archive into the provide destination path
*
* @public
* @alpha
*/
export const restoreWorkspace = async (path: string, buffer?: Buffer) => {
export const restoreWorkspace = async (opts: {
path: string;
buffer?: Buffer;
}): Promise<void> => {
const { buffer, path } = opts;
if (buffer) {
await pipeline(
Readable.from(buffer),