Skip to content
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ WordPress REST API during its build. Publishing, updating, unpublishing, or
deleting public REST-visible post types, taxonomy changes, navigation changes,
media changes, and public user changes schedule a production build. Autosaves,
revisions, and draft-only transitions are ignored. Multiple hooks in one
request collapse into one generation.
request collapse into one generation. Each new public-content change restarts a
60-second quiet window, so editing a batch of content produces one build.
Merely opening or navigating wp-admin does not schedule anything.

Delivery uses WP-Cron with a stable idempotency key. Network failures, timeouts,
rate limits, and server errors retry with bounded exponential backoff. A change
arriving while a delivery runs remains pending and schedules one successor.
**Build now** uses the same path immediately. The generic **Build now** action
arriving while a delivery runs remains pending and schedules one successor after
the quiet window. **Build now** skips the window, runs immediately, and clears
the superseded scheduled event. The generic **Build now** action
in the Spacefast dashboard triggers the same connected production repository.

The plugin sends no post content, title, author, or other WordPress data to
Expand Down
22 changes: 18 additions & 4 deletions includes/class-spacefast-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
defined( 'ABSPATH' ) || exit;

final class Spacefast_Plugin {
const AUTOMATIC_DEBOUNCE_SECONDS = MINUTE_IN_SECONDS;
const LOCK_OPTION = 'spacefast_wordpress_worker_lock';
const NOTICE_TRANSIENT = 'spacefast_wordpress_admin_notice';

Expand Down Expand Up @@ -48,7 +49,7 @@ public static function activate( bool $network_wide = false ): void {
}
$state = Spacefast_Sync_State::get();
if ( (int) $state['desired'] > (int) $state['delivered'] ) {
self::schedule( time() + 15 );
self::schedule_after_change( (int) $state['last_change_at'] );
}
}

Expand Down Expand Up @@ -225,6 +226,7 @@ public static function manual_build(): void {
self::redirect();
}
self::record_change( 'manual' );
wp_clear_scheduled_hook( Spacefast_Sync_State::HOOK );
self::deliver();
self::redirect();
}
Expand Down Expand Up @@ -311,14 +313,26 @@ private static function record_change( string $reason ): void {
}
self::$change_recorded = true;
$event_id = (string) wp_generate_uuid4();
Spacefast_Sync_State::mutate(
$state = Spacefast_Sync_State::mutate(
static fn( array $state ): array => Spacefast_Sync_State::record_change(
$state,
$reason,
$event_id
)
);
self::schedule( time() + 15 );
self::schedule_after_change( (int) $state['last_change_at'] );
}

private static function schedule_after_change( int $changed_at ): void {
$timestamp = max( time() + 1, $changed_at + self::AUTOMATIC_DEBOUNCE_SECONDS );
$scheduled = wp_next_scheduled( Spacefast_Sync_State::HOOK );
Comment on lines +327 to +328

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recheck the quiet window before delivery

When two content changes run in concurrent WordPress requests, the first request can pause after mutating state, the second can schedule from its later last_change_at, and then the first can resume here, unschedule that newer event, and replace it using its stale timestamp. Because deliver() does not verify last_change_at before triggering the build, the build can run before the latest change has been quiet for 60 seconds. Re-read the persisted timestamp while scheduling or enforce the quiet-window condition at delivery time.

Useful? React with 👍 / 👎.

if ( $scheduled === $timestamp ) {
return;
}
if ( $scheduled ) {
wp_unschedule_event( $scheduled, Spacefast_Sync_State::HOOK );
}
wp_schedule_single_event( $timestamp, Spacefast_Sync_State::HOOK );
}

