WP_Async_Task::launch_on_shutdown()

Description #

Launch the request on the WordPress shutdown hook

On VIP we got into data races due to the postback sometimes completing faster than the data could propogate to the database server cluster. This made WordPress get empty data sets from the database without failing. On their advice, we’re moving the actual firing of the async postback to the shutdown hook. Supposedly that will ensure that the data at least has time to get into the object cache.

Source #

File: lib/class-wp-async-task.php

		public function launch_on_shutdown() {
			if ( ! empty( $this->_body_data ) ) {
				$cookies = array();
				foreach ( $_COOKIE as $name => $value ) {
					$cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value );
				}

				$request_args = array(
					'timeout'   => 0.01,
					'blocking'  => false,
					'sslverify' => apply_filters( 'https_local_ssl_verify', true ),
					'body'      => $this->_body_data,
					'headers'   => array(
						'cookie' => implode( '; ', $cookies ),
					),
					'compress'  => true,
				);

				$url = admin_url( 'admin-post.php' );

				wp_remote_post( $url, $request_args );
			}
		}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment