fix: move Generate Now to background processing to avoid 120s timeout

Hostinger's LiteSpeed web server kills any HTTP connection that runs
longer than 120 seconds with a 500 "Request Timeout" error. The full
pipeline (Reddit fetch → topic scoring → article generation → editorial
pass → image gen ×2) takes 120-180s, causing "Request failed: error"
on every manual generation attempt.

Architecture change:
- Generate Now AJAX handler now schedules a one-shot WP-Cron event
  and returns immediately (~100ms response time)
- New prautoblogger_manual_generation cron action runs the pipeline
  in a separate PHP process free from the connection timeout
- Pipeline progress is written to a transient at each stage
- New prautoblogger_generation_status AJAX endpoint lets the frontend
  poll progress every 5 seconds
- Frontend auto-resumes polling on page load if a generation is running
- Direct wp-cron.php loopback ensures cron fires even if DISABLE_WP_CRON
  is set (common on Hostinger)

Version bump: 0.2.9 → 0.3.0

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-18 13:52:10 +07:00
parent b17ed4719b
commit b5bbc54f87
5 changed files with 263 additions and 43 deletions

View File

@@ -85,6 +85,10 @@ class PRAutoBlogger {
add_action( 'prautoblogger_daily_generation', [ $this->executor, 'on_daily_generation' ] );
add_action( 'prautoblogger_collect_metrics', [ $this->executor, 'on_collect_metrics' ] );
// Manual "Generate Now" runs as a one-shot cron event to avoid
// Hostinger's 120-second web-server connection timeout.
add_action( 'prautoblogger_manual_generation', [ $this->executor, 'on_manual_generation' ] );
$registry = $this->executor->get_model_registry();
add_action( 'prautoblogger_refresh_model_registry', [ $registry, 'refresh' ] );
}
@@ -92,6 +96,7 @@ class PRAutoBlogger {
/** Register AJAX handlers for admin actions. */
private function register_ajax_hooks(): void {
add_action( 'wp_ajax_prautoblogger_generate_now', [ $this->executor, 'on_ajax_generate_now' ] );
add_action( 'wp_ajax_prautoblogger_generation_status', [ $this->executor, 'on_ajax_generation_status' ] );
add_action( 'wp_ajax_prautoblogger_generate_image', [ $this->executor, 'on_ajax_generate_image' ] );
add_action( 'wp_ajax_prautoblogger_test_connection', [ $this->executor, 'on_ajax_test_connection' ] );
add_action( 'wp_ajax_prautoblogger_get_models', [ $this->executor, 'on_ajax_get_models' ] );