1+ import { taskLog } from '@clack/prompts'
12import * as Piece from '@filoz/synapse-core/piece'
23import * as SP from '@filoz/synapse-core/sp'
34import { and , asc , eq , gt , inArray } from 'drizzle-orm'
@@ -6,6 +7,7 @@ import { filterPiecesNotInDataset } from '../db/filter-pull-pieces-not-in-datase
67import { getTargetDataset } from '../db/get-target-dataset.ts'
78import { updateOperation } from '../db/update-operation.ts'
89import { upsertOperations } from '../db/upsert-operations.ts'
10+
911import type { AddPieceOperationData , SelectOperation , SelectRepair } from '../local-schema.ts'
1012import type { IndexerDatabase , LocalDatabase , WalletClient } from '../types.ts'
1113
@@ -30,20 +32,31 @@ export function createPullPiecesWorker({
3032 indexerDb,
3133 repair,
3234 client,
35+ state,
36+ log,
3337} : {
3438 localDb : LocalDatabase
3539 indexerDb : IndexerDatabase
3640 repair : SelectRepair
3741 client : WalletClient
42+ state : {
43+ totalBatches : number
44+ totalOperations : number
45+ completedOperations : number
46+ failedOperations : number
47+ }
48+ log : ReturnType < typeof taskLog >
3849} ) {
39- return async ( batch : PullPiecesBatch ) => {
50+ return async ( batch : PullPiecesBatch , batchNumber : number ) => {
51+ const spin = log . group ( `Batch ${ batchNumber } /${ state . totalBatches } ` )
52+ spin . message ( `Pull 0 completed, 0 failed` )
4053 try {
4154 const dataset = await getTargetDataset ( { localDb, repairId : repair . id , client } )
4255 // create pull pieces
4356 const pullPieces : SP . PullPieceInput [ ] = [ ]
4457 for ( const operation of batch . operations ) {
4558 const data = operation . data as AddPieceOperationData
46- const pieceCid = Piece . parse ( data . cid )
59+ const pieceCid = Piece . from ( data . cid )
4760 const sourceUrl = new URL ( `/piece/${ pieceCid . toString ( ) } ` , data . alternateProviders [ 0 ] ) . toString ( )
4861 pullPieces . push ( { pieceCid, sourceUrl, metadata : data . metadata } )
4962 }
@@ -56,13 +69,14 @@ export function createPullPiecesWorker({
5669 pieces : pullPieces ,
5770 timeout : 1000 * 60 * 30 ,
5871 onStatus : ( _status ) => {
59- // console.log(`${JSON.stringify(_status)}`)
72+ const completed = _status . pieces . filter ( ( piece ) => piece . status === 'complete' ) . length
73+ const failed = _status . pieces . filter ( ( piece ) => piece . status === 'failed' ) . length
74+ spin . message ( `Pull ${ completed } completed, ${ failed } failed` )
6075 } ,
6176 } )
6277
63- // console.log('🚀 ~ createPullPiecesWorker ~ pullResult:', pullResult.pieces)
64-
6578 const completedCids = [ ]
79+ const failedCids = [ ]
6680
6781 for ( const { pieceCid, status } of pullResult . pieces ) {
6882 const operation = batch . operations . find (
@@ -85,6 +99,7 @@ export function createPullPiecesWorker({
8599 }
86100 case 'failed' : {
87101 if ( operation ) {
102+ failedCids . push ( pieceCid )
88103 console . log ( `cid ${ pieceCid } failed to pull from ${ data . alternateProviders [ 0 ] } ` )
89104 await updateOperation ( {
90105 localDb,
@@ -101,6 +116,8 @@ export function createPullPiecesWorker({
101116 }
102117 }
103118 }
119+ state . completedOperations += completedCids . length
120+ state . failedOperations += failedCids . length
104121
105122 // prepare operations for commit
106123 const notInDatasetCids = await filterPiecesNotInDataset ( {
@@ -111,20 +128,24 @@ export function createPullPiecesWorker({
111128 const commitPieces : SP . addPieces . PieceType [ ] = [ ]
112129 for ( const cid of notInDatasetCids ) {
113130 commitPieces . push ( {
114- pieceCid : Piece . parse ( cid ) ,
131+ pieceCid : Piece . from ( cid ) ,
115132 metadata : pullPieces . find ( ( { pieceCid } ) => pieceCid . toString ( ) === cid ) ?. metadata ,
116133 } )
117134 }
118- console . log ( `Pulled ${ commitPieces . length } pieces in dataset ${ dataset . dataSetId } ` )
135+ spin . success (
136+ `Batch ${ batchNumber } /${ state . totalBatches } ${ completedCids . length } completed, ${ failedCids . length } failed`
137+ )
119138 // console.log(commitPieces)
120139 } catch ( error ) {
121- console . error ( error instanceof Error ? error . message : 'Unknown error' )
140+ state . failedOperations += batch . operations . length
141+ const message = error instanceof Error ? error . message : 'Unknown error'
142+ spin . error ( `Batch ${ batchNumber } /${ state . totalBatches } - ${ message . replace ( / \n / g, ' ' ) } ` )
122143 await upsertOperations ( {
123144 localDb,
124145 operations : batch . operations . map ( ( operation ) => ( {
125146 ...operation ,
126147 status : 'failed' ,
127- error : error instanceof Error ? error . message : 'Unknown error' ,
148+ error : message ,
128149 } ) ) ,
129150 } )
130151 }
@@ -151,6 +172,27 @@ export async function runPullPiecesPhase({
151172 const pullBatchSize = Math . max ( 1 , batchSize )
152173 let pullCursor = 0
153174
175+ const totalOperations = await localDb . $count (
176+ localSchema . operations ,
177+ and (
178+ eq ( localSchema . operations . repairId , repair . id ) ,
179+ eq ( localSchema . operations . type , 'add_piece' ) ,
180+ inArray ( localSchema . operations . status , reset ? [ 'pending' , 'failed' ] : [ 'pending' ] )
181+ )
182+ )
183+ let batchNumber = 0
184+ const state = {
185+ totalBatches : Math . ceil ( totalOperations / pullBatchSize ) ,
186+ totalOperations,
187+ completedOperations : 0 ,
188+ failedOperations : 0 ,
189+ }
190+
191+ const log = taskLog ( {
192+ title : 'Pulling pieces' ,
193+ limit : 1 ,
194+ } )
195+
154196 async function getNextPullBatch ( ) : Promise < PullPiecesBatch | null > {
155197 const operations = await localDb . query . operations . findMany ( {
156198 where : and (
@@ -170,15 +212,19 @@ export async function runPullPiecesPhase({
170212 return { operations }
171213 }
172214
173- const pullPiecesWorker = createPullPiecesWorker ( { localDb, indexerDb, repair, client } )
215+ const pullPiecesWorker = createPullPiecesWorker ( { localDb, indexerDb, repair, client, state , log } )
174216 const pullPiecesQueue = new PQueue ( { concurrency : pullConcurrency } )
175217
176218 while ( true ) {
177219 await pullPiecesQueue . onSizeLessThan ( pullConcurrency )
178220 const batch = await getNextPullBatch ( )
179221 if ( ! batch ) break
180- pullPiecesQueue . add ( ( ) => pullPiecesWorker ( batch ) ) . catch ( console . error )
222+ batchNumber ++
223+ const currentBatchNumber = batchNumber
224+ pullPiecesQueue . add ( ( ) => pullPiecesWorker ( batch , currentBatchNumber ) ) . catch ( console . error )
181225 }
182226
183227 await pullPiecesQueue . onIdle ( )
228+
229+ log . success ( `Pulled ${ state . completedOperations } pieces, ${ state . failedOperations } failed` )
184230}
0 commit comments