@@ -12,7 +12,23 @@ import {
1212} from "./result-storage.js" ;
1313
1414export class DiskBasedResultStorage implements AccuracyResultStorage {
15- async getAccuracyResult ( commitSHA : string , runId ?: string ) : Promise < AccuracyResult | null > {
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 > {
1632 const filePath = runId
1733 ? // If we have both commit and runId then we get the path for
1834 // specific file. Common case when saving prompt responses during an
@@ -23,6 +39,10 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
2339 // marked as successful.
2440 this . getAccuracyResultFilePath ( commitSHA , LATEST_ACCURACY_RUN_NAME ) ;
2541
42+ let releaseLock : ( ( ) => Promise < void > ) | undefined ;
43+ if ( preferExclusiveRead !== false ) {
44+ releaseLock = await lock ( filePath ) ;
45+ }
2646 try {
2747 const raw = await fs . readFile ( filePath , "utf8" ) ;
2848 return JSON . parse ( raw ) as AccuracyResult ;
@@ -31,14 +51,17 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
3151 return null ;
3252 }
3353 throw error ;
54+ } finally {
55+ await releaseLock ?.( ) ;
3456 }
3557 }
3658
3759 async updateRunStatus ( commitSHA : string , runId : string , status : AccuracyRunStatuses ) : Promise < void > {
3860 const resultFilePath = this . getAccuracyResultFilePath ( commitSHA , runId ) ;
39- const release = await lock ( resultFilePath , { retries : 10 } ) ;
61+ let releaseLock : ( ( ) => Promise < void > ) | undefined ;
4062 try {
41- const accuracyResult = await this . getAccuracyResult ( commitSHA , runId ) ;
63+ releaseLock = await lock ( resultFilePath , { retries : 10 } ) ;
64+ const accuracyResult = await this . getAccuracyResult ( commitSHA , runId , false ) ;
4265 if ( ! accuracyResult ) {
4366 throw new Error ( "Results not found!" ) ;
4467 }
@@ -62,7 +85,7 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
6285 ) ;
6386 throw error ;
6487 } finally {
65- await release ( ) ;
88+ await releaseLock ?. ( ) ;
6689 }
6790
6891 // This bit is important to mark the current run as the latest run for a
@@ -111,9 +134,10 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
111134 return ;
112135 }
113136
114- const releaseLock = await lock ( resultFilePath , { retries : 10 } ) ;
137+ let releaseLock : ( ( ) => Promise < void > ) | undefined ;
115138 try {
116- const accuracyResult = await this . getAccuracyResult ( commitSHA , runId ) ;
139+ releaseLock = await lock ( resultFilePath , { retries : 10 } ) ;
140+ const accuracyResult = await this . getAccuracyResult ( commitSHA , runId , false ) ;
117141 if ( ! accuracyResult ) {
118142 throw new Error ( "Expected at-least initial accuracy result to be present" ) ;
119143 }
0 commit comments