Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions includes/class-spacefast-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@ public function resume_static_upload( string $version_id ): array {
);
}

/**
* Ask Spacefast to finalize an uploaded version. Auto-finalize normally does
* this when the last upload lands; an explicit request joins the same
* finalize operation, so a lost completion signal cannot strand the version.
*
* @return array{ok:bool,retryable:bool,code:string,message:string,data:array<string,mixed>}
*/
public function finalize_static_version( string $version_id ): array {
$settings = Spacefast_Settings::get();
return $this->request(
'POST',
'/v1/spaces/' . rawurlencode( $settings['space_id'] ) . '/versions/'
. rawurlencode( $version_id ) . '/finalize?async=1',
Comment thread
indent[bot] marked this conversation as resolved.
Resolved
array( 'channel' => 'live' )
);
}

/**
* @return array{ok:bool,retryable:bool,code:string,message:string,data:array<string,mixed>}
*/
Expand Down
45 changes: 43 additions & 2 deletions includes/class-spacefast-static-publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

final class Spacefast_Static_Publisher {
const OPTION = 'spacefast_wordpress_publish_state';
// Consecutive transient failures tolerated before a publish gives up. Each
// retry is one background step, so a brief outage no longer restarts the
// whole export as a new version.
const MAX_TRANSIENT_RETRIES = 5;

/**
* Advance one bounded step of a Simply Static publish.
Expand Down Expand Up @@ -96,8 +100,9 @@ public static function step(
if ( in_array( $result['code'], array( 'upload_http_401', 'upload_http_403' ), true ) ) {
return self::resume( $state, $client );
}
throw new RuntimeException( $result['message'] );
return self::retry_or_throw( $state, $result, 'uploading' );
}
$state['retries'] = 0;
$state['next_target'] = $next_target + 1;
$state['uploaded'] = (int) ( $state['uploaded'] ?? 0 ) + 1;
update_option( self::OPTION, $state, false );
Expand Down Expand Up @@ -212,8 +217,9 @@ private static function resume( array $state, Spacefast_Client $client ): array
}
$result = $client->resume_static_upload( (string) $state['version_id'] );
if ( ! $result['ok'] ) {
throw new RuntimeException( $result['message'] );
return self::retry_or_throw( $state, $result, 'uploading' );
Comment thread
batuhan marked this conversation as resolved.
Resolved
}
$state['retries'] = 0;
$upload = $result['data']['upload'] ?? null;
if ( null !== $upload && ! is_array( $upload ) ) {
throw new RuntimeException( 'Spacefast returned invalid upload instructions.' );
Expand Down Expand Up @@ -261,6 +267,17 @@ private static function poll_version( array $state, Spacefast_Client $client ):
if ( $polls > 300 ) {
throw new RuntimeException( 'Spacefast did not finish publishing the version.' );
}
if ( empty( $state['finalize_requested'] ) ) {
$finalize = $client->finalize_static_version( (string) $state['version_id'] );
if ( ! $finalize['ok'] && $finalize['retryable'] ) {
return self::retry_or_throw( $state, $finalize, 'finalizing' );
}
// A refusal here usually means auto-finalize already owns the version;
// the status poll below reports whatever it settles to.
$state['finalize_requested'] = true;
$state['retries'] = 0;
update_option( self::OPTION, $state, false );
}
$result = $client->get_static_version( (string) $state['version_id'] );
if ( ! $result['ok'] ) {
if ( $result['retryable'] ) {
Expand Down Expand Up @@ -300,4 +317,28 @@ private static function poll_version( array $state, Spacefast_Client $client ):
'status' => 'finalizing',
);
}

/**
* Keep the publish state and try the same step again on a transient failure,
* up to MAX_TRANSIENT_RETRIES in a row. Anything else ends the publish.
*
* @param array<string,mixed> $state State.
* @param array{ok:bool,retryable:bool,code:string,message:string,data:array<string,mixed>} $result Failed result.
* @return array{done:bool,version_id:string,uploaded:int,total:int,status:string}
*/
private static function retry_or_throw( array $state, array $result, string $status ): array {
$retries = (int) ( $state['retries'] ?? 0 ) + 1;
if ( ! $result['retryable'] || $retries > self::MAX_TRANSIENT_RETRIES ) {
Comment thread
indent[bot] marked this conversation as resolved.
Resolved
throw new RuntimeException( $result['message'] );
}
$state['retries'] = $retries;
update_option( self::OPTION, $state, false );
return array(
'done' => false,
'version_id' => (string) ( $state['version_id'] ?? '' ),
'uploaded' => (int) ( $state['uploaded'] ?? 0 ),
'total' => (int) ( $state['total'] ?? 0 ),
'status' => $status,
);
}
}
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.5.7
Stable tag: 0.5.8
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.5.8 =
* Retry transient upload, upload-refresh, and finalize failures in place instead of restarting the whole export as a new version.
Comment thread
indent[bot] marked this conversation as resolved.
Resolved
* Ask Spacefast to finalize once every file has uploaded, so activation no longer depends on the upload-completion signal alone.

