blocked() ) { return new Chunk( array(), array( 'row' => 1, 'off' => $p->headerEnd(), ), true, 0 ); } return $this->readChunk( $job, $cursor, $maxItems ); } /** * Process one row. * * @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 ); $ctx->replay = self::isReplay( $job->id, $item->key ); 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() ); } $out = $this->recordResult( $job, $ctx, $result ); ChunkTuner::observe( $job->id, ( microtime( true ) - $t0 ) * 1000 ); return $out; } /** * Decide whether an interrupted row took effect. * * @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 ); return Reconciler::decide( $p, $ctx, ScanState::load( $p ), $job->id ); } /** * The job ended: totals, failed-rows CSV, ledger batch. * * @param Job $job Job. * @param array $summary Summary. * @return void */ public function finish( Job $job, array $summary ): void { $p = JobParams::of( $job ); BulkMode::leave(); FaultInjector::at( Container::requireCurrent(), 'p09.before_finalize', array( 'job' => $job->id ) ); if ( $p->blocked() ) { JobRowsRepo::sentinel( $job->id, 'outcome', array( 'outcome' => 'blocked' ) ); } JobRowsRepo::sentinel( $job->id, 'totals', self::totals( $job->id, $summary ) ); if ( $p->batchId() > 0 ) { Ledger::commitBatch( $p->batchId() ); } ReportWriter::writeFile( $job->id, $p ); self::scheduleFinalize( $job->id ); Logger::log( Logger::INFO, 'import.finished', array( 'object_type' => 'job', 'object_id' => (string) $job->id, 'job_id' => $job->id, 'context' => array( 'status' => $summary['status'] ), ) ); } /** * Run the end of the job for imports and retries whose process died after the library recorded their final status * and before finish() ran (core B15): totals, ledger batch, saved failed-rows CSV and the repair would be lost for good. * Called by the daily cleanup for jobs that ended more than an hour ago and within the file retention (the failed-rows * CSV is read from the file); finish() writes the totals first, so a job without them never got that far. * * @return int Jobs finished. */ public static function finishLost(): int { global $wpdb; $d = Container::requireCurrent(); $done = "'" . implode( "', '", Job::TERMINAL ) . "'"; // Constants, not user input. $days = max( 1, (int) apply_filters( 'catalogrelay_file_retention_days', 7 ) ); $ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT j.id FROM {$wpdb->prefix}catalogrelay_jobs j WHERE j.job_type IN (%s, %s) AND j.status IN ({$done}) AND j.finished_at >= %s AND j.finished_at < %s AND NOT EXISTS (SELECT 1 FROM {$wpdb->prefix}catalogrelay_job_rows r WHERE r.job_id = j.id AND r.row_no = 0 AND r.status = %s AND r.code = 'totals') ORDER BY j.id LIMIT 20", self::TYPE, RetryJobType::TYPE, gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ), gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ), JobRowsRepo::SENTINEL ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table names from the prefix, fixed status list. $n = 0; foreach ( $ids as $id ) { $job = JobStore::find( $d, (int) $id ); if ( ! $job ) { continue; } $type = RetryJobType::TYPE === $job->type ? new RetryJobType() : new self(); try { $type->finish( $job, array( 'status' => $job->status, 'total' => $job->total, 'processed' => $job->processed, 'failed' => $job->failed, 'skipped' => $job->skipped, ) ); ++$n; } catch ( \Throwable $e ) { Logger::log( Logger::ERROR, 'job.finish_failed', array( 'object_type' => 'job', 'object_id' => (string) $job->id, 'job_id' => $job->id, 'context' => array( 'error' => $e->getMessage() ), ) ); } } return $n; } /** * Totals sentinel. "recovered" are rows found written after an interruption (reconcile: applied, the receipt and so * its kind are lost); they may have been creations, updates or unchanged rows. * * @param int $jobId Job. * @param array $summary Summary. * @return array */ public static function totals( int $jobId, array $summary ): array { $r = self::receiptCounts( $jobId ); return array( 'rows' => (int) ( $summary['total'] ?? 0 ), 'created' => $r['c'], 'updated' => $r['u'], 'unchanged' => $r['n'], 'recovered' => $r['recovered'], 'skipped' => (int) ( $summary['skipped'] ?? 0 ), 'failed' => (int) ( $summary['failed'] ?? 0 ), 'warnings' => (int) ( JobRowsRepo::counts( $jobId )['warning'] ?? 0 ), ); } /** * Queue the repair of parent products and term counts after a takeover or a crash (the in-memory dirty set and the * deferred term counts of a killed process are lost; a process killed between rows leaves no crashed row, only the * library's job.takeover log entry). * * @param int $jobId Job. * @return void */ public static function scheduleFinalize( int $jobId ): void { global $wpdb; if ( ! function_exists( 'as_enqueue_async_action' ) ) { return; } $crashed = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}catalogrelay_job_items WHERE job_id = %d AND crash_count > 0", $jobId ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from the prefix. if ( $crashed < 1 ) { $d = Container::requireCurrent(); $crashed = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE object_type = 'job' AND object_id = %s AND action = 'job.takeover'", Schema::table( $d, $d->logPolicy()->table ), (string) $jobId ) ); } if ( $crashed < 1 ) { return; } // Not Action Scheduler's $unique: before 4.0 it ignores the args, so another job's pending repair would drop this one. if ( function_exists( 'as_has_scheduled_action' ) && as_has_scheduled_action( Finalizer::HOOK, array( $jobId ), 'catalogrelay' ) ) { return; } as_enqueue_async_action( Finalizer::HOOK, array( $jobId ), 'catalogrelay', false ); } }