*/ private $lock_tokens = []; const MIN_WINDOW_SIZE = AutomationConstants::MIN_WINDOW_SIZE; const MAX_WINDOW_SIZE = AutomationConstants::MAX_WINDOW_SIZE; const PROGRESS_FLUSH_EVERY = AutomationConstants::PROGRESS_FLUSH_EVERY; private const RETIRED_STANDARD_TOOL_MESSAGE = 'The Standard generator has been merged into the Advanced generator; recreate this job.'; /** * Constructor * * @param AutomationJobManager $job_manager Job manager instance * @param JobLoggerInterface $logger Logger instance */ public function __construct(AutomationJobManager $job_manager, JobLoggerInterface $logger) { $this->job_manager = $job_manager; $this->logger = $logger; $this->draft_manager = new DraftContentManager(); global $wpdb; $this->wpdb = $wpdb; $this->job_products_table = $wpdb->prefix . 'aipt_job_products'; $this->queue = new \AIPT\Api\AutomationJobs\ClaimBasedQueue(); $this->progress_tracker = new \AIPT\Api\AutomationJobs\IncrementalProgressTracker(); $this->options_translator = new OptionsTranslator(); $this->result_processor = new ResultProcessor($logger); $this->item_retry_tracker = new ItemRetryTracker(); } /** * Execute a job * * @param int $job_id Job ID to execute * @return bool Success status * @throws \Exception On execution failure */ public function executeJob($job_id) { $job = $this->job_manager->getJob($job_id); if (!$job) { throw new \Exception("Job not found: {$job_id}"); } if ($this->isCancelledJob($job)) { return true; } if ('standard' === (string) $job->tool_type) { if (in_array((string) $job->status, ['completed', 'failed'], true)) { return false; } return $this->failRetiredStandardJob($job); } if ($job->status === 'paused') { $this->logger->info($job_id, 'Job is paused, skipping execution'); return true; } $validation_errors = $this->validateJobForExecution($job); if (!empty($validation_errors)) { $this->logger->error($job_id, 'Job validation failed', [ 'errors' => $validation_errors, 'type' => 'VALIDATION' ]); throw new \Exception('Job validation failed: ' . wp_json_encode($validation_errors)); } if (JobScheduler::isWaitingForRecurringCycle($job, $this->job_manager)) { return true; } try { $this->logger->info($job_id, 'Starting job execution (time-sliced)', [ 'tool_type' => $job->tool_type, 'job_name' => $job->name, 'timestamp' => current_time('mysql', true) ]); if (!$this->acquireLock((int) $job_id)) { $this->logger->info($job_id, 'Another executor is already running for this job (lock held)'); return false; } try { if ($this->isCancelledJob($this->job_manager->getJob($job_id))) { return true; } $released = $this->queue->forceReleaseAllProcessing((int) $job_id); $releaseContext = ['released_count' => (int) $released]; if ($released > 0 && $this->shouldLogDebugSnapshot((int) $job_id, 'released_processing', $releaseContext)) { $this->logger->debug($job_id, 'Released processing items before tick', $releaseContext); } $tickStartTs = microtime(true); $startState = $this->describeJobProductState((int) $job_id); $startContext = array_merge($startState, [ 'job_status' => $job->status, 'progress_percent' => (int) $job->progress, 'processed_products' => (int) $job->processed_products, 'failed_products' => (int) $job->failed_products, 'total_products' => (int) $job->total_products, ]); if ($this->shouldLogDebugSnapshot((int) $job_id, 'tick_start', $startContext)) { $this->logger->debug($job_id, 'Tick starting', $startContext); } if (in_array($job->status, ['ready', 'completed'], true) && JobScheduler::isRecurringCycleDue($job)) { if (!$this->startRecurringCycle($job)) { return true; } $job = $this->job_manager->getJob($job_id); if (!$job) { return false; } } if ($job->status === 'preparing') { try { $pool_builder = new \AIPT\Api\AutomationJobs\PoolBuilder(); $filter = is_array($job->product_filter) ? $job->product_filter : []; $pool_options = AutomationJob::getPoolBuildOptions( (string) $job->tool_type, is_array($job->settings) ? $job->settings : [] ); $pool_result = $pool_builder->buildPool( (int)$job_id, $filter, AutomationConstants::POOL_BUILD_CHUNK_SIZE, AutomationConstants::MAX_POOL_BUILD_CHUNKS, $pool_options['skip_processed'], $pool_options['targeted_types'] ); if (!empty($pool_result['blocked'])) { $this->job_manager->pauseForEntitlement((int) $job_id, (string) $pool_result['blocked']['message'], true); $this->logger->warning($job_id, 'Pool build paused: saved product filter cannot be honored', [ 'reason' => $pool_result['blocked']['reason'], 'feature' => $pool_result['blocked']['feature'], 'filters' => $pool_result['blocked']['keys'], ]); return true; } $target_total = isset($pool_result['total']) ? (int)$pool_result['total'] : (int)$job->total_products; $current_total = (int) $this->wpdb->get_var( $this->wpdb->prepare( "SELECT COUNT(*) FROM {$this->job_products_table} WHERE job_id = %d", (int)$job_id ) ); $jobs_table = $this->wpdb->prefix . 'aipt_automation_jobs'; if ($target_total !== (int)$job->total_products) { $this->wpdb->update( $jobs_table, ['total_products' => $target_total, 'updated_at' => current_time('mysql')], ['id' => (int)$job_id], ['%d','%s'], ['%d'] ); } else { $this->wpdb->update( $jobs_table, ['updated_at' => current_time('mysql')], ['id' => (int)$job_id], ['%s'], ['%d'] ); } if ($current_total >= $target_total) { if ($target_total === 0) { $this->job_manager->updateJobStatus((int)$job_id, 'completed'); $this->logger->info($job_id, 'Job completed immediately; no products matched'); (new JobScheduler($this->job_manager, $this->logger))->rearmRecurringJob((int) $job_id); return true; } $this->job_manager->updateJobStatus((int)$job_id, 'ready'); $this->logger->info($job_id, 'Product pool fully built; job set to ready', ['total' => $target_total]); } else { $this->job_manager->updateJobStatus((int)$job_id, 'preparing'); $this->logger->info($job->id, 'Continuing pool build in background', [ 'inserted_so_far' => $current_total, 'target_total' => $target_total ]); } \AIPT\Api\AutomationJobs\JobExecutionHandler::register_job_hook((int)$job_id); $hook = 'aipt_execute_job_' . (int)$job_id; $scheduleTimestamp = time() + AutomationConstants::rescheduleDelay(); $scheduled = \wp_schedule_single_event($scheduleTimestamp, $hook, [(int)$job_id]); $nextScheduled = \wp_next_scheduled($hook, [(int)$job_id]); $poolContext = array_merge($this->describeJobProductState((int)$job_id), [ 'remaining_items' => max(0, (int)$target_total - $current_total), 'next_tick_due_at' => $nextScheduled ? gmdate('c', $nextScheduled) : null, 'schedule_result' => $scheduled === false ? 'failed' : 'scheduled', 'pool_target' => $target_total, 'pool_current' => $current_total, ]); if ($this->shouldLogDebugSnapshot((int)$job_id, 'pool_rescheduled', $poolContext)) { $this->logger->debug($job_id, 'Pool build tick rescheduled', $poolContext); } if ($scheduled === false) { $this->logger->warning($job_id, 'Failed to schedule next pool build tick', [ 'hook' => $hook, 'pool_target' => $target_total, 'pool_current' => $current_total, ]); } return true; } catch (\Throwable $t) { $this->logger->error($job_id, 'Pool build step failed', ['error' => $t->getMessage(), 'type' => 'DB']); return false; } } $processedThisTick = 0; $failedThisTick = 0; $claimAttempts = 0; $exitReason = 'loop_completed'; $options = $this->options_translator->translate($job->settings, $job->tool_type); $assessment = JobEntitlementGuard::assessRun($job, is_array($options) ? $options : []); if (null !== $assessment['block']) { $this->pauseForEntitlement((int) $job_id, $assessment['block']); return true; } $reviewGuaranteed = JobEntitlementGuard::reviewGuaranteed($job); $loggedStoreSectionNotices = []; if ($job->status !== 'running') { $start = $this->job_manager->startJobWithinConcurrentCap((int) $job_id); if (AutomationJobManager::START_ADMITTED !== $start) { $this->logDeferredStart((int) $job_id, $start); \AIPT\Api\AutomationJobs\JobExecutionHandler::register_job_hook((int) $job_id); $hook = 'aipt_execute_job_' . (int) $job_id; $scheduled = \wp_schedule_single_event(time() + AutomationConstants::rescheduleDelay(), $hook, [(int) $job_id]); if ($scheduled === false) { $this->logger->warning($job_id, 'Failed to schedule deferred job start retry', ['hook' => $hook]); } return true; } $this->logger->info($job_id, 'Job status set to running'); $this->logRunClamps((int) $job_id, $assessment['clamps']); } $this->logger->info($job_id, 'Initializing AI service', ['tool_type' => $job->tool_type]); $service = $this->getAIService($job->tool_type); $this->logger->info($job_id, 'AI service initialized', ['service_class' => get_class($service)]); $budgetEndsAt = time() + AutomationConstants::tickTimeBudget(); $processed_inc = 0; $failed_inc = 0; $last_flush = time(); $itemCounter = 0; $memorySoftLimit = $this->getSoftMemoryLimitBytes(); $exitReason = 'tick_completed'; while (time() < $budgetEndsAt) { $freshJob = $this->job_manager->getJob($job_id); if ($this->isCancelledJob($freshJob)) { $exitReason = 'cancelled_mid_tick'; break; } if ($freshJob && $freshJob->status === 'paused') { $exitReason = 'paused_mid_tick'; $this->logger->info($job_id, 'Job paused mid-execution, stopping tick'); break; } $claimAttempts++; $adaptiveWindowSize = $this->getAdaptiveWindowSize((int) $job_id); $claim = $this->queue->claimBatch((int) $job_id, $adaptiveWindowSize); $currentClaimToken = $claim['token'] ?? null; if (empty($claim['product_ids'])) { if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); } $exitReason = 'no_claimable_items'; break; } foreach ($claim['product_ids'] as $productIndex => $product_id) { if (time() >= $budgetEndsAt) { if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } $exitReason = 'time_budget_exhausted'; break 1; } $freshJob = $this->job_manager->getJob($job_id); if ($this->isCancelledJob($freshJob)) { $exitReason = 'cancelled_mid_tick'; if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } break 2; } if ($freshJob && $freshJob->status === 'paused') { $exitReason = 'paused_mid_tick'; $this->logger->info($job_id, 'Job paused, stopping item loop'); if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } break 1; } try { if (!$currentClaimToken || !$this->queue->ownsClaim((int)$job->id, (int)$product_id, (string)$currentClaimToken)) { $exitReason = 'claim_lost'; $currentClaimToken = null; break 2; } if (!$this->renewLock((int) $job->id)) { $exitReason = 'lock_lost'; if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } break 2; } $results = $this->generateForAutomationProduct($service, (int) $product_id, $options, (int) $job->created_by, $reviewGuaranteed); $this->logStoreSectionClampNotices((int) $job_id, $results, $loggedStoreSectionNotices); if ($this->isCancelledJob($this->job_manager->getJob($job_id))) { $exitReason = 'cancelled_mid_tick'; if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } break 2; } if (!$this->queue->ownsClaim((int)$job->id, (int)$product_id, (string)$currentClaimToken)) { $exitReason = 'claim_lost'; $currentClaimToken = null; break 2; } $counts = $this->processResults($job->id, $results, (string)$currentClaimToken); $completedCount = (int) ($counts['completed'] ?? 0); $failedCount = (int) ($counts['failed'] ?? 0); $hasCompleted = $completedCount > 0; $hasFailed = $failedCount > 0; if (!empty($counts['claim_lost'])) { $exitReason = 'claim_lost'; $currentClaimToken = null; break 2; } if (!empty($counts['cancelled']) && !$hasCompleted && !$hasFailed) { $exitReason = 'cancelled_mid_tick'; if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } break 2; } if ($hasCompleted && !$hasFailed) { $this->item_retry_tracker->clear((int)$job->id, (int)$product_id); $processed_inc += 1; $processedThisTick += 1; } elseif ($hasFailed) { $failed_inc += 1; $failedThisTick += 1; } else { if (!$this->queue->markFailed((int)$job->id, (int)$product_id, (string)$currentClaimToken, 'No products were processed')) { $exitReason = 'claim_lost'; $currentClaimToken = null; break 2; } $failed_inc += 1; $failedThisTick += 1; } } catch (\Throwable $t) { if ($this->isCancelledJob($this->job_manager->getJob($job_id))) { $exitReason = 'cancelled_mid_tick'; if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } break 2; } $providerId = (string) ($options['provider'] ?? ''); $merchantMessage = ProviderCallException::displayMessage($t, $providerId); \AIPT\Core\ErrorHandler::log_error(sprintf('[%s] %s', '' !== $providerId ? $providerId : 'API', $t->getMessage()), 'error'); if ($t instanceof ProviderCallException && $t->isRunWide()) { $reason = $t->reason(); if (ProviderCallException::QUOTA_EXHAUSTED === $reason || ProviderCallException::RATE_LIMITED === $reason) { do_action('aipt_automation_ai_call_failed', (int) $job->id, 'PROVIDER_QUOTA_LIMIT'); } if (!$t->isRetryable()) { $this->item_retry_tracker->clear((int)$job->id, (int)$product_id); $this->queue->requeueItem((int)$job->id, (int)$product_id, (string)$currentClaimToken); if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } $this->pauseForProviderFailure((int)$job->id, $t, $providerId, $merchantMessage); $exitReason = 'provider_run_wide_failure'; break 2; } $attempts = $this->item_retry_tracker->recordAttempt((int)$job->id, (int)$product_id); if ($attempts >= ItemRetryTracker::MAX_ATTEMPTS && ProviderCallException::RATE_LIMITED === $t->reason()) { $this->item_retry_tracker->clear((int)$job->id, (int)$product_id); $this->queue->requeueItem((int)$job->id, (int)$product_id, (string)$currentClaimToken); if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } $this->pauseForProviderFailure( (int)$job->id, $t, $providerId, $merchantMessage . ' ' . __('The job was paused so the remaining products wait instead of failing; resume it once the limit resets.', 'ai-product-tools') ); $exitReason = 'provider_rate_limit_persisted'; break 2; } if ($attempts >= ItemRetryTracker::MAX_ATTEMPTS) { $this->item_retry_tracker->clear((int)$job->id, (int)$product_id); if (!$this->queue->markFailed((int)$job->id, (int)$product_id, (string)$currentClaimToken, $merchantMessage)) { $exitReason = 'claim_lost'; $currentClaimToken = null; break 2; } $failed_inc += 1; $failedThisTick += 1; } else { $this->queue->requeueItem((int)$job->id, (int)$product_id, (string)$currentClaimToken); } if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } $this->recordErrorForThrottling((int)$job->id); $exitReason = 'provider_transient_failure'; break 2; } if (!$this->queue->markFailed((int)$job->id, (int)$product_id, (string)$currentClaimToken, $merchantMessage)) { $exitReason = 'claim_lost'; $currentClaimToken = null; break 2; } $failed_inc += 1; $failedThisTick += 1; $this->recordErrorForThrottling((int)$job->id); } $itemCounter++; unset($results); if (($itemCounter % AutomationConstants::GC_COLLECT_CYCLES_INTERVAL) === 0 && function_exists('gc_collect_cycles')) { gc_collect_cycles(); } if (function_exists('memory_get_usage')) { $mem = memory_get_usage(true); if ($mem > $memorySoftLimit) { $exitReason = 'memory_soft_limit'; $this->logger->warning((int)$job->id, 'Soft memory limit reached, stopping tick early', [ 'type' => 'SYSTEM', 'memory_usage_bytes' => (int) $mem, 'soft_limit_bytes' => (int) $memorySoftLimit, ]); if ($currentClaimToken) { $this->queue->releaseClaimByToken((int)$job->id, (string)$currentClaimToken); $currentClaimToken = null; } break 2; } } if (($processed_inc + $failed_inc) >= self::PROGRESS_FLUSH_EVERY || (time() - $last_flush) >= 5) { $this->progress_tracker->increment((int)$job->id, (int)$processed_inc, (int)$failed_inc); $processed_inc = 0; $failed_inc = 0; $last_flush = time(); } } } if ($exitReason === 'tick_completed') { $exitReason = ($processedThisTick > 0 || $failedThisTick > 0) ? 'work_done' : 'tick_idle'; } if ($processed_inc > 0 || $failed_inc > 0) { $this->progress_tracker->increment((int)$job->id, (int)$processed_inc, (int)$failed_inc); } if ($processedThisTick > 0 && $failedThisTick === 0) { $this->resetErrorThrottling((int) $job->id); } $endState = $this->describeJobProductState((int) $job->id); $tickDurationMs = (int) round((microtime(true) - $tickStartTs) * 1000); $logContext = array_merge($endState, [ 'exit_reason' => $exitReason, 'processed_this_tick' => $processedThisTick, 'failed_this_tick' => $failedThisTick, 'claim_batches' => $claimAttempts, 'tick_duration_ms' => $tickDurationMs, ]); if ($this->shouldLogDebugSnapshot((int) $job->id, 'tick_finished', $logContext)) { $this->logger->debug($job_id, 'Tick finished', $logContext); } $freshJob = $this->job_manager->getJob($job_id); if ($this->isCancelledJob($freshJob) || ($freshJob && $freshJob->status === 'paused')) { return true; } $jobs_table = $this->wpdb->prefix . 'aipt_automation_jobs'; $now = current_time('mysql'); $completed = $this->wpdb->query( $this->wpdb->prepare( "UPDATE {$jobs_table} SET status = 'completed', completed_at = %s, updated_at = %s, progress = 100 WHERE id = %d AND status = 'running' AND NOT EXISTS ( SELECT 1 FROM {$this->job_products_table} WHERE job_id = %d AND status IN ('pending', 'processing') )", $now, $now, (int)$job->id, (int)$job->id ) ); if ($completed > 0) { $this->logger->info($job_id, 'Job completed successfully', $logContext); (new JobScheduler($this->job_manager, $this->logger))->rearmRecurringJob((int) $job_id); } else { $remaining = (int) $this->wpdb->get_var( $this->wpdb->prepare( "SELECT COUNT(*) FROM {$this->job_products_table} WHERE job_id = %d AND status IN ('pending','processing')", (int)$job->id ) ); if ($remaining > 0) { \AIPT\Api\AutomationJobs\JobExecutionHandler::register_job_hook((int)$job->id); $hook = 'aipt_execute_job_' . (int)$job->id; $scheduleTimestamp = time() + AutomationConstants::rescheduleDelay(); $scheduled = \wp_schedule_single_event($scheduleTimestamp, $hook, [(int)$job->id]); $nextScheduled = \wp_next_scheduled($hook, [(int)$job->id]); $scheduleContext = array_merge($logContext, [ 'remaining_items' => $remaining, 'next_tick_due_at' => $nextScheduled ? gmdate('c', $nextScheduled) : null, 'schedule_result' => $scheduled === false ? 'failed' : 'scheduled', ]); if ($this->shouldLogDebugSnapshot((int)$job->id, 'tick_rescheduled', $scheduleContext)) { $this->logger->debug($job_id, 'Tick rescheduled', $scheduleContext); } if ($scheduled === false) { $this->logger->warning($job_id, 'Failed to schedule next tick', array_merge($scheduleContext, [ 'hook' => $hook, ])); } $this->dispatchAsyncRunner(); } } return true; } finally { $this->releaseLock((int) $job_id); } } catch (\Exception $e) { $this->logger->error($job_id, 'Job execution failed', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'type' => 'SYSTEM' ]); $this->job_manager->updateJobStatus($job_id, 'failed', $e->getMessage()); throw $e; } } /** * Pause a running job * * @param int $job_id Job ID to pause * @return bool Success status * @throws \Exception On pause failure */ public function pauseJob($job_id) { $job = $this->job_manager->getJob($job_id); if (!$job) { throw new \Exception("Job not found: {$job_id}"); } if (!$job->canBePaused()) { throw new \Exception("Job cannot be paused in current status: {$job->status}"); } $this->job_manager->updateJobStatus($job_id, 'paused'); $this->logger->info($job_id, 'Job paused by user'); if (function_exists('delete_transient')) { delete_transient('aipt_job_monitor_' . (int) $job_id); } $hook = 'aipt_execute_job_' . (int) $job_id; if (function_exists('wp_clear_scheduled_hook')) { wp_clear_scheduled_hook($hook, [(int) $job_id]); } return true; } /** * Resume a paused job * * @param int $job_id Job ID to resume * @return bool Success status * @throws \Exception On resume failure */ public function resumeJob($job_id) { $job_id = (int) $job_id; $job = $this->job_manager->getJob($job_id); if (!$job) { throw new \Exception("Job not found: {$job_id}"); } if (!$job->canBeResumed()) { throw new \Exception("Job cannot be resumed in current status: {$job->status}"); } if (JobScheduler::isRecurringCycleDue($job)) { $resume_status = 'ready'; } else { $resume_status = $this->hasPoolRows((int) $job_id) ? 'running' : 'preparing'; } if ('running' === $resume_status) { $start = $this->job_manager->startJobWithinConcurrentCap((int) $job_id); if (AutomationJobManager::START_ADMITTED !== $start) { throw new \Exception(self::refusedResumeMessage($start)); } } else { $this->job_manager->updateJobStatus($job_id, $resume_status); } $this->logger->info($job_id, 'Job resumed by user'); if ('running' === $resume_status) { $options = $this->options_translator->translate(is_array($job->settings) ? $job->settings : [], (string) $job->tool_type); $this->logRunClamps((int) $job_id, JobEntitlementGuard::assessRun($job, is_array($options) ? $options : [])['clamps']); } if (function_exists('delete_transient')) { delete_transient('aipt_job_monitor_' . (int) $job_id); } \AIPT\Api\AutomationJobs\JobExecutionHandler::register_job_hook($job_id); $hook = 'aipt_execute_job_' . $job_id; $scheduled = \wp_schedule_single_event(time() + AutomationConstants::IMMEDIATE_EXECUTION_DELAY, $hook, [$job_id]); if ($scheduled === false) { $this->logger->error($job_id, 'Failed to schedule job resume'); } else { $this->logger->info($job_id, 'Job resume scheduled successfully'); } return true; } /** * Why a resume into 'running' was refused, for the REST error. * * @param string $start A refused AutomationJobManager::START_* result. * @return string */ private static function refusedResumeMessage(string $start): string { switch ($start) { case AutomationJobManager::START_BUSY: return 'Another automation job is starting right now. Please try again in a moment.'; case AutomationJobManager::START_WRITE_FAILED: return 'The job could not be resumed. Please try again.'; default: return sprintf('%d automation jobs are already running, the maximum allowed at once. Please try again in a few minutes.', AutomationConstants::MAX_CONCURRENT_JOBS); } } /** * Log why a tick did not start its job running; a later tick retries. * * @param int $job_id Job ID. * @param string $start A refused AutomationJobManager::START_* result. * @return void */ private function logDeferredStart(int $job_id, string $start): void { $context = ['max_concurrent_jobs' => AutomationConstants::MAX_CONCURRENT_JOBS]; if (AutomationJobManager::START_WRITE_FAILED === $start) { $this->logger->warning($job_id, 'Deferring job start: the running status could not be saved', $context); } elseif (AutomationJobManager::START_BUSY === $start) { $this->logger->info($job_id, 'Deferring job start: another automation job was starting at the same moment', $context); } else { $this->logger->info($job_id, 'Deferring job start: maximum concurrent automation jobs already running', $context); } } /** * Cancel a job * * @param int $job_id Job ID to cancel * @return bool Success status * @throws \Exception On cancel failure */ public function cancelJob($job_id) { $job_id = (int) $job_id; $job = $this->job_manager->getJob($job_id); if (!$job) { throw new \Exception("Job not found: {$job_id}"); } if ($job->isCompleted()) { throw new \Exception("Cannot cancel completed job"); } $this->job_manager->updateJobStatus($job_id, 'failed', AutomationJobManager::CANCELLED_BY_USER_ERROR_MESSAGE); try { $this->queue->forceReleaseAllProcessing((int) $job_id); } catch (\Throwable $release_error) { $this->logger->error($job_id, 'Failed to release claims on cancel', [ 'error' => $release_error->getMessage(), ]); } if (function_exists('wp_clear_scheduled_hook')) { wp_clear_scheduled_hook('aipt_execute_job_' . $job_id, [$job_id]); } $this->logger->info($job_id, 'Job cancelled by user'); return true; } /** * Get job execution progress * * @param int $job_id Job ID * @return array Progress information */ public function getJobProgress($job_id) { $job = $this->job_manager->getJob($job_id); if (!$job) { return []; } return [ 'progress' => $job->progress, 'total_products' => $job->total_products, 'processed_products' => $job->processed_products, 'failed_products' => $job->failed_products, 'status' => $job->status, 'started_at' => $job->started_at, 'estimated_completion' => $this->estimateCompletion($job) ]; } /** * Check if executor can handle specific tool type * * @param string $tool_type Tool type to check * @return bool Whether tool type is supported */ public function supportsToolType($tool_type) { return in_array($tool_type, ['advanced', 'title', 'faq', 'seo_meta', 'image_alt']); } /** * Validate job before execution * * @param AutomationJob $job Job to validate * @return array Validation errors (empty if valid) */ public function validateJobForExecution(AutomationJob $job) { $errors = []; if (!$this->supportsToolType($job->tool_type)) { $errors[] = "Unsupported tool type: {$job->tool_type}"; } $runnable_statuses = ['ready', 'preparing', 'running']; if (JobEntitlementGuard::isRecurring($job->schedule_config)) { $runnable_statuses[] = 'completed'; } if (!in_array($job->status, $runnable_statuses, true)) { $errors[] = "Job cannot be executed in current status: {$job->status}"; } if (empty($job->settings['provider'])) { $errors[] = "AI provider not specified in job settings"; } if (empty($job->settings['model'])) { $errors[] = "AI model not specified in job settings"; } return $errors; } /** * Get AI service for tool type * * @param string $tool_type Tool type * @return object AI service instance * @throws \Exception If tool type is not supported */ private function getAIService($tool_type) { switch ($tool_type) { case 'advanced': return new ContentGenerator(); case 'title': return new TitleGeneratorService(); case 'faq': return new FAQGeneratorService(); case 'seo_meta': return new SeoMetaGeneratorService(); case 'image_alt': return new ImageAltService(); default: throw new \Exception("Unsupported tool type: {$tool_type}"); } } /** * Update products status * * @param int $job_id Job ID * @param array $product_ids Product IDs * @param string $status New status * @param string|null $error_message Error message */ private function updateProductsStatus($job_id, $product_ids, $status, $error_message = null) { $job_id = (int) $job_id; if (empty($product_ids)) { return; } $ids = array_map('intval', (array) $product_ids); if (empty($ids)) { return; } $placeholders = implode(',', array_fill(0, count($ids), '%d')); $set_parts = ['status = %s']; $params = [$status]; if ($status === 'completed') { $set_parts[] = 'processed_at = %s'; $params[] = current_time('mysql'); } if ($error_message !== null && $error_message !== '') { $set_parts[] = 'error_message = %s'; $params[] = $error_message; } else { $set_parts[] = 'error_message = NULL'; } $params_with_ids = $params; $params_with_ids[] = $job_id; $params_with_ids = array_merge($params_with_ids, $ids); $sql = $this->wpdb->prepare( "UPDATE {$this->job_products_table} SET " . implode(', ', $set_parts) . " WHERE job_id = %d AND product_id IN ($placeholders)", $params_with_ids ); $this->wpdb->query($sql); } /** * Fail legacy Standard-generator jobs without instantiating the retired class. * * @param AutomationJob $job Legacy job row. * @return bool Always false because the job cannot continue. */ private function failRetiredStandardJob(AutomationJob $job): bool { $job_id = (int) $job->id; $message = self::RETIRED_STANDARD_TOOL_MESSAGE; $this->logger->error($job_id, $message, [ 'type' => 'VALIDATION', 'tool_type' => $job->tool_type, ]); $product_ids = $this->getPendingJobProductIds($job_id); if (!empty($product_ids)) { $this->updateProductsStatus($job_id, $product_ids, 'failed', $message); } $this->updateJobProgress($job_id); if (!$this->job_manager->updateJobStatus($job_id, 'failed', $message)) { $this->logger->warning($job_id, 'Could not persist retired Standard job transition to failed', [ 'type' => 'VALIDATION', ]); } return false; } /** * Return the non-terminal (pending/processing) product rows for a job. * * Terminal rows (completed/applied/rejected) are intentionally excluded so * a partially-processed legacy job keeps its already-earned progress. * * @param int $job_id Job ID. * @return int[] Product IDs still awaiting processing. */ private function getPendingJobProductIds(int $job_id): array { $product_ids = $this->wpdb->get_col( $this->wpdb->prepare( "SELECT product_id FROM {$this->job_products_table} WHERE job_id = %d AND status IN ('pending', 'processing')", $job_id ) ); return array_values(array_map('intval', is_array($product_ids) ? $product_ids : [])); } /** * Process generation results * * @param int $job_id Job ID * @param array $results Generation results * @param string|null $claim_token Current claim token, or null for legacy unclaimed callers */ private function processResults($job_id, $results, $claim_token = null) { $job = $this->job_manager->getJob($job_id); if (!$job) { $this->logger->error($job_id, 'Job not found when processing results'); return ['completed' => 0, 'failed' => 0, 'failure_messages' => []]; } if ($this->isCancelledJob($job)) { return ['completed' => 0, 'failed' => 0, 'failure_messages' => [], 'cancelled' => true]; } $content_settings = []; if (!empty($job->settings['contentSettings'])) { $content_settings = $job->settings['contentSettings']; } elseif (!empty($job->settings['content_settings'])) { $content_settings = $job->settings['content_settings']; } $this->logger->debug($job_id, 'Raw job settings for content parsing', [ 'all_settings' => $job->settings, 'contentSettings_key' => $job->settings['contentSettings'] ?? 'not_found', 'content_settings_key' => $job->settings['content_settings'] ?? 'not_found' ]); $content_status = $content_settings['status'] ?? 'draft'; $normalized = strtolower(is_string($content_status) ? $content_status : 'draft'); $content_settings['status'] = ($normalized === 'draft') ? 'draft' : 'published'; if (isset($job->settings['writeMode'])) { $content_settings['writeMode'] = ('replace' === $job->settings['writeMode'] && apply_filters('aipt_faq_replace_enabled', false)) ? 'replace' : 'append'; } $content_settings['created_by'] = (int) $job->created_by; $content_settings['overwrite'] = OptionsTranslator::overwriteMode(is_array($job->settings) ? $job->settings : []); $this->logger->debug($job_id, 'Processing results with content status: ' . $content_settings['status'], [ 'results_count' => count($results), 'content_settings' => $content_settings, 'raw_results_sample' => array_slice($results, 0, 2) ]); $content_items = []; $error_items = []; if (is_array($results)) { foreach ($results as $rawItem) { if (!is_array($rawItem)) { continue; } if (!empty($rawItem['api_error'])) { $providerName = isset($rawItem['provider']) ? (string)$rawItem['provider'] : 'API'; $productId = isset($rawItem['product_id']) ? (int)$rawItem['product_id'] : null; $apiError = (string) $rawItem['api_error']; $truncated = $this->truncateErrorMessage($apiError, AutomationConstants::MAX_ERROR_MESSAGE_LENGTH); if ($productId) { $this->logger->error($job_id, $apiError, [ 'type' => 'AI_PROVIDER', 'product_id' => $productId, 'provider' => $providerName, ]); if (!isset($error_items[$productId])) { $error_items[$productId] = $truncated; } } else { $this->logger->warning($job_id, 'API error without product context: ' . $apiError, [ 'type' => 'AI_PROVIDER', 'provider' => $providerName, 'api_error' => $apiError, ]); } continue; } $isAttachmentLevelImageError = !empty($rawItem['error']) && isset($rawItem['attachment_id'], $rawItem['status']) && (int) $rawItem['attachment_id'] > 0 && in_array(sanitize_text_field((string)$rawItem['status']), ['error', 'rejected'], true); if (!empty($rawItem['error']) && empty($rawItem['type']) && !$isAttachmentLevelImageError) { $productId = isset($rawItem['product_id']) ? (int)$rawItem['product_id'] : null; if ($productId && !isset($error_items[$productId])) { $error_items[$productId] = $this->truncateErrorMessage((string)$rawItem['error'], AutomationConstants::MAX_ERROR_MESSAGE_LENGTH); } continue; } $content_items[] = $rawItem; } } $unified_results = []; if (!empty($content_items)) { $unified_results = $this->result_processor->transformToUnifiedFormat($job_id, $content_items); } $completed_count = 0; $failed_count = 0; $failure_messages = []; $processed_product_ids = []; foreach ($error_items as $productId => $errorMsg) { if (!isset($processed_product_ids[$productId])) { if (!$this->persistTerminalFailure($job_id, $productId, $errorMsg, $claim_token)) { return ['completed' => $completed_count, 'failed' => $failed_count, 'failure_messages' => $failure_messages, 'claim_lost' => true]; } $processed_product_ids[$productId] = true; $failure_messages[$productId] = $errorMsg; $failed_count++; } } foreach ($unified_results as $result) { if (!isset($result['product_id']) || !is_numeric($result['product_id'])) { $this->logger->warning($job_id, 'Skipping result without valid product_id', [ 'product_id' => $result['product_id'] ?? null, 'fields' => array_keys($result), ]); continue; } $productId = (int)$result['product_id']; $result['created_by'] = (int) $job->created_by; if (isset($processed_product_ids[$productId])) { continue; } $item_content_settings = $content_settings; $heldBack = $this->blueprintHeldBackRouting($result, $content_settings); if (null !== $heldBack) { if (!$heldBack['routeToDraft']) { if (!$this->persistTerminalFailure($job_id, $productId, $heldBack['message'], $claim_token, $result)) { return ['completed' => $completed_count, 'failed' => $failed_count, 'failure_messages' => $failure_messages, 'claim_lost' => true]; } $processed_product_ids[$productId] = true; $failure_messages[$productId] = $heldBack['message']; $failed_count++; continue; } $item_content_settings = array_merge($content_settings, ['status' => 'draft']); $this->logger->info($job_id, sprintf( /* translators: %s: why the result needs review. */ __('The result needs review, so it was saved as a draft instead of applied: %s', 'ai-product-tools'), $heldBack['message'] ), [ 'product_id' => $productId, ]); } if (!$this->result_processor->hasApplicableContent($result)) { $no_content_message = $this->isBlockedByTheEngine($result) ? BlueprintIntegration::heldBackMessage($result) : ResultProcessor::NO_APPLICABLE_CONTENT_MESSAGE; $this->logger->warning($job_id, $no_content_message, [ 'product_id' => $productId, 'type' => 'AI_RESPONSE', ]); if (!$this->persistTerminalFailure($job_id, $productId, $no_content_message, $claim_token)) { return ['completed' => $completed_count, 'failed' => $failed_count, 'failure_messages' => $failure_messages, 'claim_lost' => true]; } $processed_product_ids[$productId] = true; $failure_messages[$productId] = $no_content_message; $failed_count++; continue; } try { if ($this->isCancelledJob($this->job_manager->getJob($job_id))) { return ['completed' => $completed_count, 'failed' => $failed_count, 'failure_messages' => $failure_messages, 'cancelled' => true]; } $outcome = $this->result_processor->applyResult($result, $item_content_settings, $this->draft_manager); if (!$this->persistTerminalSuccess($job_id, $productId, $result, $claim_token)) { return ['completed' => $completed_count, 'failed' => $failed_count, 'failure_messages' => $failure_messages, 'claim_lost' => true]; } $this->logger->info( $job_id, $this->successLogMessage($outcome), [ 'product_id' => $productId, 'destination' => $outcome, ] ); do_action('aipt_automation_result_succeeded', (int) $job_id, (int) $productId); $processed_product_ids[$productId] = true; $completed_count++; } catch (\Exception $e) { $this->logger->error($job_id, 'Failed to save result', [ 'product_id' => $productId, 'error' => $e->getMessage() ]); $error_message = 'Save failed: ' . $e->getMessage(); if (!$this->persistTerminalFailure($job_id, $productId, $error_message, $claim_token)) { return ['completed' => $completed_count, 'failed' => $failed_count, 'failure_messages' => $failure_messages, 'claim_lost' => true]; } $processed_product_ids[$productId] = true; $failure_messages[$productId] = $error_message; $failed_count++; } } return ['completed' => $completed_count, 'failed' => $failed_count, 'failure_messages' => $failure_messages]; } private function persistTerminalSuccess($job_id, $product_id, array $result, $claim_token = null) { if (is_string($claim_token) && $claim_token !== '') { return $this->queue->markCompleted((int)$job_id, (int)$product_id, $claim_token, $result); } $this->result_processor->recordSuccess((int)$job_id, (int)$product_id, $result); return true; } private function persistTerminalFailure($job_id, $product_id, $message, $claim_token = null, array $result_payload = []) { if (is_string($claim_token) && $claim_token !== '') { return $this->queue->markFailed((int)$job_id, (int)$product_id, $claim_token, (string)$message, $result_payload ?: null); } $this->result_processor->recordFailure((int)$job_id, (int)$product_id, (string)$message, $result_payload); return true; } /** * Whether a content structure unified result must not go through the * normal apply/draft routing as-is, and if so what to do about it. * * A legacy/advanced result (no executionPath key, or 'advanced') is * never held back here: BlueprintIntegration only attaches eligibility * to Blueprint-path results, so this returns null for every job that * does not run the Blueprint path — legacy automation behavior stays * byte-identical. * * @param array $result Unified per-product result (ResultProcessor::transformToUnifiedFormat()). * @param array $content_settings Job content settings (status: 'draft'|'published'). * @return array{routeToDraft:bool,message:string}|null */ private function blueprintHeldBackRouting(array $result, array $content_settings): ?array { if ('blueprint' !== ($result['executionPath'] ?? null)) { return null; } $status = strtolower((string) ($content_settings['status'] ?? 'published')); $destination = 'draft' === $status ? BlueprintIntegration::DESTINATION_REVIEW : BlueprintIntegration::DESTINATION_APPLY; if (!BlueprintIntegration::isHeldBack($result, $destination)) { return null; } $can_route_to_draft = BlueprintIntegration::DESTINATION_APPLY === $destination && (bool) apply_filters('aipt_draft_review_enabled', false); return [ 'routeToDraft' => $can_route_to_draft, 'message' => BlueprintIntegration::heldBackMessage($result), ]; } /** * Whether a unified result is a Blueprint run the engine itself blocked * (`eligibility.canApply` false - every AI unit the run asked for * failed). A legacy/advanced result (no `executionPath`, or one whose * canApply is true or absent) is never "blocked by the engine" here. * * @param array $result Unified per-product result. * @return bool */ private function isBlockedByTheEngine(array $result): bool { if ('blueprint' !== ($result['executionPath'] ?? null)) { return false; } $eligibility = $result['eligibility'] ?? null; return is_array($eligibility) && array_key_exists('canApply', $eligibility) && empty($eligibility['canApply']); } /** * Pause a job whose run cannot honor its paid behavior, before any * provider call, storing the user-visible reason. * * @param int $job_id Job ID. * @param array $block JobEntitlementGuard denial descriptor. * @return void */ private function pauseForEntitlement(int $job_id, array $block): void { $this->job_manager->updateJobStatus($job_id, 'paused', (string) $block['message']); $this->logger->warning($job_id, 'Job paused before generation: paid behavior cannot be honored', [ 'reason' => $block['reason'], 'feature' => $block['feature'], 'keys' => $block['keys'], ]); if (function_exists('wp_clear_scheduled_hook')) { wp_clear_scheduled_hook('aipt_execute_job_' . $job_id, [$job_id]); } } /** * Pause the job after a permanent, run-wide provider failure (a bad or * revoked key, no access to the model, an unknown model, an exhausted * quota, a missing key, AI turned off, or an unsupported provider), or * after a rate limit that outlasted an item's retries (the account's own * ceiling, Help Scout #921): * every further call this run would fail the identical way, so the job * stops instead of burning through its remaining products one doomed * call at a time. Mirrors pauseForEntitlement()'s pause-with-reason * shape; unlike a pool reset, the already-queued rows are left exactly * as they are (the caller already requeued the current item) so a * resume continues where the job left off. * * @param int $job_id Job ID. * @param ProviderCallException $failure The permanent run-wide failure. * @param string $provider_id AIPT provider id this call used. * @param string $merchant_message The merchant-facing sentence to store as the job's error. * @return void */ private function pauseForProviderFailure(int $job_id, ProviderCallException $failure, string $provider_id, string $merchant_message): void { $this->job_manager->updateJobStatus($job_id, 'paused', $merchant_message); $this->logger->warning($job_id, $merchant_message, [ 'reason' => $failure->reason(), 'provider' => $provider_id, 'type' => 'PROVIDER', ]); if (function_exists('wp_clear_scheduled_hook')) { wp_clear_scheduled_hook('aipt_execute_job_' . $job_id, [$job_id]); } } /** * Report the definitive denials a run falls back to Free behavior for. * * @param int $job_id Job ID. * @param array $clamps JobEntitlementGuard::assessRun() clamps. * @return void */ private function logRunClamps(int $job_id, array $clamps): void { foreach ($clamps as $clamp) { $this->logger->warning($job_id, $clamp['message'], [ 'reason' => $clamp['reason'], 'feature' => $clamp['feature'], 'keys' => $clamp['keys'], ]); } } /** * Surface Description's "sections from your store" skip notices * ("What customers say", "Compare with similar products") to this * job's own Logs modal, each the first time it actually fires for a * real product this tick — whichever of StoreSectionsPolicy's clamp * reasons applies: not entitled at all (e.g. * 'description:customerReviews'), or entitled but this run publishes * without review ('description:customerReviewsAutomation'). A * reviewGuaranteed=true run's not-entitled clamp is ALSO staged into * Content Review (see ReviewStager/ContentReviewStore's * section_notices), so that case is reported twice, truthfully; a * reviewGuaranteed=false run has no other channel at all, since * nothing is staged into review for it. * * EntitlementPolicy::apply()'s appliedFreeDefaults rides on every row * generateForAutomationProduct() returns via * BlueprintIntegration::responseFields() (merged into each row by * transformContentGeneratorResult()); this is the only place those * notices are read and logged — without it, the clamp still correctly * withholds the section, but nothing ever tells the merchant why. * * @param int $job_id Job id. * @param array $results generateForAutomationProduct()'s rows for one product. * @param array &$logged_keys Notice keys already logged this tick, updated in place. * @return void */ private function logStoreSectionClampNotices(int $job_id, array $results, array &$logged_keys): void { $notice_types = StoreSectionsPolicy::noticeTypesByKey(); foreach ($results as $row) { if (!is_array($row) || empty($row['appliedFreeDefaults']) || !is_array($row['appliedFreeDefaults'])) { continue; } foreach ($row['appliedFreeDefaults'] as $applied) { $key = is_array($applied) ? (string) ($applied['key'] ?? '') : ''; if (!isset($notice_types[$key]) || isset($logged_keys[$key])) { continue; } $this->logger->warning($job_id, (string) ($applied['reason'] ?? ''), [ 'key' => $key, ]); $logged_keys[$key] = true; } } } /** * Start the due cycle of a waiting recurring job. Without the recurring * entitlement the cycle is refused: next_run stays due and the stored job * says why. An entitled cycle is started by the Premium build * (`aipt_automation_start_recurring_cycle`): it moves next_run to the * following cycle, re-pins the saved structure choice and resets the pool * so it is rebuilt from the saved filter. * * @param AutomationJob $job Ready or completed recurring job whose cycle is due. * @return bool True when the cycle started (job is now preparing). */ private function startRecurringCycle(AutomationJob $job): bool { $job_id = (int) $job->id; $block = JobEntitlementGuard::recurringBlock(JobEntitlementGuard::CONTEXT_REARM); if (null !== $block) { $this->job_manager->updateJobStatus( $job_id, PlanDenial::isVerificationUnavailable($block['reason']) ? 'paused' : 'completed', $block['message'] ); $this->logger->warning($job_id, 'Recurring cycle not started: recurring schedule cannot be honored', [ 'reason' => $block['reason'], 'feature' => $block['feature'], ]); if (function_exists('wp_clear_scheduled_hook')) { wp_clear_scheduled_hook('aipt_execute_job_' . $job_id, [$job_id]); } return false; } /** * Start the due cycle of a recurring job the plan can run. * * The Premium RecurringCycleService is the sole registrant; without it * no cycle starts. * * @since 2.8.0 * * @param bool $started Whether the cycle started (the job is now preparing). Default false. * @param AutomationJob $job The job whose cycle is due. * @param AutomationJobManager $job_manager Job manager. * @param JobLoggerInterface $logger Job logger. */ return (bool) apply_filters('aipt_automation_start_recurring_cycle', false, $job, $this->job_manager, $this->logger); } /** * Whether the job has any pool rows. * * @param int $job_id Job ID. * @return bool */ private function hasPoolRows(int $job_id): bool { return (int) $this->wpdb->get_var( $this->wpdb->prepare( "SELECT COUNT(*) FROM {$this->job_products_table} WHERE job_id = %d", $job_id ) ) > 0; } private function isCancelledJob(?AutomationJob $job): bool { return null === $job || 'cancelled' === $job->status || ('failed' === $job->status && AutomationJobManager::CANCELLED_BY_USER_ERROR_MESSAGE === $job->error_message); } /** * Truncate error message to specified length with ellipsis. * * @param string $message Message to truncate * @param int $maxLength Maximum length * @return string Truncated message */ private function truncateErrorMessage(string $message, int $maxLength): string { if ($maxLength <= 3) { return $maxLength > 0 ? str_repeat('.', $maxLength) : ''; } $len = function_exists('mb_strlen') ? mb_strlen($message) : strlen($message); if ($len > $maxLength) { $truncateAt = $maxLength - 3; return (function_exists('mb_substr') ? mb_substr($message, 0, $truncateAt) : substr($message, 0, $truncateAt)) . '...'; } return $message; } /** * Update job progress * * @param int $job_id Job ID */ private function updateJobProgress($job_id) { $stats = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT COUNT(*) as total, SUM(CASE WHEN status IN ('completed', 'applied') THEN 1 ELSE 0 END) as completed, SUM(CASE WHEN status IN ('failed', 'rejected') THEN 1 ELSE 0 END) as failed FROM {$this->job_products_table} WHERE job_id = %d", $job_id ), ARRAY_A ); if ($stats) { $this->job_manager->updateJobProgress( $job_id, (int) $stats['completed'], (int) $stats['failed'] ); } } /** * Describe current job product queue state for logging/diagnostics. * * @param int $job_id * @return array */ private function describeJobProductState(int $job_id): array { $row = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS pending, SUM(CASE WHEN status = 'processing' THEN 1 ELSE 0 END) AS processing, SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) AS completed, SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) AS failed, MIN(CASE WHEN status = 'processing' THEN processing_started_at END) AS oldest_processing_started_at, MAX(CASE WHEN status = 'processing' THEN processing_started_at END) AS newest_processing_started_at, MIN(CASE WHEN status = 'pending' THEN created_at END) AS oldest_pending_created_at FROM {$this->job_products_table} WHERE job_id = %d", $job_id ), ARRAY_A ); $state = [ 'pending' => 0, 'processing' => 0, 'completed' => 0, 'failed' => 0, ]; if (is_array($row)) { $state['pending'] = (int) ($row['pending'] ?? 0); $state['processing'] = (int) ($row['processing'] ?? 0); $state['completed'] = (int) ($row['completed'] ?? 0); $state['failed'] = (int) ($row['failed'] ?? 0); $totalRows = array_sum([$state['pending'], $state['processing'], $state['completed'], $state['failed']]); $state['total_rows'] = $totalRows; $now = time(); if (!empty($row['oldest_processing_started_at'])) { $ts = SiteTime::toTimestamp($row['oldest_processing_started_at']); if ($ts !== false) { $state['oldest_processing_started_at'] = $row['oldest_processing_started_at']; $state['oldest_processing_age_sec'] = max(0, $now - $ts); } } if (!empty($row['newest_processing_started_at'])) { $ts = SiteTime::toTimestamp($row['newest_processing_started_at']); if ($ts !== false) { $state['newest_processing_started_at'] = $row['newest_processing_started_at']; $state['newest_processing_age_sec'] = max(0, $now - $ts); } } if (!empty($row['oldest_pending_created_at'])) { $ts = SiteTime::toTimestamp($row['oldest_pending_created_at']); if ($ts !== false) { $state['oldest_pending_created_at'] = $row['oldest_pending_created_at']; $state['oldest_pending_age_sec'] = max(0, $now - $ts); } } } else { $state['total_rows'] = 0; } return $state; } /** * Determine soft memory ceiling for ticks based on environment limits. * * @return int */ private function getSoftMemoryLimitBytes(): int { $default = AutomationConstants::DEFAULT_MEMORY_LIMIT_MB * 1024 * 1024; $limits = []; if (\function_exists('wp_convert_hr_to_bytes')) { $iniLimit = \ini_get('memory_limit'); if ($iniLimit && $iniLimit !== '-1') { $bytes = \wp_convert_hr_to_bytes($iniLimit); if ($bytes > 0) { $limits[] = (int) floor($bytes * 0.7); } } if (\defined('WP_MEMORY_LIMIT') && \WP_MEMORY_LIMIT !== '-1') { $wpLimit = \wp_convert_hr_to_bytes(\WP_MEMORY_LIMIT); if ($wpLimit > 0) { $limits[] = (int) floor($wpLimit * 0.7); } } } if (empty($limits)) { return $default; } return (int) min($limits); } /** * Get adaptive window size based on server capacity and recent performance * * Adjusts batch size dynamically: * - Reduces if memory pressure is high * - Reduces if recent errors detected * - Increases if system is healthy and fast * * @param int $job_id Job ID for error rate lookup * @return int Optimal window size for current conditions */ private function getAdaptiveWindowSize(int $job_id): int { $base = AutomationConstants::windowSize(); if (function_exists('memory_get_usage')) { $currentMem = memory_get_usage(true); $softLimit = $this->getSoftMemoryLimitBytes(); $memRatio = $softLimit > 0 ? ($currentMem / $softLimit) : 0; if ($memRatio > 0.8) { $base = (int) floor($base * 0.5); } elseif ($memRatio > 0.6) { $base = (int) floor($base * 0.75); } } $errorRateKey = 'aipt_job_error_rate_' . $job_id; $recentErrors = 0; if (function_exists('get_transient')) { $recentErrors = (int) get_transient($errorRateKey); } if ($recentErrors >= AutomationConstants::ERROR_THROTTLE_THRESHOLD) { $base = (int) floor($base * 0.5); } elseif ($recentErrors >= 2) { $base = (int) floor($base * 0.75); } $maxExecTime = (int) ini_get('max_execution_time'); if ($maxExecTime > 0 && $maxExecTime < 30) { $base = (int) floor($base * 0.6); } return max(self::MIN_WINDOW_SIZE, min(self::MAX_WINDOW_SIZE, $base)); } /** * Record error for adaptive throttling * * @param int $job_id Job ID */ private function recordErrorForThrottling(int $job_id): void { if (!function_exists('get_transient') || !function_exists('set_transient')) { return; } $key = 'aipt_job_error_rate_' . $job_id; $current = (int) get_transient($key); set_transient($key, $current + 1, AutomationConstants::ERROR_THROTTLE_WINDOW_SECONDS); } /** * Reset error counter on successful processing * * @param int $job_id Job ID */ private function resetErrorThrottling(int $job_id): void { if (function_exists('delete_transient')) { delete_transient('aipt_job_error_rate_' . $job_id); } } /** * Prevent noisy debug logs by caching snapshot hashes per job/slug. */ private function shouldLogDebugSnapshot(int $job_id, string $slug, array $context, int $ttl = AutomationConstants::DEBUG_SNAPSHOT_TTL): bool { if (!function_exists('get_transient') || !function_exists('set_transient')) { return true; } try { ksort($context); } catch (\Throwable $t) { } $hash = md5(wp_json_encode($context)); $key = 'aipt_jobdbg_' . $slug . '_' . $job_id; $stored = get_transient($key); if ($stored === $hash) { return false; } set_transient($key, $hash, $ttl); return true; } /** * Acquire per-job execution lock using atomic add_option() * * Uses add_option() for atomic check-and-set to prevent TOCTOU race conditions. * add_option() returns false if option already exists, providing atomicity. */ private function acquireLock(int $job_id): bool { $key = 'aipt_job_lock_' . $job_id; $token = wp_generate_password(AutomationConstants::LOCK_TOKEN_LENGTH, false, false); $lock_data = wp_json_encode([ 'token' => $token, 'expires' => time() + AutomationConstants::lockTtl(), ]); $acquired = add_option($key, $lock_data, '', 'no'); if ($acquired) { $this->lock_tokens[$job_id] = $token; return true; } $existing = get_option($key); if ($existing) { $data = json_decode($existing, true); if (is_array($data) && isset($data['expires']) && time() > (int) $data['expires']) { wp_cache_delete($key, 'options'); $fresh_existing = get_option($key); $fresh_data = json_decode($fresh_existing, true); if (is_array($fresh_data) && isset($fresh_data['expires']) && time() > (int) $fresh_data['expires']) { global $wpdb; $swapped = $wpdb->update( $wpdb->options, ['option_value' => $lock_data], ['option_name' => $key, 'option_value' => $fresh_existing], ['%s'], ['%s', '%s'] ); wp_cache_delete($key, 'options'); $acquired = 1 === (int) $swapped; } else { $acquired = false; } if ($acquired) { $this->lock_tokens[$job_id] = $token; return true; } } } return false; } /** * Release per-job execution lock (only if we own it) * * Only releases the lock if our stored token matches the one in the option, * preventing accidental release of another process's lock. */ private function releaseLock(int $job_id): void { $key = 'aipt_job_lock_' . $job_id; $myToken = $this->lock_tokens[$job_id] ?? null; if ($myToken) { $existing = get_option($key); if ($existing) { $data = json_decode($existing, true); if (is_array($data) && isset($data['token']) && $data['token'] === $myToken) { delete_option($key); } } } unset($this->lock_tokens[$job_id]); } /** * Renew a currently-held per-job execution lock's TTL. * * acquireLock() sets a fixed one-shot "expires" that a long-running tick * can outlive while still legitimately in flight (e.g. one AI provider * call whose own timeout is as long as LOCK_TTL). This refreshes the * option's "expires" so a slow-but-alive tick is never mistaken for an * abandoned one and stolen mid-flight. It only extends a lock we can * prove (via our own stored token) that we still hold; it never blindly * overwrites, so it cannot revive a lock another executor has already * taken over. * * @param int $job_id Job ID. * @return bool True if the lock is confirmed ours and was renewed. */ private function renewLock(int $job_id): bool { $myToken = $this->lock_tokens[$job_id] ?? null; if (!$myToken) { return false; } $key = 'aipt_job_lock_' . $job_id; wp_cache_delete($key, 'options'); $existing = get_option($key); if (!$existing) { return false; } $data = json_decode($existing, true); if (!is_array($data) || !isset($data['token']) || $data['token'] !== $myToken) { return false; } $lock_data = wp_json_encode([ 'token' => $myToken, 'expires' => time() + AutomationConstants::lockTtl(), ]); global $wpdb; $renewed = $wpdb->update( $wpdb->options, ['option_value' => $lock_data], ['option_name' => $key, 'option_value' => $existing], ['%s'], ['%s', '%s'] ); wp_cache_delete($key, 'options'); if (false === $renewed) { return false; } if (0 === (int) $renewed) { $after = get_option($key); $after_data = is_string($after) ? json_decode($after, true) : null; return is_array($after_data) && isset($after_data['token']) && $after_data['token'] === $myToken; } return true; } /** * Job log line for one product's apply outcome. * * @param string $outcome 'draft', 'published' or 'unchanged' from ResultProcessor::applyResult(). */ private function successLogMessage(string $outcome): string { if ('draft' === $outcome) { return __('Saved the result as a draft for review', 'ai-product-tools'); } if ('unchanged' === $outcome) { return __('Left the product unchanged: nothing needed to be written', 'ai-product-tools'); } return __('Updated the product', 'ai-product-tools'); } /** * Estimate job completion time using actual processing times * * Uses moving average of real processing durations from job_products table * to provide accurate ETA that excludes cron idle time between ticks. * * @param AutomationJob $job Job instance * @return string|null Estimated completion time in Y-m-d H:i:s format */ private function estimateCompletion(AutomationJob $job) { if ($job->status !== 'running' || $job->processed_products == 0) { return null; } $remaining_products = max(0, $job->total_products - ($job->processed_products + $job->failed_products)); if ($remaining_products <= 0) { return null; } $sample_size = min(AutomationConstants::MAX_SAMPLE_SIZE, max(AutomationConstants::MIN_SAMPLE_SIZE, $job->processed_products)); $avg_seconds = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT AVG(TIMESTAMPDIFF(SECOND, processing_started_at, processed_at)) FROM ( SELECT processing_started_at, processed_at FROM {$this->job_products_table} WHERE job_id = %d AND status = 'completed' AND processing_started_at IS NOT NULL AND processed_at IS NOT NULL AND processed_at > processing_started_at ORDER BY processed_at DESC LIMIT %d ) AS recent_products", (int) $job->id, $sample_size ) ); if (!$avg_seconds || $avg_seconds <= 0) { $started_ts = SiteTime::toTimestamp($job->started_at); $elapsed_time = false !== $started_ts ? time() - $started_ts : 0; if ($elapsed_time > 0 && $job->processed_products > 0) { $avg_seconds = $elapsed_time / $job->processed_products; } else { return null; } } $cron_overhead_per_item = AutomationConstants::rescheduleDelay() / AutomationConstants::windowSize(); $effective_avg = (float) $avg_seconds + $cron_overhead_per_item; $estimated_seconds = $remaining_products * $effective_avg; $max_seconds = AutomationConstants::MAX_COMPLETION_ESTIMATE_DAYS * DAY_IN_SECONDS; if ($estimated_seconds > $max_seconds) { $estimated_seconds = $max_seconds; } return gmdate('Y-m-d H:i:s', time() + (int) $estimated_seconds); } /** * Call a generator's Blueprint-aware entry point for one Automation * product, keeping the trusted facts scoped to this one call only. * * This job's own "Content structure" choice — already computed into * $options['jobMode']/['legacyPrompts'] by OptionsTranslator from the * job's DB-stored settings, never from a live REST request — is set as * BlueprintIntegration's ambient trusted facts for the duration of this * one call only, then cleared (finally), so each generator's own * Blueprint fork reads the job's real choice instead of silently * falling through to the generator's site-wide mode. Because the * channel is ambient rather than threaded through $options itself, a * REST-shaped options array (bulk generate, Product Editor, the * metabox) has no way to reach it: those callers never run through * this method, so the channel stays empty for them and their execution * path is unaffected. * * Every automation generator catches its own provider failures internally * and returns a failed row - carrying api_error/error plus a providerError * (ProviderCallException::toResponseArray()) shape - instead of throwing. * Before returning, this inspects the row(s) and rebuilds + throws a * ProviderCallException when one is run-wide, so the tick loop's * catch(\Throwable) block (executeJob()) still classifies it: see * throwForRunWideProviderFailure(). * * @param object $service One of TitleGeneratorService|FAQGeneratorService|SeoMetaGeneratorService|ImageAltService|ContentGenerator. * @param int $product_id Product id for this call. * @param array $options This job's translated options (OptionsTranslator::translate()). * @param int $created_by This job's creator, set as BlueprintIntegration's * ambient acting user for the same call window as the * trusted facts above: WP-Cron runs with no logged-in * user, so the engine's object-permission check * (ExecutionContextBuilder::checkObjectPermission(), via * BlueprintIntegration::run()) must check THIS user's * capability instead of the absent current one. * @param bool $reviewGuaranteed This job's job-uniform * JobEntitlementGuard::reviewGuaranteed(), set as * BlueprintIntegration's ambient trusted fact for the * same call window, so EntitlementPolicy::apply() can * clamp Description's "What customers say" section * when an Automation run will publish without review. * @return array Results in each service's own existing shape. * @throws \Exception Unknown service type. * @throws ProviderCallException When a returned row's providerError says the failure is run-wide. */ private function generateForAutomationProduct($service, int $product_id, array $options, int $created_by = 0, bool $reviewGuaranteed = false): array { BlueprintIntegration::setJobTrustedFacts([ 'jobMode' => isset($options['jobMode']) ? (string) $options['jobMode'] : '', 'legacyPrompts' => !empty($options['legacyPrompts']), ]); BlueprintIntegration::setJobActorUserId($created_by); BlueprintIntegration::setJobReviewGuaranteed($reviewGuaranteed); try { if ($service instanceof TitleGeneratorService || $service instanceof FAQGeneratorService || $service instanceof SeoMetaGeneratorService || $service instanceof ImageAltService) { $results = $service->bulk_generate([$product_id], $options); } elseif ($service instanceof ContentGenerator) { $contentTypes = $this->extractContentTypes($options); $productResult = $service->generateForProduct($product_id, $contentTypes, $options); $results = $this->transformContentGeneratorResult($productResult, $product_id); } else { throw new \Exception('Unknown service type: ' . get_class($service)); } $this->throwForRunWideProviderFailure($results, (string) ($options['provider'] ?? '')); return $results; } finally { BlueprintIntegration::setJobTrustedFacts([]); BlueprintIntegration::setJobActorUserId(null); BlueprintIntegration::setJobReviewGuaranteed(null); } } /** * Rebuild and throw a ProviderCallException when a returned row's * providerError says the failure is run-wide (a revoked/missing key, no * model access, an exhausted quota, a rate limit, an outage, a network * failure, AI turned off, or an unsupported provider): every further call * to this provider in this run would fail the same way. This is the real * production path's only source of that exception today - every * generator catches its own ProviderCallException and returns a failed * row instead of throwing - so without this, the tick loop's * catch(\Throwable) run-wide classification (pause-and-requeue for a * permanent reason, a capped retry for a transient one, the * PROVIDER_QUOTA_LIMIT hook) never ran. * * A row whose providerError is absent, null, or not run-wide (content * blocked, truncated output, an unavailable image, a rejected schema, * ...) is left alone: it keeps today's per-item failure path through * processResults(), unaffected by this check. So is a row that carries * a run-wide providerError but no error of its own: a partial content * structure result that still has usable content. * * @param array $results Rows generateForAutomationProduct() is about to return, for the single product this call was for. * @param string $fallback_provider Provider id to use when a row's providerError carries none. * @return void * @throws ProviderCallException When a row's providerError is run-wide. */ private function throwForRunWideProviderFailure(array $results, string $fallback_provider): void { foreach ($results as $row) { if (!is_array($row)) { continue; } $providerError = $row['providerError'] ?? null; if (!is_array($providerError) || empty($providerError['runWide'])) { continue; } $failed = '' !== trim((string) ($row['api_error'] ?? '')) || '' !== trim((string) ($row['error'] ?? '')) || 'error' === ($row['status'] ?? ''); if (!$failed) { continue; } $reason = isset($providerError['reason']) ? (string) $providerError['reason'] : ''; if ('' === $reason) { continue; } $provider = (isset($providerError['provider']) && '' !== (string) $providerError['provider']) ? (string) $providerError['provider'] : $fallback_provider; throw new ProviderCallException( isset($providerError['message']) ? (string) $providerError['message'] : '', $reason, isset($providerError['httpStatus']) ? (int) $providerError['httpStatus'] : null, '', isset($providerError['retryAfter']) ? (int) $providerError['retryAfter'] : null, $provider ); } } /** * Extract content types from options for ContentGenerator * * Converts boolean flags (generateDescription, generateShortDescription, generateTags) * to an array of content type strings that ContentGenerator expects. * * @param array $options Job options containing boolean content type flags * @return array Array of content type strings (e.g., ['description', 'short_description', 'tags']) */ private function extractContentTypes(array $options): array { $types = []; if (!empty($options['generateDescription'])) { $types[] = 'description'; } if (!empty($options['generateShortDescription'])) { $types[] = 'short_description'; } if (!empty($options['generateTags'])) { $types[] = 'tags'; } if (empty($types)) { $types[] = 'description'; } $max = (int) apply_filters('aipt_max_job_content_types', 1); if ($max > 0) { $types = array_slice($types, 0, $max); } return $types; } /** * Transform ContentGenerator ProductResult to expected array format * * ContentGenerator returns ProductResult objects with a different structure. * This method transforms them to the array format expected by the automation pipeline. * * Every row also carries the ProductResult's own blueprintInfo (executionPath/ * blueprint/findings/eligibility/providerError), exactly like Title/SeoMeta/FAQ/ * ImageAlt's own raw items already do (`array_merge($item, $blueprint_fields)`). * Without this, ResultProcessor::transformToUnifiedFormat() never sees an * executionPath for a Description result, so JobExecutor::blueprintHeldBackRouting()/ * isBlockedByTheEngine() silently treat every Description automation item as a * legacy/advanced one - a Blueprint result the engine held back for review * (hard_limit_exceeded, say) could reach a live apply with no held-back check at * all. For the Advanced (custom-prompts) path $result->blueprintInfo is the * ProductResult class default (an empty array), so this merge is a no-op there. * * @param ProductResult $result The ProductResult from ContentGenerator * @param int $productId The product ID * @return array Array of result items compatible with automation pipeline */ private function transformContentGeneratorResult($result, int $productId): array { $output = []; if (is_object($result)) { $content = $result->content ?? []; $success = $result->success ?? false; $blueprintInfo = is_array($result->blueprintInfo ?? null) ? $result->blueprintInfo : []; $providerError = $blueprintInfo['providerError'] ?? ($result->providerError ?? null); if (!$success && !empty($result->errors)) { return [ array_merge( [ 'product_id' => $productId, 'error' => implode(', ', $result->errors), ], $blueprintInfo, ['providerError' => $providerError] ), ]; } foreach ($content as $type => $text) { if (empty($text)) { continue; } $typeMap = [ 'short_description' => 'short', 'description' => 'full', 'tags' => 'tags', ]; $dbType = $typeMap[$type] ?? $type; $output[] = array_merge( [ 'product_id' => $productId, 'type' => $dbType, 'text' => $text, ], $blueprintInfo, ['providerError' => $providerError] ); } if (empty($output) && !$success) { $errorMsg = !empty($result->errors) ? implode(', ', $result->errors) : 'AI provider returned no content'; $output[] = array_merge( [ 'product_id' => $productId, 'error' => $errorMsg, ], $blueprintInfo, ['providerError' => $providerError] ); } } elseif (is_array($result)) { foreach ($result as $item) { if (is_array($item)) { $item['product_id'] = $item['product_id'] ?? $productId; $output[] = $item; } } } return $output; } /** * Dispatch async runner to trigger immediate cron execution. * * Attempts multiple methods to trigger cron processing immediately * instead of waiting for external page hits. This significantly * improves job processing speed on low-traffic sites. */ private function dispatchAsyncRunner(): void { if (class_exists('ActionScheduler_AsyncRequest_QueueRunner')) { try { $async_request = new \ActionScheduler_AsyncRequest_QueueRunner( \ActionScheduler::store() ); $async_request->maybe_dispatch(); } catch (\Exception $e) { } } JobScheduler::spawnDueCron(); } }