@@ -12,23 +12,7 @@ import {
12
12
} from "./result-storage.js" ;
13
13
14
14
export 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 > {
32
16
const filePath = runId
33
17
? // If we have both commit and runId then we get the path for
34
18
// specific file. Common case when saving prompt responses during an
@@ -39,27 +23,13 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
39
23
// marked as successful.
40
24
this . getAccuracyResultFilePath ( commitSHA , LATEST_ACCURACY_RUN_NAME ) ;
41
25
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 ) ) ;
57
27
}
58
28
59
29
async updateRunStatus ( commitSHA : string , runId : string , status : AccuracyRunStatuses ) : Promise < void > {
60
30
const resultFilePath = this . getAccuracyResultFilePath ( commitSHA , runId ) ;
61
31
await this . withFileLock ( resultFilePath , async ( ) => {
62
- const accuracyResult = await this . getAccuracyResult ( commitSHA , runId , false ) ;
32
+ const accuracyResult = await this . getAccuracyResultWithoutLock ( resultFilePath ) ;
63
33
if ( ! accuracyResult ) {
64
34
throw new Error ( "Results not found!" ) ;
65
35
}
@@ -126,7 +96,7 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
126
96
}
127
97
128
98
await this . withFileLock ( resultFilePath , async ( ) => {
129
- let accuracyResult = await this . getAccuracyResult ( commitSHA , runId , false ) ;
99
+ let accuracyResult = await this . getAccuracyResultWithoutLock ( resultFilePath ) ;
130
100
if ( ! accuracyResult ) {
131
101
throw new Error ( "Expected at-least initial accuracy result to be present" ) ;
132
102
}
@@ -161,6 +131,18 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
161
131
return Promise . resolve ( ) ;
162
132
}
163
133
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
+
164
146
private async ensureAccuracyResultFile (
165
147
filePath : string ,
166
148
initialData : string
@@ -183,11 +165,11 @@ export class DiskBasedResultStorage implements AccuracyResultStorage {
183
165
}
184
166
}
185
167
186
- private async withFileLock ( filePath : string , callback : ( ) => Promise < void > ) : Promise < void > {
168
+ private async withFileLock < R > ( filePath : string , callback : ( ) => Promise < R > ) : Promise < R > {
187
169
let releaseLock : ( ( ) => Promise < void > ) | undefined ;
188
170
try {
189
171
releaseLock = await lock ( filePath , { retries : 10 } ) ;
190
- await callback ( ) ;
172
+ return await callback ( ) ;
191
173
} catch ( error ) {
192
174
console . warn ( `Could not acquire lock for file - ${ filePath } .` , error ) ;
193
175
throw error ;
0 commit comments