store->get( $post->ID ); $state = Freshness::classify( $entry, AGENTWELCOME_VERSION ); if ( null !== $entry && Freshness::FRESH === $state ) { return new Served( $entry, Served::HIT ); } if ( null !== $entry && Freshness::STALE === $state ) { $this->schedule( $post->ID, RenderResult::FALLBACK === $entry->source ? self::FALLBACK_RETRY_DELAY : self::REFRESH_DELAY_SECONDS ); return new Served( $entry, Served::STALE ); } $deadline = microtime( true ) + self::WAIT_SECONDS; do { $slot = $this->take_render_slot(); if ( null === $slot ) { $this->schedule( $post->ID, self::REFRESH_DELAY_SECONDS ); return null !== $entry ? new Served( $entry, Served::STALE ) : null; } try { $served = $this->generate_unless_fresh( $post ); } finally { $this->lock->release_slot( $slot ); } if ( null !== $served ) { return $served; } if ( null !== $entry ) { return new Served( $entry, Served::STALE ); } usleep( self::POLL_MICROSECONDS ); $current = $this->store->get( $post->ID ); if ( null !== $current && Freshness::OUTDATED !== Freshness::classify( $current, AGENTWELCOME_VERSION ) ) { return new Served( $current, Served::HIT ); } } while ( microtime( true ) < $deadline ); return null; } /** * Takes a slot for rendering a page for a visitor. * * @return int|null Slot number, or null when every slot is busy. */ private function take_render_slot(): ?int { return $this->lock->take_slot( max( 1, (int) apply_filters( 'agentwelcome_max_concurrent_renders', self::MAX_RENDERS ) ) ); } /** * Generates and stores the copy of a page, even when a current one exists. * * @param WP_Post $post The page. * @return Entry|null The new copy, or null when another request holds the lock. */ public function regenerate( WP_Post $post ): ?Entry { if ( ! $this->lock->acquire( $post->ID ) ) { return null; } try { $entry = $this->build( $post ); $this->store->save( $entry ); return $entry; } finally { $this->lock->release( $post->ID ); } } /** * Generates the copy of a page unless another request has just stored a current one. * * @param WP_Post $post The page. * @return Served|null MISS when this request generated the copy, HIT when it found a current one, or null when another request holds the lock. */ private function generate_unless_fresh( WP_Post $post ): ?Served { if ( ! $this->lock->acquire( $post->ID ) ) { return null; } try { $current = $this->store->get( $post->ID ); if ( null !== $current && Freshness::FRESH === Freshness::classify( $current, AGENTWELCOME_VERSION ) ) { return new Served( $current, Served::HIT ); } $entry = $this->build( $post ); $this->store->save( $entry ); return new Served( $entry, Served::MISS ); } finally { $this->lock->release( $post->ID ); } } /** * Cron callback that refreshes one page, or removes its copy when it is no longer served. * * @param int $post_id Page ID. * @return void */ public function run_job( int $post_id ): void { if ( ! $this->environment->is_active() ) { return; } $post = get_post( $post_id ); if ( ! $post instanceof WP_Post || ! $this->eligibility->is_eligible( $post ) ) { $this->store->delete( $post_id ); return; } $this->regenerate( $post ); } /** * Queues a refresh of a page unless one is already queued. * * @param int $post_id Page ID. * @param int $delay Seconds to wait. * @return void */ public function schedule( int $post_id, int $delay = self::REFRESH_DELAY_SECONDS ): void { $args = array( $post_id ); if ( ! $this->environment->is_active() || false !== wp_next_scheduled( self::JOB, $args ) ) { return; } wp_schedule_single_event( time() + $delay, self::JOB, $args ); } /** * Marks pages out of date and queues a refresh for each, spaced apart. * * @param int[] $post_ids Page IDs. * @return void */ public function queue( array $post_ids ): void { foreach ( array_values( $post_ids ) as $index => $post_id ) { $this->store->mark_stale( $post_id ); $this->schedule( $post_id, self::REFRESH_DELAY_SECONDS + $index * self::QUEUE_SPACING_SECONDS ); } } /** * Generates the copy of a page. A copy built without the rendered page is kept out of date so it is retried later. * * @param WP_Post $post The page. * @return Entry The new copy. */ private function build( WP_Post $post ): Entry { $page = $this->generator->generate( $post ); $fallback = RenderResult::FALLBACK === $page->source; return new Entry( $post->ID, $page->markdown, sha1( $page->markdown ), $page->source, AGENTWELCOME_VERSION, gmdate( 'Y-m-d H:i:s' ), $fallback ? Entry::STALE : Entry::FRESH ); } }