Files
profile/inc/wp-cli-scheduler-commands.php

159 lines
4.4 KiB
PHP
Raw Permalink Normal View History

2026-03-09 20:51:08 +03:00
<?php
class Scheduler_CLI_Commands extends WP_CLI_Command {
/**
* Принудительно запустить задачу Action Scheduler, даже если она failed
*
* ## OPTIONS
*
* <action_id>
* : ID задачи Action Scheduler
*
* [--retry]
* : Сбросить статус failed перед запуском
*
* ## EXAMPLES
*
* wp scheduler force-run-action 11843340
* wp scheduler force-run-action 456 --retry
*
* @param array $args
* @param array $assoc_args
*/
public function force_run_action( $args, $assoc_args ) {
list( $action_id ) = $args;
$retry = isset( $assoc_args['retry'] );
WP_CLI::log( "Принудительный запуск задачи ID: {$action_id}" );
global $wpdb;
// Получаем информацию о задаче
$action = $wpdb->get_row( $wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}actionscheduler_actions WHERE action_id = %d",
$action_id
) );
if ( ! $action ) {
WP_CLI::error( "Задача с ID {$action_id} не найдена." );
return;
}
WP_CLI::log( "Статус: " . $action->status );
WP_CLI::log( "Хук: " . $action->hook );
// Если нужно сбросить статус failed
if ( $retry && 'failed' === $action->status ) {
$updated = $wpdb->update(
"{$wpdb->prefix}actionscheduler_actions",
array( 'status' => 'pending' ),
array( 'action_id' => $action_id ),
array( '%s' ),
array( '%d' )
);
if ( $updated ) {
WP_CLI::log( "Статус сброшен с failed на pending" );
}
}
// Запускаем задачу через Action Scheduler
try {
// Создаем экземпляр задачи
$store = ActionScheduler::store();
$action_obj = $store->fetch_action( $action_id );
if ( ! $action_obj ) {
WP_CLI::error( "Не удалось создать объект задачи." );
return;
}
WP_CLI::log( "Запуск задачи..." );
// Выполняем задачу
$start_time = microtime( true );
$action_obj->execute();
$execution_time = microtime( true ) - $start_time;
// Проверяем новый статус
$new_status = $wpdb->get_var( $wpdb->prepare(
"SELECT status FROM {$wpdb->prefix}actionscheduler_actions WHERE action_id = %d",
$action_id
) );
WP_CLI::success( sprintf(
"Задача выполнена за %.2f секунд. Новый статус: %s",
$execution_time,
$new_status
) );
} catch ( Exception $e ) {
WP_CLI::error( "Ошибка выполнения: " . $e->getMessage() );
}
}
/**
* Сбросить статус failed задачи на pending
*
* ## OPTIONS
*
* <action_id>
* : ID задачи Action Scheduler
*
* ## EXAMPLES
*
* wp scheduler reset-failed-action 123
*/
public function reset_failed_action( $args, $assoc_args ) {
list( $action_id ) = $args;
global $wpdb;
$action = $wpdb->get_row( $wpdb->prepare(
"SELECT status FROM {$wpdb->prefix}actionscheduler_actions WHERE action_id = %d",
$action_id
) );
if ( ! $action ) {
WP_CLI::error( "Задача с ID {$action_id} не найдена." );
return;
}
if ( 'failed' !== $action->status ) {
WP_CLI::warning( "Статус задачи не 'failed', а '{$action->status}'. Сброс не требуется." );
return;
}
$updated = $wpdb->update(
"{$wpdb->prefix}actionscheduler_actions",
array( 'status' => 'pending' ),
array( 'action_id' => $action_id ),
array( '%s' ),
array( '%d' )
);
if ( $updated ) {
WP_CLI::success( "Статус задачи сброшен с failed на pending." );
} else {
WP_CLI::error( "Не удалось сбросить статус задачи." );
}
}
}
// Регистрируем команды - ЭТО ОБЯЗАТЕЛЬНО!
WP_CLI::add_command( 'scheduler', 'Scheduler_CLI_Commands' );