prefix . 'ai_app_onsite_user_statistics'; $data = array( 'webhook_status' => $job_data['status'], 'webhook_scheduled_at' => gmdate('Y-m-d H:i:s', $job_data['scheduled_at']), 'webhook_attempts' => 0, 'updated_at' => current_time('mysql') ); // Update existing record by response_id $result = $wpdb->update( $table, $data, array('response_id' => $response_id), array('%s', '%s', '%d', '%s'), array('%s') ); return $result !== false; } } if (!function_exists('aiapp_jobs_get')) { /** * Get a webhook job by response_id */ function aiapp_jobs_get($response_id): ?array { global $wpdb; $table = $wpdb->prefix . 'ai_app_onsite_user_statistics'; $job = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE response_id = %s AND webhook_status IS NOT NULL", $response_id ), ARRAY_A ); if (!$job) { return null; } return array( 'response_id' => $job['response_id'], 'app_id' => $job['app_id'], 'status' => $job['webhook_status'], 'scheduled_at' => strtotime($job['webhook_scheduled_at']), 'attempts' => (int) $job['webhook_attempts'], 'last_error' => $job['webhook_last_error'], 'payload' => array( 'ai_response' => $job['ai_output'], 'app_id' => $job['app_id'], 'prompt' => $job['prompt'], 'data' => $job['data'], 'user_ip' => $job['user_ip'], 'user_geolocation' => $job['user_geolocation'] ) ); } } if (!function_exists('aiapp_jobs_list_overdue')) { /** * Get overdue webhook jobs that should have been processed */ function aiapp_jobs_list_overdue($current_time) { global $wpdb; $table = $wpdb->prefix . 'ai_app_onsite_user_statistics'; return $wpdb->get_results( $wpdb->prepare( "SELECT response_id, app_id FROM {$table} WHERE webhook_status = 'queued' AND webhook_scheduled_at <= %s AND webhook_attempts < 3 ORDER BY webhook_scheduled_at ASC LIMIT 50", gmdate('Y-m-d H:i:s', $current_time) ), ARRAY_A ); } } if (!function_exists('aiapp_jobs_mark_sent')) { /** * Mark a job as successfully sent */ function aiapp_jobs_mark_sent($response_id): bool { global $wpdb; $table = $wpdb->prefix . 'ai_app_onsite_user_statistics'; $result = $wpdb->update( $table, array( 'webhook_status' => 'sent', 'updated_at' => current_time('mysql') ), array('response_id' => $response_id), array('%s', '%s'), array('%s') ); return $result !== false; } } if (!function_exists('aiapp_jobs_mark_failed')) { /** * Mark a job as failed and optionally schedule retry */ function aiapp_jobs_mark_failed($response_id, $error_message = '', $schedule_retry = true): bool { global $wpdb; $table = $wpdb->prefix . 'ai_app_onsite_user_statistics'; $job = aiapp_jobs_get($response_id); if (!$job) { return false; } $attempts = $job['attempts'] + 1; $status = ($attempts >= 3 || !$schedule_retry) ? 'failed' : 'queued'; $data = array( 'webhook_status' => $status, 'webhook_attempts' => $attempts, 'webhook_last_error' => substr($error_message, 0, 500), 'updated_at' => current_time('mysql') ); // If retrying, schedule for later (exponential backoff) if ($status === 'queued' && $schedule_retry) { $delay = min(300 * pow(2, $attempts - 1), 1800); // 5min, 10min, 30min max $data['webhook_scheduled_at'] = gmdate('Y-m-d H:i:s', time() + $delay); } $result = $wpdb->update( $table, $data, array('response_id' => $response_id), array('%s', '%d', '%s', '%s', '%s'), array('%s') ); //create the entry in the app_onsite_app_usage_statistics where response_id is the same as the webhook response_id,update the webhook_triggered column $usage_table = $wpdb->prefix . 'ai_app_onsite_app_usage_statistics'; $wpdb->update( $usage_table, array( 'webhook_triggered' => 'Y', 'updated_at' => current_time('mysql') ), array('response_id' => $response_id), array('%s', '%s'), array('%s') ); return $result !== false; } }