= 0.5.7 =
* Include public custom post type archive routes in Simply Static exports.

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.5.7
* Version: 0.5.8
* 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.5.7' );
define( 'SPACEFAST_WORDPRESS_VERSION', '0.5.8' );
define( 'SPACEFAST_WORDPRESS_FILE', __FILE__ );

require_once __DIR__ . '/includes/class-spacefast-settings.php';
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function spacefast_accept( bool $condition, string $message ): void {
);

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

Spacefast_Settings::disconnect();
Expand Down
79 changes: 79 additions & 0 deletions tests/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,85 @@ public function run_static_export() { self::$runs++; return true; }
Spacefast_Plugin::static_publish_progress( 'uploading' );
check( 'uploading' === Spacefast_Sync_State::get()['last_status'], 'static upload progress refreshes the delivery heartbeat' );

Spacefast_Static_Publisher::reset();
$retry_requests = array();
$retry_uploads = 0;
$retry_target = array(
'path' => 'index.html',
'method' => 'PUT',
'url' => 'https://uploads.example.test/retry',
'headers' => array(),
);
$retry_client = new Spacefast_Client(
static function ( string $url, array $args ) use ( &$retry_requests, $retry_target ): array {
$retry_requests[] = array( $url, $args['method'] );
if ( 'GET' === $args['method'] ) {
return response( 200, array( 'id' => 'ver_retry', 'status' => 'ready', 'isCurrentProduction' => true ) );
}
if ( str_contains( $url, '/finalize' ) ) {
return response( 202, array( 'id' => 'ver_retry', 'status' => 'finalizing' ) );
}
return response(
201,
array(
'versionId' => 'ver_retry',
'upload' => array(
'summary' => array( 'upload' => 1, 'reused' => 0, 'ignored' => 0 ),
'targets' => array( $retry_target ),
),
)
);
},
static function () use ( &$retry_uploads ): array {
$retry_uploads++;
return response( 1 === $retry_uploads ? 503 : 204 );
}
);
$retry_step = Spacefast_Static_Publisher::step( $archive, $retry_client );
check( false === $retry_step['done'] && 0 === $retry_step['uploaded'], 'a transient upload failure keeps the publish running' );
check( 'ver_retry' === $retry_step['version_id'], 'a transient upload failure keeps the same Spacefast version' );
$retry_step = Spacefast_Static_Publisher::step( $archive, $retry_client );
check( 1 === $retry_step['uploaded'] && 2 === $retry_uploads, 'the next step retries the same upload target' );
$retry_step = Spacefast_Static_Publisher::step( $archive, $retry_client );
check( 'finalizing' === $retry_step['status'], 'a finished upload waits for activation' );
$retry_step = Spacefast_Static_Publisher::step( $archive, $retry_client );
check( true === $retry_step['done'] && 'live' === $retry_step['status'], 'the retried publish goes live' );
$retry_creates = array_filter(
$retry_requests,
static fn ( array $request ): bool => 'POST' === $request[1] && str_ends_with( $request[0], '/versions' )
);
$retry_finalizes = array_filter(
$retry_requests,
static fn ( array $request ): bool => str_contains( $request[0], '/versions/ver_retry/finalize?async=1' )
);
check( 1 === count( $retry_creates ), 'a retried publish never declares a second version' );
check( 1 === count( $retry_finalizes ), 'a finished upload asks Spacefast to finalize once' );

Spacefast_Static_Publisher::reset();
$rejecting_client = new Spacefast_Client(
static function ( string $url, array $args ) use ( $retry_target ): array {
return response(
201,
array(
'versionId' => 'ver_rejected',
'upload' => array(
'summary' => array( 'upload' => 1, 'reused' => 0, 'ignored' => 0 ),
'targets' => array( $retry_target ),
),
)
);
},
static fn (): array => response( 400 )
);
$rejected = false;
try {
Spacefast_Static_Publisher::step( $archive, $rejecting_client );
} catch ( RuntimeException $error ) {
$rejected = 'Spacefast rejected a generated file upload.' === $error->getMessage();
}
check( $rejected, 'a rejected upload still ends the publish' );
Spacefast_Static_Publisher::reset();

Spacefast_Settings::merge( $connection );

unlink( $archive . '/assets/app.js' );
Expand Down
Loading