This commit is contained in:
Javier Casares 2026-02-18 16:09:17 +00:00
commit 768bbfbdd2
6 changed files with 150 additions and 34 deletions

View file

@ -7,12 +7,17 @@
*
* This script runs as a standalone CLI process, independent of WordPress cron.
* It loads WordPress with DOING_CRON defined to prevent triggering actual cron.
* Batch size is read from the plugin settings in the database no need to pass it
* as a command-line argument.
*
* Usage: php worker.php <batch_size> <worker_id> [--url=<site_url>]
* Example: php worker.php 10 "w1"
* MultiSite: php worker.php 10 "w1" --url="https://example.com"
* Parallel: seq 1 10 | xargs -P10 -I{} php worker.php 10 "w{}"
* Parallel MultiSite: seq 1 10 | xargs -P10 -I{} php worker.php 10 "w{}" --url="https://example.com"
* Usage: php worker.php <worker_id> [--url=<site_url>]
* Example: php worker.php "w1"
* MultiSite: php worker.php "w1" --url="https://example.com"
* Parallel: seq 1 10 | xargs -P10 -I{} php worker.php "w{}"
* Parallel MS: seq 1 10 | xargs -P10 -I{} php worker.php "w{}" --url="https://example.com"
*
* Legacy format (deprecated, still supported):
* php worker.php <batch_size> <worker_id> [--url=<site_url>]
*
* @package ROBOTSTXT_SMTP_Newsletter
* @since 2.2.0
@ -28,19 +33,29 @@ if ( php_sapi_name() !== 'cli' ) {
}
// Parse command-line arguments.
if ( $argc < 3 ) {
fwrite( STDERR, "Usage: php worker.php <batch_size> <worker_id> [--url=<site_url>]\n" );
fwrite( STDERR, "Example: php worker.php 10 \"w1\"\n" );
fwrite( STDERR, "MultiSite: php worker.php 10 \"w1\" --url=\"https://example.com\"\n" );
// New format: php worker.php <worker_id> [--url=<site_url>]
// Legacy format (deprecated): php worker.php <batch_size> <worker_id> [--url=<site_url>]
if ( $argc < 2 ) {
fwrite( STDERR, "Usage: php worker.php <worker_id> [--url=<site_url>]\n" );
fwrite( STDERR, "Example: php worker.php \"w1\"\n" );
fwrite( STDERR, "MultiSite: php worker.php \"w1\" --url=\"https://example.com\"\n" );
exit( 1 );
}
$batch_size = max( 1, (int) $argv[1] );
$worker_id = $argv[2];
// Detect legacy format: first arg is numeric (old batch_size), second arg is worker_id.
$arg_offset = 2;
if ( is_numeric( $argv[1] ) && $argc >= 3 && 0 !== strpos( $argv[2], '--' ) ) {
// Legacy: php worker.php <batch_size> <worker_id> — batch_size argument is now ignored.
$worker_id = $argv[2];
$arg_offset = 3;
fwrite( STDERR, "[DEPRECATED] Batch size is now read from plugin settings. Remove the numeric argument from the command.\n" );
} else {
$worker_id = $argv[1];
}
// Parse optional --url parameter for MultiSite support.
$site_url = null;
for ( $i = 3; $i < $argc; $i++ ) {
for ( $i = $arg_offset; $i < $argc; $i++ ) {
if ( strpos( $argv[ $i ], '--url=' ) === 0 ) {
$site_url = substr( $argv[ $i ], 6 );
break;
@ -126,6 +141,10 @@ if ( $plugin_manually_loaded ) {
\Robotstxt_SMTP_Newsletter\Plugin::instance_init();
}
// Read batch size from plugin settings (database).
$plugin_options = get_option( 'robotstxt_smtp_newsletter_options', array() );
$batch_size = max( 1, (int) ( $plugin_options['batch_size'] ?? 10 ) );
// Get queue and statistics instances.
$queue = \Robotstxt_SMTP_Newsletter\Queue::get_instance();
$stats = \Robotstxt_SMTP_Newsletter\Statistics::get_instance();