@@ -12,23 +12,7 @@ import {
1212} from "./result-storage.js" ;
1313
1414export class DiskBasedResultStorage implements AccuracyResultStorage {
15- /**
16- *
17- * @param commitSHA The commit for which accuracy result needs to be
18- * fetched.
19- * @param runId An optional runId to get the result for. If the runId is not
20- * provided then the result of the latest run are fetched.
21- * @param preferExclusiveRead An optional flag, which when set to false,
22- * will not lock the result file before reading otherwise the default
23- * behavior is to lock the result file before reading. This should always be
24- * set to false when the calling context already holds the lock on the
25- * result file.
26- */
27- async getAccuracyResult (
28- commitSHA : string ,
29- runId ?: string ,
30- preferExclusiveRead ?: boolean
31- ) : Promise < AccuracyResult | null > {
15+ async getAccuracyResult ( commitSHA : string , runId ?: string ) : Promise < AccuracyResult | null > {
3216 const filePath = runId
3317 ? // If we have both commit and runId then we get the path for
3418 // specific file. Common case when saving prompt responses during an
@@ -39,27 +23,13 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
3923 // marked as successful.
4024 this . getAccuracyResultFilePath ( commitSHA , LATEST_ACCURACY_RUN_NAME ) ;
4125
42- let releaseLock : ( ( ) => Promise < void > ) | undefined ;
43- if ( preferExclusiveRead !== false ) {
44- releaseLock = await lock ( filePath ) ;
45- }
46- try {
47- const raw = await fs . readFile ( filePath , "utf8" ) ;
48- return JSON . parse ( raw ) as AccuracyResult ;
49- } catch ( error ) {
50- if ( ( error as NodeJS . ErrnoException ) . code === "ENOENT" ) {
51- return null ;
52- }
53- throw error ;
54- } finally {
55- await releaseLock ?.( ) ;
56- }
26+ return this . withFileLock < AccuracyResult | null > ( filePath , ( ) => this . getAccuracyResultWithoutLock ( filePath ) ) ;
5727 }
5828
5929 async updateRunStatus ( commitSHA : string , runId : string , status : AccuracyRunStatuses ) : Promise < void > {
6030 const resultFilePath = this . getAccuracyResultFilePath ( commitSHA , runId ) ;
6131 await this . withFileLock ( resultFilePath , async ( ) => {
62- const accuracyResult = await this . getAccuracyResult ( commitSHA , runId , false ) ;
32+ const accuracyResult = await this . getAccuracyResultWithoutLock ( resultFilePath ) ;
6333 if ( ! accuracyResult ) {
6434 throw new Error ( "Results not found!" ) ;
6535 }
@@ -126,7 +96,7 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
12696 }
12797
12898 await this . withFileLock ( resultFilePath , async ( ) => {
129- let accuracyResult = await this . getAccuracyResult ( commitSHA , runId , false ) ;
99+ let accuracyResult = await this . getAccuracyResultWithoutLock ( resultFilePath ) ;
130100 if ( ! accuracyResult ) {
131101 throw new Error ( "Expected at-least initial accuracy result to be present" ) ;
132102 }
@@ -161,6 +131,18 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
161131 return Promise . resolve ( ) ;
162132 }
163133
134+ private async getAccuracyResultWithoutLock ( filePath : string ) : Promise < AccuracyResult | null > {
135+ try {
136+ const raw = await fs . readFile ( filePath , "utf8" ) ;
137+ return JSON . parse ( raw ) as AccuracyResult ;
138+ } catch ( error ) {
139+ if ( ( error as NodeJS . ErrnoException ) . code === "ENOENT" ) {
140+ return null ;
141+ }
142+ throw error ;
143+ }
144+ }
145+
164146 private async ensureAccuracyResultFile (
165147 filePath : string ,
166148 initialData : string
@@ -183,11 +165,11 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
183165 }
184166 }
185167
186- private async withFileLock ( filePath : string , callback : ( ) => Promise < void > ) : Promise < void > {
168+ private async withFileLock < R > ( filePath : string , callback : ( ) => Promise < R > ) : Promise < R > {
187169 let releaseLock : ( ( ) => Promise < void > ) | undefined ;
188170 try {
189171 releaseLock = await lock ( filePath , { retries : 10 } ) ;
190- await callback ( ) ;
172+ return await callback ( ) ;
191173 } catch ( error ) {
192174 console . warn ( `Could not acquire lock for file - ${ filePath } .` , error ) ;
193175 throw error ;
0 commit comments