@@ -12,7 +12,23 @@ import {
12
12
} from "./result-storage.js" ;
13
13
14
14
export 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 > {
16
32
const filePath = runId
17
33
? // If we have both commit and runId then we get the path for
18
34
// specific file. Common case when saving prompt responses during an
@@ -23,6 +39,10 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
23
39
// marked as successful.
24
40
this . getAccuracyResultFilePath ( commitSHA , LATEST_ACCURACY_RUN_NAME ) ;
25
41
42
+ let releaseLock : ( ( ) => Promise < void > ) | undefined ;
43
+ if ( preferExclusiveRead !== false ) {
44
+ releaseLock = await lock ( filePath ) ;
45
+ }
26
46
try {
27
47
const raw = await fs . readFile ( filePath , "utf8" ) ;
28
48
return JSON . parse ( raw ) as AccuracyResult ;
@@ -31,14 +51,17 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
31
51
return null ;
32
52
}
33
53
throw error ;
54
+ } finally {
55
+ await releaseLock ?.( ) ;
34
56
}
35
57
}
36
58
37
59
async updateRunStatus ( commitSHA : string , runId : string , status : AccuracyRunStatuses ) : Promise < void > {
38
60
const resultFilePath = this . getAccuracyResultFilePath ( commitSHA , runId ) ;
39
- const release = await lock ( resultFilePath , { retries : 10 } ) ;
61
+ let releaseLock : ( ( ) => Promise < void > ) | undefined ;
40
62
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 ) ;
42
65
if ( ! accuracyResult ) {
43
66
throw new Error ( "Results not found!" ) ;
44
67
}
@@ -62,7 +85,7 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
62
85
) ;
63
86
throw error ;
64
87
} finally {
65
- await release ( ) ;
88
+ await releaseLock ?. ( ) ;
66
89
}
67
90
68
91
// This bit is important to mark the current run as the latest run for a
@@ -111,9 +134,10 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
111
134
return ;
112
135
}
113
136
114
- const releaseLock = await lock ( resultFilePath , { retries : 10 } ) ;
137
+ let releaseLock : ( ( ) => Promise < void > ) | undefined ;
115
138
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 ) ;
117
141
if ( ! accuracyResult ) {
118
142
throw new Error ( "Expected at-least initial accuracy result to be present" ) ;
119
143
}
0 commit comments