Skip to content

Commit 6d41162

Browse files
authored
feat: StorageReader: Publish ClaimsNotDownloaded (#613)
1 parent d1ee925 commit 6d41162

File tree

10 files changed

+72
-8
lines changed

10 files changed

+72
-8
lines changed

src/Configuration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ describe('loadConfigurationWithDefaults', async (assert: any) => {
117117
exchangeIpfsHashTxId: 'myPrefix.IPFS_HASH_TX_ID',
118118
exchangePoetAnchorDownloaded: 'myPrefix.POET_ANCHOR_DOWNLOADED',
119119
exchangeClaimsDownloaded: 'myPrefix.CLAIMS_DOWNLOADED',
120+
exchangeClaimsNotDownloaded: 'myPrefix.CLAIMS_NOT_DOWNLOADED',
120121
exchangeStorageWriterStoreNextClaim: 'myPrefix.STORAGE_WRITER::STORE_NEXT_CLAIM',
121122
exchangeGetHealth: 'myPrefix.HEALTH::GET_HEALTH',
122123
}

src/Configuration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface ExchangeConfiguration {
7272
readonly exchangeIpfsHashTxId: string
7373
readonly exchangePoetAnchorDownloaded: string
7474
readonly exchangeClaimsDownloaded: string
75+
readonly exchangeClaimsNotDownloaded: string
7576
readonly exchangeStorageWriterStoreNextClaim: string
7677
readonly exchangeGetHealth: string
7778
}
@@ -130,6 +131,7 @@ const defaultConfiguration: Configuration = {
130131
exchangeIpfsHashTxId: 'IPFS_HASH_TX_ID',
131132
exchangePoetAnchorDownloaded: 'POET_ANCHOR_DOWNLOADED',
132133
exchangeClaimsDownloaded: 'CLAIMS_DOWNLOADED',
134+
exchangeClaimsNotDownloaded: 'CLAIMS_NOT_DOWNLOADED',
133135
exchangeStorageWriterStoreNextClaim: 'STORAGE_WRITER::STORE_NEXT_CLAIM',
134136
exchangeGetHealth: 'HEALTH::GET_HEALTH',
135137
}
@@ -172,6 +174,7 @@ const applyExchangePrefix = (configVars: any) => {
172174
'exchangeIpfsHashTxId',
173175
'exchangePoetAnchorDownloaded',
174176
'exchangeClaimsDownloaded',
177+
'exchangeClaimsNotDownloaded',
175178
'exchangeStorageWriterStoreNextClaim',
176179
'exchangeGetHealth',
177180
]

src/Interfaces.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ export interface ClaimIdIPFSHashPair {
1616
readonly claimId: string
1717
readonly ipfsFileHash: string
1818
}
19+
20+
export interface IPFSHashFailure {
21+
readonly ipfsFileHash: string
22+
readonly failureType: string
23+
readonly failureReason: string
24+
readonly failureTime: number
25+
}
26+
const hasFailureType = has('failureType')
27+
const hasFailureReason = has('failureReason')
28+
const hasFailureTime = has('failureTime')
29+
export const isIPFSHashFailure = allPass([hasIPFSFileHash, hasFailureReason, hasFailureType, hasFailureTime])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface ExchangeConfiguration {
22
readonly poetAnchorDownloaded?: string
33
readonly claimsDownloaded?: string
4+
readonly claimsNotDownloaded?: string
45
}

src/Messaging/Messaging.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* tslint:disable:no-console */
22
import { Connection, connect, Channel } from 'amqplib'
33

4-
import { ClaimIPFSHashPair, isClaimIPFSHashPair } from 'Interfaces'
4+
import { ClaimIPFSHashPair, isClaimIPFSHashPair, IPFSHashFailure } from 'Interfaces'
55

66
import { ExchangeConfiguration } from './ExchangeConfiguration'
77
import { BlockDownloaded, isBlockDownloaded } from './Messages'
@@ -101,4 +101,8 @@ export class Messaging {
101101
consume(claimIPFSHashPairs)
102102
})
103103
}
104+
105+
publishClaimsNotDownloaded = async (ipfsHashFailure: ReadonlyArray<IPFSHashFailure>) => {
106+
return this.publish(this.exchanges.claimsNotDownloaded, ipfsHashFailure)
107+
}
104108
}

src/StorageReader/ClaimController.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ export class ClaimController {
9696
},
9797
)
9898

