Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect dead jobs and set to 'failed' status #806

Merged
merged 2 commits into from
Sep 18, 2021
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
23 changes: 18 additions & 5 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ public static function wp2staticManuallyEnqueueJobs() : void {
earlier jobs of the same type having been "squashed" first
*/
public static function wp2staticProcessQueue() : void {
global $wpdb;

JobQueue::markFailedJobs();
// skip any earlier jobs of same type still in 'waiting' status
JobQueue::squashQueue();

Expand Down Expand Up @@ -609,11 +612,21 @@ public static function wp2staticProcessQueue() : void {
WsLog::l( 'No deployment add-ons are enabled, skipping deployment.' );
} else {
WsLog::l( 'Starting deployment' );
do_action(
'wp2static_deploy',
ProcessedSite::getPath(),
$deployer
);
$query = "SELECT GET_LOCK('wp2static_jobs_deploying', 30) AS lck";
$locked = intval( $wpdb->get_row( $query )->lck );
if ( ! $locked ) {
WsLog::l( 'Failed to acquire "wp2static_jobs_deploying" lock.' );
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this still let registered post deployment hooks fire? ie, if S3 deploy fails and we return here, might it still call CF invalidation unnecessarily?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, we're not catching the exceptions so it will not run post-deploy hooks in that case.

}
try {
do_action(
'wp2static_deploy',
ProcessedSite::getPath(),
$deployer
);
} finally {
$wpdb->query( "DO RELEASE_LOCK('wp2static_jobs_deploying')" );
Copy link
Contributor

Choose a reason for hiding this comment

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

If this ever fails, do we have a way to manually clean up, like with caches?

Copy link
Contributor Author

@john-shaffer john-shaffer Sep 18, 2021

Choose a reason for hiding this comment

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

In testing, releasing here was not even necessary because WP apparently closes the connection after the request, which implicitly releases the locks. If we ever run into a problem with a customized WP or something, we can add a config option to turn this off.

}
}
WsLog::l( 'Starting post-deployment actions' );
do_action( 'wp2static_post_deploy_trigger', $deployer );
Expand Down
35 changes: 35 additions & 0 deletions src/JobQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,39 @@ public static function truncate() : void {
WsLog::l( 'failed to truncate JobQueue: try deleting instead' );
}
}

/**
* Detect any 'processing' jobs that are not running and change status to 'failed'.
*
* @throws \Throwable
*/
public static function markFailedJobs() : void {
global $wpdb;

$table_name = $wpdb->prefix . 'wp2static_jobs';

$wpdb->query( 'START TRANSACTION' );

try {
$query = "SELECT IS_FREE_LOCK('wp2static_jobs_deploying') AS free";
$deploy_free = intval( $wpdb->get_row( $query )->free );

if ( $deploy_free ) {
$failed_jobs = $wpdb->query(
"UPDATE $table_name
SET status = 'failed'
WHERE job_type = 'deploy' AND status = 'processing'"
);
if ( $failed_jobs ) {
$s = $failed_jobs === 1 ? '' : 's';
WsLog::l( "$failed_jobs processing deploy job$s marked as failed." );
}
}

$wpdb->query( 'COMMIT' );
} catch ( \Throwable $e ) {
$wpdb->query( 'ROLLBACK' );
throw $e;
}
}
}
3 changes: 3 additions & 0 deletions src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ function ( $path ) use ( $s ) {
}

public static function renderJobsPage() : void {
JobQueue::markFailedJobs();
JobQueue::squashQueue();

$view = [];
$view['nonce_action'] = 'wp2static-ui-job-options';
$view['jobs'] = JobQueue::getJobs();
Expand Down