Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the Translated pattern cron task to run many smaller tasks #620

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use function WordPressdotorg\Pattern_Translations\create_or_update_translated_pattern;
use function WordPressdotorg\Locales\get_locales;

const CHUNK_SIZE = 200;

/**
* Register the cron jobs needed.
*/
Expand All @@ -22,7 +24,7 @@ function register_cron_tasks() {
/**
* Periodically import all Patterns into GlotPress for translation.
*
* This is the equivilient of the following WP-CLI command:
* This is the equivalent of the following WP-CLI command:
* `wp --url=https://wordpress.org/patterns/ patterns glotpress-import --all-posts --save`
*/
function pattern_import_to_glotpress() {
Expand All @@ -36,15 +38,41 @@ function pattern_import_to_glotpress() {
* Sync/Create translated patterns of GlotPress translated patterns.
*
* This creates the "forked" patterns of a parent pattern when translations are available.
* This queues sub-tasks which each process a CHUNK_SIZE group of patterns, to avoid memory exhaustion.
* These subtasks are spread between now and the next time this cron is expected to run.
*
* @param int[] $pattern_ids Optional. An array of Pattern IDs to process.
* If not provided, queues sub-tasks if in cron context, else processes all patterns.
*/
function pattern_import_translations_to_directory() {
$patterns = Pattern::get_patterns();
$locales = get_locales();
function pattern_import_translations_to_directory( $pattern_ids = array() ) {
if ( ! $pattern_ids ) {
$pattern_ids = Pattern::get_patterns( [ 'fields' => 'ids' ] );

if ( wp_doing_cron() ) {
// Chunk the patterns to avoid memory exhaustion.
$timestamp = time();
$chunks = array_chunk( $pattern_ids, CHUNK_SIZE );
// Spread out the sub-tasks over the entire twicedaily period.
$delay = floor( ( 12 * HOUR_IN_SECONDS ) / count( $chunks ) );
foreach ( $chunks as $chunk ) {
wp_schedule_single_event( $timestamp, current_action(), array( $chunk ) );

$timestamp += $delay;
}

printf( "Queued %d cron jobs of %d Patterns each.\n", count( $pattern_ids ) / CHUNK_SIZE, CHUNK_SIZE ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
}

$locales = get_locales();

printf( "Processing %d Patterns in %d locales.\n", count( $pattern_ids ), count( $locales ) );

printf( "Processing %d Patterns in %d locales.\n", count( $patterns ), count( $locales ) );
foreach ( $pattern_ids as $i => $pattern_id ) {
$pattern = Pattern::from_post( get_post( $pattern_id ) );

foreach ( $patterns as $pattern ) {
echo "Processing {$pattern->name} / '{$pattern->title}'..\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "{$i}. Processing {$pattern->name} / '{$pattern->title}'..\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
foreach ( $locales as $gp_locale ) {
$locale = $gp_locale->wp_locale;
if ( ! $locale || 'en_US' === $locale ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ public static function get_patterns( array $args = [] ) : array {

$options = wp_parse_args( $args, $defaults );

$query = new \WP_Query();
$posts = $query->query( $options );
$query = new \WP_Query();
$patterns = $query->query( $options );

wp_reset_postdata();

$patterns = array_map( [ self::class, 'from_post' ], $posts );
if ( 'ids' !== $query->get( 'fields' ) ) {
$patterns = array_map( [ self::class, 'from_post' ], $patterns );
}

return $patterns;
}
Expand Down
Loading