private static function schedule( int $timestamp ): void {
Expand Down Expand Up @@ -372,7 +386,7 @@ static function ( array $state ): array {
)
);
if ( (int) $current['desired'] > (int) $current['delivered'] ) {
self::schedule( time() + 15 );
self::schedule_after_change( (int) $current['last_change_at'] );
}
return;
}
Expand Down
6 changes: 4 additions & 2 deletions includes/class-spacefast-sync-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class Spacefast_Sync_State {
const HOOK = 'spacefast_wordpress_deliver';

/**
* @return array{desired:int,delivered:int,event_id:string,reasons:array<int,string>,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string}
* @return array{desired:int,delivered:int,event_id:string,reasons:array<int,string>,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string,last_change_at:int}
*/
public static function defaults(): array {
return array(
Expand All @@ -21,13 +21,14 @@ public static function defaults(): array {
'last_message' => '',
'last_build_id' => '',
'last_version_id' => '',
'last_change_at' => 0,
'last_attempt_at' => 0,
'last_success_at' => 0,
);
}

/**
* @return array{desired:int,delivered:int,event_id:string,reasons:array<int,string>,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string}
* @return array{desired:int,delivered:int,event_id:string,reasons:array<int,string>,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string,last_change_at:int}
*/
public static function get(): array {
$value = get_option( self::OPTION, array() );
Expand All @@ -54,6 +55,7 @@ public static function record_change( array $state, string $reason, string $even
);
$state['attempts'] = 0;
$state['next_at'] = 0;
$state['last_change_at'] = time();
$state['last_status'] = 'pending';
$state['last_message'] = '';
return $state;
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: spacefast
Tags: static site, headless cms, simply static, deployment
Requires at least: 6.5
Requires PHP: 8.1
Stable tag: 0.3.0
Stable tag: 0.3.1
License: GPLv2 or later

Publish a Simply Static export to Spacefast, or rebuild a headless site when WordPress content changes.
Expand Down Expand Up @@ -31,6 +31,10 @@ OAuth access is limited to the Team you authorize and the mode you choose. WordP

== Changelog ==

= 0.3.1 =
* Debounce automatic headless builds until public content has been quiet for 60 seconds.
* Keep manual builds immediate and avoid duplicate scheduled delivery afterward.

= 0.3.0 =
* Replace copied API keys with Team-scoped OAuth 2 authorization and Space selection in WordPress.
* Add refresh-token rotation and remote revocation on disconnect.
Expand Down
4 changes: 2 additions & 2 deletions spacefast-wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Spacefast
* Plugin URI: https://spacefast.com/
* Description: Publishes static WordPress exports or rebuilds a headless Spacefast site.
* Version: 0.3.0
* Version: 0.3.1
* Update URI: https://github.com/spacefast/wordpress
* Requires at least: 6.5
* Requires PHP: 8.1
Expand All @@ -14,7 +14,7 @@

defined( 'ABSPATH' ) || exit;

define( 'SPACEFAST_WORDPRESS_VERSION', '0.3.0' );
define( 'SPACEFAST_WORDPRESS_VERSION', '0.3.1' );
define( 'SPACEFAST_WORDPRESS_FILE', __FILE__ );

require_once __DIR__ . '/includes/class-spacefast-settings.php';
Expand Down
21 changes: 20 additions & 1 deletion tests/acceptance/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ function spacefast_accept( bool $condition, string $message ): void {
'access_token' => 'acceptance-access',
'refresh_token' => 'acceptance-refresh',
'expires_at' => time() + 900,
'team_id' => 'team_acceptance',
'space_id' => 'spc_acceptance',
'verified_at' => time(),
)
);
global $wpdb;
Expand All @@ -51,8 +54,24 @@ function spacefast_accept( bool $condition, string $message ): void {
);
spacefast_accept( ! in_array( $autoload, array( 'yes', 'on', 'auto-on' ), true ), 'OAuth tokens are autoloaded.' );

$changed_at = time();
$post_id = wp_insert_post(
array(
'post_title' => 'Debounce acceptance',
'post_status' => 'publish',
'post_type' => 'post',
)
);
spacefast_accept( ! is_wp_error( $post_id ), 'Could not publish acceptance content.' );
$scheduled_at = wp_next_scheduled( Spacefast_Sync_State::HOOK );
spacefast_accept( false !== $scheduled_at, 'Publishing public content did not schedule a build.' );
spacefast_accept(
$scheduled_at >= $changed_at + 59 && $scheduled_at <= time() + 61,
'Public content did not receive the 60-second quiet window.'
);

$plugin = get_plugin_data( WP_PLUGIN_DIR . '/spacefast-wordpress/spacefast-wordpress.php', false, false );
spacefast_accept( '0.3.0' === $plugin['Version'], 'Unexpected plugin version.' );
spacefast_accept( '0.3.1' === $plugin['Version'], 'Unexpected plugin version.' );
spacefast_accept( 'https://github.com/spacefast/wordpress' === $plugin['UpdateURI'], 'Update URI is missing.' );

Spacefast_Settings::disconnect();
Expand Down
19 changes: 13 additions & 6 deletions tests/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ static function ( string $url, array $args ) use ( &$revoke_requests ): array {
check( 2 === $state['desired'], 'increments desired generation' );
check( 'event-two' === $state['event_id'], 'keeps latest stable event id' );
check( array( 'postpublished', 'taxonomy' ) === $state['reasons'], 'coalesces reasons' );
check( 0 < $state['last_change_at'], 'records when the latest public content change happened' );
$state['desired'] = 3;
$ack = Spacefast_Sync_State::acknowledge( $state, 2, 'bld_test' );
check( 2 === $ack['delivered'], 'acknowledges only observed generation' );
Expand Down Expand Up @@ -372,13 +373,19 @@ static function ( string $url, array $args ) use ( &$revoke_requests ): array {
Spacefast_Sync_State::release_lock( 'spacefast_test_lock', 'owner-a' );
check( false === get_option( 'spacefast_test_lock', false ), 'owner releases lock' );

$schedule = new ReflectionMethod( Spacefast_Plugin::class, 'schedule' );
$GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ] = time() + DAY_IN_SECONDS;
$earlier = time() + 15;
$schedule->invoke( null, $earlier );
$debounce = new ReflectionMethod( Spacefast_Plugin::class, 'schedule_after_change' );
$changed_at = time();
$GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ] = $changed_at + 10;
$debounce->invoke( null, $changed_at );
check(
$earlier === $GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ],
'new content pulls a far-future retry forward'
$changed_at + MINUTE_IN_SECONDS === $GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ],
'public content waits for a 60-second quiet window'
);
$later_change = $changed_at + 20;
$debounce->invoke( null, $later_change );
check(
$later_change + MINUTE_IN_SECONDS === $GLOBALS['spacefast_scheduled'][ Spacefast_Sync_State::HOOK ],
'a later content change restarts the quiet window'
);

Spacefast_Sync_State::save(
Expand Down
Loading