99+
const publishEntryFailureReason = async (
100+
ipfsFileHash: string,
101+
failureType: FailureType,
102+
failureReason: FailureReason,
103+
failureTime: number
104+
) => {
105+
const logger = this.logger.child({ method: 'publishEntryFailureReason' })
106+
logger.trace('started publishing')
107+
108+
await this.messaging.publishClaimsNotDownloaded([
109+
{
110+
ipfsFileHash,
111+
failureType,
112+
failureReason,
113+
failureTime,
114+
},
115+
])
116+
logger.trace('finished publishing')
117+
}
118+
99119
const pipe = pipeP(
100120
this.findEntryToDownload,
101121
this.updateEntryAttempts,
@@ -107,13 +127,26 @@ export class ClaimController {
107127

108128
const handleErrors = async (error: Error) => {
109129
if (error instanceof NoMoreEntriesException) logger.trace(error.message)
110-
else if (error instanceof InvalidClaim)
130+
else if (error instanceof InvalidClaim) {
111131
await updateEntryFailureReason(error.ipfsFileHash, FailureType.Hard, error.failureReason)
112-
else if (error instanceof IPFSTimeoutError)
132+
await publishEntryFailureReason(error.ipfsFileHash, FailureType.Hard, error.failureReason, error.failureTime)
133+
} else if (error instanceof IPFSTimeoutError) {
113134
await updateEntryFailureReason(error.ipfsFileHash, FailureType.Soft, FailureReason.IPFSTimeout)
114-
else if (error instanceof IPFSGenericError) {
135+
await publishEntryFailureReason(
136+
error.ipfsFileHash,
137+
FailureType.Soft,
138+
FailureReason.IPFSTimeout,
139+
error.failureTime
140+
)
141+
} else if (error instanceof IPFSGenericError) {
115142
logger.warn({ error })
116143
await updateEntryFailureReason(error.ipfsFileHash, FailureType.Soft, FailureReason.IPFSGeneric)
144+
await publishEntryFailureReason(
145+
error.ipfsFileHash,
146+
FailureType.Soft,
147+
FailureReason.IPFSGeneric,
148+
error.failureTime
149+
)
117150
} else throw error
118151
}
119152

src/StorageReader/Exceptions.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,37 @@ export class NoMoreEntriesException extends Error {
1212
export class InvalidClaim extends Error {
1313
readonly ipfsFileHash: string
1414
readonly failureReason: FailureReason
15+
readonly failureTime?: number
1516

16-
constructor(ipfsFileHash: string, failureReason: FailureReason) {
17+
constructor(ipfsFileHash: string, failureReason: FailureReason, failureTime = new Date().getTime()) {
1718
super()
1819
this.ipfsFileHash = ipfsFileHash
1920
this.failureReason = failureReason
21+
this.failureTime = failureTime
2022
}
2123
}
2224

2325
export class IPFSGenericError extends Error {
2426
readonly ipfsFileHash: string
2527
readonly underlyingError: Error
28+
readonly failureTime: number
2629

27-
constructor(ipfsFileHash: string, underlyingError: Error) {
30+
constructor(ipfsFileHash: string, underlyingError: Error, failureTime = new Date().getTime()) {
2831
super()
2932
this.ipfsFileHash = ipfsFileHash
3033
this.underlyingError = underlyingError
34+
this.failureTime = failureTime
3135
}
3236
}
3337

3438
export class IPFSTimeoutError extends Error {
3539
readonly ipfsFileHash: string
40+
readonly failureTime: number
3641

37-
constructor(ipfsFileHash: string) {
42+
constructor(ipfsFileHash: string, failureTime = new Date().getTime()) {
3843
super()
3944
this.ipfsFileHash = ipfsFileHash
45+
this.failureTime = failureTime
4046
}
4147
}
4248

src/StorageReader/ExchangeConfiguration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export interface ExchangeConfiguration {
22
readonly batchReaderReadNextDirectorySuccess?: string
33
readonly poetAnchorDownloaded?: string
44
readonly claimsDownloaded?: string
5+
readonly claimsNotDownloaded?: string
56
}

src/StorageReader/StorageReader.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export class StorageReader {
4747
this.mongoClient = await MongoClient.connect(this.configuration.dbUrl)
4848
this.dbConnection = await this.mongoClient.db()
4949

50-
const exchangesMessaging = pick(['poetAnchorDownloaded', 'claimsDownloaded'], this.configuration.exchanges)
50+
const exchangesMessaging = pick(
51+
['poetAnchorDownloaded', 'claimsDownloaded', 'claimsNotDownloaded'],
52+
this.configuration.exchanges,
53+
)
5154
this.messaging = new Messaging(this.configuration.rabbitmqUrl, exchangesMessaging)
5255
await this.messaging.start()
5356

src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export async function app(localVars: any = {}) {
138138
batchReaderReadNextDirectorySuccess: configuration.exchangeBatchReaderReadNextDirectorySuccess,
139139
poetAnchorDownloaded: configuration.exchangePoetAnchorDownloaded,
140140
claimsDownloaded: configuration.exchangeClaimsDownloaded,
141+
claimsNotDownloaded: configuration.exchangeClaimsNotDownloaded,
141142
},
142143
})
143144
try {

0 commit comments

Comments
 (0)