Skip to content

Commit cb085b8

Browse files
Add concept of 'operations' to track long-running tasks (like cloning)
1 parent f3fb235 commit cb085b8

File tree

5 files changed

+73
-13
lines changed

5 files changed

+73
-13
lines changed

packages/backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"devDependencies": {
1313
"@types/argparse": "^2.0.16",
1414
"@types/node": "^22.7.5",
15+
"@types/uuid": "^10.0.0",
1516
"json-schema-to-typescript": "^15.0.2",
1617
"tsx": "^4.19.1",
1718
"typescript": "^5.6.2"
@@ -21,6 +22,7 @@
2122
"argparse": "^2.0.1",
2223
"lowdb": "^7.0.1",
2324
"simple-git": "^3.27.0",
25+
"uuid": "^10.0.0",
2426
"winston": "^3.15.0"
2527
}
2628
}

packages/backend/src/db.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
import { JSONFilePreset } from "lowdb/node";
22
import { type Low } from "lowdb";
3-
import { AppContext, Repository } from "./types.js";
3+
import { AppContext, Operation, Repository } from "./types.js";
4+
import { v4 as uuid } from 'uuid';
45

56
type Schema = {
67
repos: {
78
[key: string]: Repository;
9+
},
10+
operations: {
11+
[key: string]: Operation;
812
}
913
}
1014

1115
export type Database = Low<Schema>;
1216

1317
export const loadDB = async (ctx: AppContext): Promise<Database> => {
14-
const db = await JSONFilePreset<Schema>(`${ctx.cachePath}/db.json`, { repos: {} });
18+
const db = await JSONFilePreset<Schema>(`${ctx.cachePath}/db.json`, { repos: {}, operations: {} });
1519
return db;
1620
}
21+
22+
export const updateOperation = async (id: string, data: Partial<Operation>, db: Database) => {
23+
db.data.operations[id] = {
24+
...db.data.operations[id],
25+
...data,
26+
}
27+
await db.write();
28+
}
29+
30+
export const createOperation = async (data: Operation, db: Database) => {
31+
const id = uuid();
32+
db.data.operations[id] = data;
33+
await db.write();
34+
return id;
35+
}
36+
1737
export const updateRepository = async (repoId: string, data: Partial<Repository>, db: Database) => {
1838
db.data.repos[repoId] = {
1939
...db.data.repos[repoId],

packages/backend/src/index.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getGitHubReposFromConfig } from "./github.js";
88
import { AppContext, Repository } from "./types.js";
99
import { cloneRepository, fetchRepository } from "./git.js";
1010
import { createLogger } from "./logger.js";
11-
import { Database, loadDB } from './db.js';
11+
import { createOperation, createRepository, Database, loadDB, updateOperation, updateRepository } from './db.js';
1212
import { measure } from "./utils.js";
1313
import { REINDEX_INTERVAL_MS, RESYNC_CONFIG_INTERVAL_MS } from "./constants.js";
1414

@@ -196,19 +196,38 @@ const syncConfig = async (configPath: string, db: Database, signal: AbortSignal,
196196

197197
logger.info(`Indexing ${repo.id}...`);
198198

199-
try {
200-
if (existsSync(repo.path)) {
201-
await fetchRepository(repo);
202-
} else {
203-
await cloneRepository(repo);
199+
if (existsSync(repo.path)) {
200+
await fetchRepository(repo);
201+
} else {
202+
const operationId = await createOperation({
203+
type: 'clone',
204+
progress: 0,
205+
status: 'running',
206+
repoId: repo.id,
207+
startedAtDate: new Date().toUTCString(),
208+
}, db);
209+
210+
try {
211+
await cloneRepository(repo, (progress) => {
212+
updateOperation(operationId, {
213+
progress: progress,
214+
}, db);
215+
});
216+
} catch (err: any) {
217+
console.error(err);
218+
await updateOperation(operationId, {
219+
status: 'failed',
220+
message: err.message,
221+
}, db);
222+
throw err;
204223
}
205224

206-
await indexRepository(repo, context);
207-
} catch (err: any) {
208-
// @todo : better error handling here..
209-
logger.error(err);
210-
continue;
225+
await updateOperation(operationId, {
226+
status: 'completed',
227+
}, db);
211228
}
229+
230+
await indexRepository(repo, context);
212231

213232
await db.update(({ repos }) => repos[repo.id].lastIndexedDate = new Date().toUTCString());
214233
logger.info(`Indexed ${repo.id}`);

packages/backend/src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11

2+
export type Operation = {
3+
type: 'clone',
4+
status: 'running' | 'completed' | 'failed',
5+
progress: number,
6+
repoId: string;
7+
startedAtDate: string;
8+
message?: string;
9+
}
10+
211
export type Repository = {
312
/**
413
* Name of the repository (e.g., 'sourcebot-dev/sourcebot')

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,11 @@
13241324
resolved "https://registry.yarnpkg.com/@types/triple-beam/-/triple-beam-1.3.5.tgz#74fef9ffbaa198eb8b588be029f38b00299caa2c"
13251325
integrity sha512-6WaYesThRMCl19iryMYP7/x2OVgCtbIVflDGFpWnb9irXI3UjYE4AzmYuiUKY1AJstGijoY+MgUszMgRxIYTYw==
13261326

1327+
"@types/uuid@^10.0.0":
1328+
version "10.0.0"
1329+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-10.0.0.tgz#e9c07fe50da0f53dc24970cca94d619ff03f6f6d"
1330+
integrity sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==
1331+
13271332
"@typescript-eslint/eslint-plugin@^8.3.0":
13281333
version "8.8.0"
13291334
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.8.0.tgz#b2b02a5447cdc885950eb256b3b8a97b92031bd3"
@@ -4599,6 +4604,11 @@ util-deprecate@^1.0.1, util-deprecate@^1.0.2:
45994604
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
46004605
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==
46014606

4607+
uuid@^10.0.0:
4608+
version "10.0.0"
4609+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-10.0.0.tgz#5a95aa454e6e002725c79055fd42aaba30ca6294"
4610+
integrity sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==
4611+
46024612
validate-npm-package-license@^3.0.1:
46034613
version "3.0.4"
46044614
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz#fc91f6b9c7ba15c857f4cb2c5defeec39d4f410a"

0 commit comments

Comments
 (0)