diff --git a/includes/cli/class-wpvdb-cli.php b/includes/cli/class-embeddings-command.php similarity index 55% rename from includes/cli/class-wpvdb-cli.php rename to includes/cli/class-embeddings-command.php index 4ebcb49..7640eb6 100644 --- a/includes/cli/class-wpvdb-cli.php +++ b/includes/cli/class-embeddings-command.php @@ -1,6 +1,6 @@ ] - * : Number of jobs to list. Default 20. - * - * [--format=] - * : Output format. Default: table. - * --- - * default: table - * options: - * - table - * - csv - * - json - * - yaml - * --- - * - * @param array $args Positional arguments. - * @param array $assoc_args Associative arguments. - * @return void - */ - public function list( $args, $assoc_args ) { - $limit = isset( $assoc_args['limit'] ) ? (int) $assoc_args['limit'] : 20; - $format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table'; - $rows = Embedding_Enqueuer::list_jobs( $limit ); - if ( empty( $rows ) ) { - \WP_CLI::log( 'No jobs found.' ); - return; - } - $fields = array( 'job_id', 'status', 'provider', 'model', 'queued_count', 'scanned_count', 'skipped_count', 'last_seen_id', 'upper_bound_id', 'updated_at' ); - \WP_CLI\Utils\format_items( $format, $rows, $fields ); - } - - /** - * Print a job's current state. - * - * ## OPTIONS - * - * - * : The job ID. - * - * @param array $args Positional arguments. - * @param array $assoc_args Associative arguments. - * @return void - */ - public function status( $args, $assoc_args ) { - $job_id = isset( $args[0] ) ? (int) $args[0] : 0; - if ( $job_id <= 0 ) { - \WP_CLI::error( 'Missing job_id argument.' ); - } - $job = Embedding_Enqueuer::get_job( $job_id ); - if ( ! $job ) { - \WP_CLI::error( "Job {$job_id} not found." ); - } - foreach ( $job as $key => $value ) { - if ( 'scope_args' === $key && is_string( $value ) ) { - $decoded = json_decode( $value, true ); - $value = wp_json_encode( $decoded ); - } - \WP_CLI::log( str_pad( (string) $key, 18 ) . ' : ' . (string) $value ); - } - } - - /** - * Cancel a job. Already-scheduled AS pages will exit early on their next firing. - * - * ## OPTIONS - * - * - * : The job ID. - * - * @param array $args Positional arguments. - * @param array $assoc_args Associative arguments. - * @return void - */ - public function cancel( $args, $assoc_args ) { - $job_id = isset( $args[0] ) ? (int) $args[0] : 0; - if ( $job_id <= 0 ) { - \WP_CLI::error( 'Missing job_id argument.' ); - } - if ( Embedding_Enqueuer::cancel_job( $job_id ) ) { - \WP_CLI::success( "Job {$job_id} canceled." ); - } else { - \WP_CLI::error( "Failed to cancel job {$job_id}." ); - } - } - - /** - * Resume a paused job. - * - * ## OPTIONS - * - * - * : The job ID. - * - * @param array $args Positional arguments. - * @param array $assoc_args Associative arguments. - * @return void - */ - public function resume( $args, $assoc_args ) { - $job_id = isset( $args[0] ) ? (int) $args[0] : 0; - if ( $job_id <= 0 ) { - \WP_CLI::error( 'Missing job_id argument.' ); - } - $result = Embedding_Enqueuer::resume_job( $job_id ); - if ( is_wp_error( $result ) ) { - \WP_CLI::error( $result->get_error_message() ); - } - if ( $result ) { - \WP_CLI::success( "Job {$job_id} resumed." ); - } else { - \WP_CLI::error( "Job {$job_id} is not in a paused state." ); - } - } -} - \WP_CLI::add_command( 'wpvdb embeddings', __NAMESPACE__ . '\\Embeddings_Command' ); -\WP_CLI::add_command( 'wpvdb embeddings job', __NAMESPACE__ . '\\Jobs_Command' ); diff --git a/includes/cli/class-jobs-command.php b/includes/cli/class-jobs-command.php new file mode 100644 index 0000000..f1ad9b1 --- /dev/null +++ b/includes/cli/class-jobs-command.php @@ -0,0 +1,141 @@ +] + * : Number of jobs to list. Default 20. + * + * [--format=] + * : Output format. Default: table. + * --- + * default: table + * options: + * - table + * - csv + * - json + * - yaml + * --- + * + * @param array $args Positional arguments. + * @param array $assoc_args Associative arguments. + * @return void + */ + public function list( $args, $assoc_args ) { + $limit = isset( $assoc_args['limit'] ) ? (int) $assoc_args['limit'] : 20; + $format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table'; + $rows = Embedding_Enqueuer::list_jobs( $limit ); + if ( empty( $rows ) ) { + \WP_CLI::log( 'No jobs found.' ); + return; + } + $fields = array( 'job_id', 'status', 'provider', 'model', 'queued_count', 'scanned_count', 'skipped_count', 'last_seen_id', 'upper_bound_id', 'updated_at' ); + \WP_CLI\Utils\format_items( $format, $rows, $fields ); + } + + /** + * Print a job's current state. + * + * ## OPTIONS + * + * + * : The job ID. + * + * @param array $args Positional arguments. + * @param array $assoc_args Associative arguments. + * @return void + */ + public function status( $args, $assoc_args ) { + $job_id = isset( $args[0] ) ? (int) $args[0] : 0; + if ( $job_id <= 0 ) { + \WP_CLI::error( 'Missing job_id argument.' ); + } + $job = Embedding_Enqueuer::get_job( $job_id ); + if ( ! $job ) { + \WP_CLI::error( "Job {$job_id} not found." ); + } + foreach ( $job as $key => $value ) { + if ( 'scope_args' === $key && is_string( $value ) ) { + $decoded = json_decode( $value, true ); + $value = wp_json_encode( $decoded ); + } + \WP_CLI::log( str_pad( (string) $key, 18 ) . ' : ' . (string) $value ); + } + } + + /** + * Cancel a job. Already-scheduled AS pages will exit early on their next firing. + * + * ## OPTIONS + * + * + * : The job ID. + * + * @param array $args Positional arguments. + * @param array $assoc_args Associative arguments. + * @return void + */ + public function cancel( $args, $assoc_args ) { + $job_id = isset( $args[0] ) ? (int) $args[0] : 0; + if ( $job_id <= 0 ) { + \WP_CLI::error( 'Missing job_id argument.' ); + } + if ( Embedding_Enqueuer::cancel_job( $job_id ) ) { + \WP_CLI::success( "Job {$job_id} canceled." ); + } else { + \WP_CLI::error( "Failed to cancel job {$job_id}." ); + } + } + + /** + * Resume a paused job. + * + * ## OPTIONS + * + * + * : The job ID. + * + * @param array $args Positional arguments. + * @param array $assoc_args Associative arguments. + * @return void + */ + public function resume( $args, $assoc_args ) { + $job_id = isset( $args[0] ) ? (int) $args[0] : 0; + if ( $job_id <= 0 ) { + \WP_CLI::error( 'Missing job_id argument.' ); + } + $result = Embedding_Enqueuer::resume_job( $job_id ); + if ( is_wp_error( $result ) ) { + \WP_CLI::error( $result->get_error_message() ); + } + if ( $result ) { + \WP_CLI::success( "Job {$job_id} resumed." ); + } else { + \WP_CLI::error( "Job {$job_id} is not in a paused state." ); + } + } +} + +\WP_CLI::add_command( 'wpvdb embeddings job', __NAMESPACE__ . '\\Jobs_Command' ); diff --git a/wpvdb.php b/wpvdb.php index 5ed45e0..37f8634 100644 --- a/wpvdb.php +++ b/wpvdb.php @@ -75,7 +75,8 @@ require_once WPVDB_PLUGIN_DIR . 'includes/class-wpvdb-embedding-enqueuer.php'; require_once WPVDB_PLUGIN_DIR . 'includes/class-wpvdb-plugin.php'; if ( defined( 'WP_CLI' ) && WP_CLI ) { - require_once WPVDB_PLUGIN_DIR . 'includes/cli/class-wpvdb-cli.php'; + require_once WPVDB_PLUGIN_DIR . 'includes/cli/class-embeddings-command.php'; + require_once WPVDB_PLUGIN_DIR . 'includes/cli/class-jobs-command.php'; } if ( ! function_exists( 'wpvdb_has_action_scheduler' ) ) {