add timestamp calculation

This commit is contained in:
uinstinct 2025-07-10 20:47:44 +05:30
parent 69f035d263
commit a020a49f4b
3 changed files with 43 additions and 12 deletions

View file

@ -643,9 +643,21 @@ export class CodebaseIndexer {
);
}
/**
* We want to prevent sqlite concurrent write errors
* when there are 2 indexing happening from different windows.
* We want the other window to wait until the first window's indexing finishes.
* Incase the first window closes before indexing is finished,
* we want to unlock the IndexLock by checking the last timestamp.
*/
private async *waitForDBIndex(): AsyncGenerator<IndexingProgressUpdate> {
let foundLock = await IndexLock.isLocked();
while (foundLock?.locked) {
if ((Date.now() - foundLock.timestamp) / 1000 > 10) {
console.log(`${foundLock.dirs} is not being indexed... unlocking`);
await IndexLock.unlock();
break;
}
console.log(`indexing ${foundLock.dirs}`);
yield {
progress: 0,
@ -653,6 +665,7 @@ export class CodebaseIndexer {
status: "waiting",
};
await new Promise((resolve) => setTimeout(resolve, 1000));
foundLock = await IndexLock.isLocked();
}
}
@ -664,7 +677,13 @@ export class CodebaseIndexer {
for await (const update of this.waitForDBIndex()) {
this.updateProgress(update);
}
await IndexLock.lock(paths.join(", ")); // acquire the index lock to prevent multiple windows to start indexing
await IndexLock.lock(paths.join(", ")); // acquire the index lock to prevent multiple windows to begin indexing
const indexLockTimestampUpdateInterval = setInterval(
() => void IndexLock.updateTimestamp(),
5_000,
);
try {
for await (const update of this.refreshDirs(
paths,
@ -680,7 +699,10 @@ export class CodebaseIndexer {
console.log(`Failed refreshing codebase index directories: ${e}`);
await this.handleIndexingError(e);
}
await IndexLock.unlock(); // release the index lock
clearInterval(indexLockTimestampUpdateInterval); // interval will also be cleared when window closes before indexing is finished
await IndexLock.unlock();
// Directly refresh submenu items
if (this.messenger) {
this.messenger.send("refreshSubmenuItems", {

View file

@ -544,36 +544,45 @@ export function truncateSqliteLikePattern(input: string, safety: number = 100) {
}
export class IndexLock {
private static async getLockTableName() {
private static getLockTableName() {
return "indexing_lock";
}
static async isLocked(): Promise<
{ locked: boolean; dirs: string } | undefined | undefined
{ locked: boolean; dirs: string; timestamp: number } | undefined | undefined
> {
const db = await SqliteDb.get();
const lockTableName = await IndexLock.getLockTableName();
const row = (await db.get(
`SELECT locked, dirs FROM ${lockTableName} WHERE locked = ?`,
`SELECT locked, dirs, timestamp FROM ${IndexLock.getLockTableName()} WHERE locked = ?`,
true,
)) as { locked: boolean; dirs: string } | undefined;
)) as { locked: boolean; dirs: string; timestamp: number } | undefined;
return row;
}
static async lock(dirs: string) {
const db = await SqliteDb.get();
const lockTableName = await IndexLock.getLockTableName();
await db.run(
`INSERT INTO ${lockTableName} (locked, timestamp, dirs) VALUES (?, ?, ?)`,
`INSERT INTO ${IndexLock.getLockTableName()} (locked, timestamp, dirs) VALUES (?, ?, ?)`,
true,
Date.now(),
dirs,
);
}
static async updateTimestamp() {
const db = await SqliteDb.get();
await db.run(
`UPDATE ${IndexLock.getLockTableName()} SET timestamp = ? where locked = ?`,
Date.now(),
true,
);
}
static async unlock() {
const db = await SqliteDb.get();
const lockTableName = await IndexLock.getLockTableName();
await db.run(`DELETE FROM ${lockTableName} WHERE locked = ?`, true);
await db.run(
`DELETE FROM ${IndexLock.getLockTableName()} WHERE locked = ?`,
true,
);
}
}

View file

@ -111,7 +111,7 @@ function IndexingProgress() {
<div className="mt-4 flex flex-col">
<div className="mb-0 flex justify-between text-sm">
<IndexingProgressTitleText update={update} />
{["loading", "waiting"].includes(update.status) && (
{!["loading", "waiting"].includes(update.status) && (
<IndexingProgressIndicator update={update} />
)}
</div>