Skip to content

Commit 17df083

Browse files
committed
Harden WordPress delivery recovery
1 parent 166e89d commit 17df083

5 files changed

Lines changed: 319 additions & 16 deletions

File tree

‎includes/class-spacefast-client.php‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private function request( string $method, string $path, ?array $body = null, arr
3434
'redirection' => 0,
3535
'reject_unsafe_urls' => true,
3636
'sslverify' => true,
37-
'limit_response_size' => 65536,
37+
'limit_response_size' => 1024 * 1024,
3838
'headers' => array_merge(
3939
array(
4040
'Authorization' => 'Bearer ' . $access_token,
@@ -61,7 +61,21 @@ private function request( string $method, string $path, ?array $body = null, arr
6161
}
6262

6363
$status = (int) wp_remote_retrieve_response_code( $response );
64-
$decoded = json_decode( (string) wp_remote_retrieve_body( $response ), true );
64+
$raw_body = (string) wp_remote_retrieve_body( $response );
65+
$decoded = json_decode( $raw_body, true );
66+
if (
67+
$status >= 200
68+
&& $status < 300
69+
&& ( '' === $raw_body || JSON_ERROR_NONE !== json_last_error() || ! is_array( $decoded ) )
70+
) {
71+
return array(
72+
'ok' => false,
73+
'retryable' => true,
74+
'code' => 'invalid_response_body',
75+
'message' => 'Spacefast returned an unreadable response.',
76+
'data' => array(),
77+
);
78+
}
6579
$decoded = is_array( $decoded ) ? $decoded : array();
6680
if ( $status >= 200 && $status < 300 ) {
6781
$data = isset( $decoded['data'] ) && is_array( $decoded['data'] )

‎includes/class-spacefast-plugin.php‎

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
final class Spacefast_Plugin {
66
const AUTOMATIC_DEBOUNCE_SECONDS = MINUTE_IN_SECONDS;
7+
const STATIC_DELIVERY_STALE_SECONDS = 2 * HOUR_IN_SECONDS;
78
const LOCK_OPTION = 'spacefast_wordpress_worker_lock';
89
const NOTICE_TRANSIENT = 'spacefast_wordpress_admin_notice';
910

@@ -42,6 +43,7 @@ public static function boot(): void {
4243
add_action( 'update_option_blogname', array( __CLASS__, 'wordpress_setting_changed' ), 10, 3 );
4344
add_action( 'update_option_blogdescription', array( __CLASS__, 'wordpress_setting_changed' ), 10, 3 );
4445
add_action( 'update_option_blog_public', array( __CLASS__, 'wordpress_setting_changed' ), 10, 3 );
46+
add_action( 'update_option_home', array( __CLASS__, 'wordpress_setting_changed' ), 10, 3 );
4547
add_filter( 'site_status_tests', array( __CLASS__, 'site_health_tests' ) );
4648
}
4749

@@ -55,7 +57,12 @@ public static function activate( bool $network_wide = false ): void {
5557
add_option( Spacefast_Sync_State::OPTION, Spacefast_Sync_State::defaults(), '', false );
5658
}
5759
$state = Spacefast_Sync_State::get();
58-
if ( (int) $state['desired'] > (int) $state['delivered'] ) {
60+
$active_delivery = 'building' === (string) $state['last_status']
61+
&& '' !== (string) $state['last_build_id'];
62+
$active_export = self::static_delivery_active( $state );
63+
if ( $active_delivery || $active_export ) {
64+
self::schedule( time() + 1 );
65+
} elseif ( (int) $state['desired'] > (int) $state['delivered'] ) {
5966
self::schedule_after_change( (int) $state['last_change_at'] );
6067
}
6168
}
@@ -259,6 +266,13 @@ public static function create_space(): void {
259266
* @return array{ok:bool,retryable:bool,code:string,message:string,data:array<string,mixed>}
260267
*/
261268
private static function connect_space( array $space, array $team ): array {
269+
$current_settings = Spacefast_Settings::get();
270+
if ( ! hash_equals( (string) $current_settings['space_id'], (string) $space['id'] ) ) {
271+
wp_clear_scheduled_hook( Spacefast_Sync_State::HOOK );
272+
Spacefast_Sync_State::save( Spacefast_Sync_State::defaults() );
273+
Spacefast_Static_Publisher::reset();
274+
self::$change_recorded = false;
275+
}
262276
Spacefast_Settings::merge(
263277
array(
264278
'team_id' => (string) ( $space['teamId'] ?? $team['id'] ?? '' ),
@@ -273,7 +287,7 @@ private static function connect_space( array $space, array $team ): array {
273287
);
274288
self::mark_settings_pending();
275289
$settings_sync = self::sync_space_settings();
276-
if ( ! $settings_sync['ok'] ) {
290+
if ( ! $settings_sync['ok'] && 'reauthorization_required' !== $settings_sync['code'] ) {
277291
self::static_publish_failed( $settings_sync['message'] );
278292
return $settings_sync;
279293
}
@@ -370,16 +384,24 @@ public static function manual_static_publish(): void {
370384
self::notice( 'error', __( 'Install and activate Simply Static before publishing.', 'spacefast-wordpress' ) );
371385
self::redirect();
372386
}
387+
$before = Spacefast_Sync_State::get();
388+
$was_active = self::static_delivery_active( $before );
389+
$previous_attempt = (int) $before['last_attempt_at'];
373390
self::record_change( 'manual', true );
374391
wp_clear_scheduled_hook( Spacefast_Sync_State::HOOK );
375392
self::deliver();
376393
$state = Spacefast_Sync_State::get();
377-
$active = in_array( (string) $state['last_status'], array( 'exporting', 'uploading', 'finalizing' ), true );
394+
$active = self::static_delivery_active( $state );
395+
$started = $active && ( ! $was_active || (int) $state['last_attempt_at'] > $previous_attempt );
396+
$message = $started
397+
? __( 'Publishing started. You can leave this page.', 'spacefast-wordpress' )
398+
: ( $active
399+
? __( 'A publish is already running. Your latest changes will publish next.', 'spacefast-wordpress' )
400+
: ( (string) $state['last_message'] ?: __( 'The publish could not start. The current live version is safe.', 'spacefast-wordpress' ) )
401+
);
378402
self::notice(
379403
$active ? 'success' : 'error',
380-
$active
381-
? __( 'Publishing started. You can leave this page.', 'spacefast-wordpress' )
382-
: ( (string) $state['last_message'] ?: __( 'The publish could not start. The current live version is safe.', 'spacefast-wordpress' ) )
404+
$message
383405
);
384406
self::redirect();
385407
}
@@ -641,7 +663,7 @@ public static function deliver(): void {
641663

642664
try {
643665
$snapshot = Spacefast_Sync_State::get();
644-
if ( ! empty( $snapshot['settings_pending'] ) ) {
666+
if ( ! empty( $snapshot['settings_pending'] ) && Spacefast_OAuth::has_scope( 'spaces:write' ) ) {
645667
$settings_sync = self::sync_space_settings();
646668
if ( ! $settings_sync['ok'] ) {
647669
Spacefast_Sync_State::mutate(
@@ -726,6 +748,10 @@ private static function poll_headless_build( array $snapshot ): void {
726748
self::schedule( time() + 15 );
727749
return;
728750
}
751+
if ( ! in_array( $status, array( 'succeeded', 'failed', 'canceled', 'skipped' ), true ) ) {
752+
self::schedule( time() + 60 );
753+
return;
754+
}
729755
$diagnostics = isset( $build['diagnostics'] ) && is_array( $build['diagnostics'] ) ? $build['diagnostics'] : array();
730756
$message = '';
731757
foreach ( $diagnostics as $diagnostic ) {
@@ -739,22 +765,42 @@ private static function poll_headless_build( array $snapshot ): void {
739765
);
740766
if ( 'succeeded' === $status ) {
741767
Spacefast_Settings::merge( array( 'verified_at' => time() ) );
742-
if ( (int) $current['desired'] > (int) $current['delivered'] ) {
743-
self::schedule_after_change( (int) $current['last_change_at'] );
744-
}
768+
}
769+
if ( (int) $current['desired'] > (int) $current['delivered'] ) {
770+
self::schedule_after_change( (int) $current['last_change_at'] );
745771
}
746772
}
747773

748774
/** @param array<string,mixed> $snapshot Current delivery state. */
749775
private static function deliver_static( array $snapshot ): void {
750-
if ( in_array( (string) $snapshot['last_status'], array( 'exporting', 'uploading', 'finalizing' ), true ) ) return;
776+
if ( self::static_delivery_active( $snapshot ) ) {
777+
if ( ! self::static_delivery_stale( $snapshot ) ) {
778+
self::schedule( max( time() + 60, (int) $snapshot['last_attempt_at'] + self::STATIC_DELIVERY_STALE_SECONDS ) );
779+
return;
780+
}
781+
Spacefast_Static_Publisher::reset();
782+
$snapshot = Spacefast_Sync_State::mutate(
783+
static function ( array $state ): array {
784+
$state['last_status'] = 'pending';
785+
$state['last_message'] = '';
786+
$state['active_generation'] = 0;
787+
return $state;
788+
}
789+
);
790+
}
751791
if ( (int) $snapshot['desired'] <= (int) $snapshot['delivered'] ) return;
752792
if ( ! self::simply_static_available() ) {
753793
self::static_publish_failed( __( 'Simply Static is required. Install it, then retry; the current live version is safe.', 'spacefast-wordpress' ) );
754794
return;
755795
}
756796
self::configure_simply_static();
757797
try {
798+
Spacefast_Sync_State::mutate(
799+
static function ( array $state ): array {
800+
$state['last_attempt_at'] = time();
801+
return $state;
802+
}
803+
);
758804
$started = \Simply_Static\Plugin::instance()->run_static_export();
759805
if ( ! $started ) {
760806
self::schedule( time() + 60 );
@@ -764,6 +810,7 @@ private static function deliver_static( array $snapshot ): void {
764810
static function ( array $state ): array {
765811
$state['last_status'] = 'exporting';
766812
$state['last_message'] = '';
813+
$state['last_attempt_at'] = time();
767814
return $state;
768815
}
769816
);
@@ -772,6 +819,17 @@ static function ( array $state ): array {
772819
}
773820
}
774821

822+
/** @param array<string,mixed> $state Current delivery state. */
823+
private static function static_delivery_active( array $state ): bool {
824+
return in_array( (string) $state['last_status'], array( 'exporting', 'uploading', 'finalizing' ), true );
825+
}
826+
827+
/** @param array<string,mixed> $state Current delivery state. */
828+
private static function static_delivery_stale( array $state ): bool {
829+
$last_attempt = (int) $state['last_attempt_at'];
830+
return 0 === $last_attempt || $last_attempt + self::STATIC_DELIVERY_STALE_SECONDS <= time();
831+
}
832+
775833
public static function register_simply_static(): void {
776834
if ( ! self::simply_static_available() ) {
777835
return;
@@ -845,11 +903,23 @@ static function ( array $state ): array {
845903
);
846904
$next['last_status'] = 'exporting';
847905
$next['active_generation'] = (int) $next['desired'];
906+
$next['last_attempt_at'] = time();
848907
return $next;
849908
}
850909
);
851910
}
852911

912+
public static function static_publish_progress( string $status ): void {
913+
if ( ! in_array( $status, array( 'uploading', 'finalizing' ), true ) ) return;
914+
Spacefast_Sync_State::mutate(
915+
static function ( array $state ) use ( $status ): array {
916+
$state['last_status'] = $status;
917+
$state['last_attempt_at'] = time();
918+
return $state;
919+
}
920+
);
921+
}
922+
853923
public static function static_publish_completed( string $version_id, string $status ): void {
854924
$current = Spacefast_Sync_State::mutate(
855925
static fn( array $state ): array => Spacefast_Sync_State::acknowledge_static(
@@ -869,6 +939,7 @@ public static function static_publish_failed( string $message ): void {
869939
static function ( array $state ) use ( $message ): array {
870940
$state['last_status'] = 'blocked';
871941
$state['last_message'] = $message;
942+
$state['active_generation'] = 0;
872943
return $state;
873944
}
874945
);

‎includes/class-spacefast-simply-static-task.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function perform() {
2929
$partial_export ? 'additive' : 'snapshot'
3030
);
3131
if ( ! $result['done'] ) {
32+
Spacefast_Plugin::static_publish_progress( $result['status'] );
3233
$this->save_status_message(
3334
'finalizing' === $result['status']
3435
? __( 'Spacefast is activating the uploaded version', 'spacefast-wordpress' )

‎includes/class-spacefast-sync-state.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ final class Spacefast_Sync_State {
77
const HOOK = 'spacefast_wordpress_deliver';
88

99
/**
10-
* @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}
10+
* @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,last_attempt_at:int,last_success_at:int,last_settings_sync_at:int,active_generation:int,settings_pending:bool}
1111
*/
1212
public static function defaults(): array {
1313
return array(
@@ -31,7 +31,7 @@ public static function defaults(): array {
3131
}
3232

3333
/**
34-
* @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}
34+
* @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,last_attempt_at:int,last_success_at:int,last_settings_sync_at:int,active_generation:int,settings_pending:bool}
3535
*/
3636
public static function get(): array {
3737
$value = get_option( self::OPTION, array() );
@@ -91,9 +91,11 @@ public static function acknowledge_build_terminal( array $state, string $status,
9191
$state['last_status'] = (int) $state['desired'] > (int) $state['delivered'] ? 'pending' : 'live';
9292
$state['last_message'] = '';
9393
$state['last_success_at'] = time();
94+
$state['last_build_id'] = '';
9495
return $state;
9596
}
9697
$state['last_status'] = 'blocked';
98+
$state['last_build_id'] = '';
9799
$state['last_message'] = '' !== $message
98100
? $message
99101
: 'The build did not finish successfully. The previous live version is safe.';
@@ -114,7 +116,10 @@ public static function acknowledge_settings_sync( array $state ): array {
114116
*/
115117
public static function acknowledge_static( array $state, string $version_id, string $status ): array {
116118
$state = array_merge( self::defaults(), $state );
117-
$state['delivered'] = max( (int) $state['delivered'], (int) $state['active_generation'] );
119+
$generation = 0 < (int) $state['active_generation']
120+
? (int) $state['active_generation']
121+
: (int) $state['desired'];
122+
$state['delivered'] = max( (int) $state['delivered'], $generation );
118123
$state['attempts'] = 0;
119124
$state['next_at'] = 0;
120125
$state['last_status'] = (int) $state['desired'] > (int) $state['delivered'] ? 'pending' : $status;

0 commit comments

Comments
 (0)