data['include_codes'] ?? array() ) ) ); } /** * Next failed rows of the root job. * * @param Job $job Job. * @param array $cursor Cursor. * @param int $maxItems Engine suggestion. * @return Chunk * @throws \RuntimeException When the file is gone. */ public function nextChunk( Job $job, array $cursor, int $maxItems ): Chunk { $p = JobParams::of( $job ); if ( ! FileStore::hasSource( $p->ref() ) ) { throw new \RuntimeException( 'The import file no longer exists.' ); } $after = (int) ( $cursor['after'] ?? 0 ); if ( ! ScanBuilder::ready( $p ) ) { // The file-level rules (repeated SKU or GTIN) need the scan, which a newer format or a damaged file makes again. ScanBuilder::advance( $p, 4.0 ); return new Chunk( array(), array( 'after' => $after ), false, (int) ( $p->data['failed_total'] ?? 0 ) ); } $size = ChunkTuner::next( $job->id, $maxItems ); $reader = new CsvReader( $p->path(), $p->delimiter() ); $rows = JobRowsRepo::failed( $p->retryOf(), $after, $size, self::codes( $p ) ); $items = array(); foreach ( $rows as $r ) { $cells = $reader->at( (int) $r['byte_offset'] ); $after = (int) $r['row_no']; if ( null === $cells ) { continue; } $items[] = new ItemRef( 'r' . $r['row_no'], array( 'row' => (int) $r['row_no'], 'off' => (int) $r['byte_offset'], 'cells' => $cells, 'code' => (string) $r['code'], ) ); } $done = count( $rows ) < $size; return new Chunk( $items, array( 'after' => $after ), $done, (int) ( $p->data['failed_total'] ?? 0 ) ); } /** * Process one failed row again. * * @param Job $job Job. * @param ItemRef $item Item. * @return ItemResult */ public function processItem( Job $job, ItemRef $item ): ItemResult { $t0 = microtime( true ); BulkMode::enter(); $p = JobParams::of( $job ); $ctx = RowParser::parse( (array) $item->payload['cells'], (int) $item->payload['row'], (int) $item->payload['off'], $p ); $again = self::isReplay( $job->id, $item->key ); $ctx->replay = $again || in_array( (string) ( $item->payload['code'] ?? '' ), self::ENGINE_CODES, true ); // A row the engine gave up on may have created its product. if ( $again ) { JobRowsRepo::forget( $job->id, $ctx->row ); // The outcome an interrupted attempt recorded is replaced by this one. } try { $plan = ( new RowPlanner() )->plan( $ctx, $p, ScanState::load( $p ), false ); $result = ( new ProductWriter() )->apply( $plan, $ctx, $p, $job ); } catch ( \WC_Data_Exception $e ) { $result = RowResult::failed( self::wcCode( $e ), $e->getErrorCode() ); } foreach ( $result->warnings as $w ) { JobRowsRepo::record( $p->retryOf(), $ctx->row, $ctx->offset, 'warning', $w->code, $w->detail, $result->productId ); JobRowsRepo::record( $job->id, $ctx->row, $ctx->offset, 'warning', $w->code, $w->detail, $result->productId ); // The retry's own totals and status. } if ( RowResult::FAILED === $result->kind ) { JobRowsRepo::refail( $p->retryOf(), $ctx->row, $result->code, $result->detail ); JobRowsRepo::record( $job->id, $ctx->row, $ctx->offset, 'failed', $result->code, $result->detail ); $out = ItemResult::failed( $result->code, $result->detail ); } elseif ( RowResult::SKIPPED === $result->kind ) { // Nothing was written: no longer a failed row, but listed as skipped (on the import too), not as fixed. JobRowsRepo::resolve( $p->retryOf(), $ctx->row, $job->id ); JobRowsRepo::record( $p->retryOf(), $ctx->row, $ctx->offset, 'skipped', $result->code, $result->detail ); JobRowsRepo::record( $job->id, $ctx->row, $ctx->offset, 'skipped', $result->code, $result->detail ); $out = ItemResult::skipped( $result->code, $result->detail ); } else { JobRowsRepo::resolve( $p->retryOf(), $ctx->row, $job->id ); $out = ItemResult::done( $result->ref() ); } ChunkTuner::observe( $job->id, ( microtime( true ) - $t0 ) * 1000 ); return $out; } /** * Same rules as an import. A row found written is fixed: the engine marks it done without processItem(), so it is * resolved on the root job here (a later retry would otherwise take it again as a failed row and create it twice). * * @param Job $job Job. * @param ItemRef $item Item. * @return int */ public function reconcile( Job $job, ItemRef $item ): int { $p = JobParams::of( $job ); $ctx = RowParser::parse( (array) $item->payload['cells'], (int) $item->payload['row'], (int) $item->payload['off'], $p ); $verdict = Reconciler::decide( $p, $ctx, ScanState::load( $p ), $job->id ); if ( ReconcileResult::APPLIED === $verdict ) { JobRowsRepo::resolve( $p->retryOf(), $ctx->row, $job->id ); } return $verdict; } /** * Totals, ledger batch and a fresh failed-rows CSV of the root job. * * @param Job $job Job. * @param array $summary Summary. * @return void */ public function finish( Job $job, array $summary ): void { $p = JobParams::of( $job ); BulkMode::leave(); JobRowsRepo::sentinel( $job->id, 'totals', ImportJobType::totals( $job->id, $summary ) ); if ( $p->batchId() > 0 ) { Ledger::commitBatch( $p->batchId() ); } ReportWriter::writeFile( $p->retryOf(), $p ); ImportJobType::scheduleFinalize( $job->id ); } }