Skip to content

Commit

Permalink
Extend dead job detection to detect, crawl, and post_process jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
john-shaffer committed Sep 18, 2021
1 parent ead7307 commit a83d562
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 63 deletions.
102 changes: 57 additions & 45 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,58 +585,70 @@ public static function wp2staticProcessQueue() : void {
$jobs = JobQueue::getProcessableJobs();

foreach ( $jobs as $job ) {
JobQueue::setStatus( $job->id, 'processing' );

switch ( $job->job_type ) {
case 'detect':
WsLog::l( 'Starting URL detection' );
$detected_count = URLDetector::detectURLs();
WsLog::l( "URL detection completed ($detected_count URLs detected)" );
break;
case 'crawl':
self::wp2staticCrawl();
break;
case 'post_process':
WsLog::l( 'Starting post-processing' );
$post_processor = new PostProcessor();
$processed_site_dir =
SiteInfo::getPath( 'uploads' ) . 'wp2static-processed-site';
$processed_site = new ProcessedSite();
$post_processor->processStaticSite( StaticSite::getPath() );
WsLog::l( 'Post-processing completed' );
break;
case 'deploy':
$deployer = Addons::getDeployer();

if ( ! $deployer ) {
WsLog::l( 'No deployment add-ons are enabled, skipping deployment.' );
} else {
WsLog::l( 'Starting deployment' );
$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;
}
try {
$lock = 'wp2static_jobs.' . $job->job_type;
$query = "SELECT GET_LOCK('$lock', 30) AS lck";
$locked = intval( $wpdb->get_row( $query )->lck );
if ( ! $locked ) {
WsLog::l( "Failed to acquire \"$lock\" lock." );
return;
}
try {
JobQueue::setStatus( $job->id, 'processing' );

switch ( $job->job_type ) {
case 'detect':
WsLog::l( 'Starting URL detection' );
$detected_count = URLDetector::detectURLs();
WsLog::l( "URL detection completed ($detected_count URLs detected)" );
break;
case 'crawl':
self::wp2staticCrawl();
break;
case 'post_process':
WsLog::l( 'Starting post-processing' );
$post_processor = new PostProcessor();
$processed_site_dir =
SiteInfo::getPath( 'uploads' ) . 'wp2static-processed-site';
$processed_site = new ProcessedSite();
$post_processor->processStaticSite( StaticSite::getPath() );
WsLog::l( 'Post-processing completed' );
break;
case 'deploy':
$deployer = Addons::getDeployer();

if ( ! $deployer ) {
WsLog::l( 'No deployment add-ons are enabled, skipping deployment.' );
} else {
WsLog::l( 'Starting deployment' );
do_action(
'wp2static_deploy',
ProcessedSite::getPath(),
$deployer
);
} finally {
$wpdb->query( "DO RELEASE_LOCK('wp2static_jobs_deploying')" );
}
}
WsLog::l( 'Starting post-deployment actions' );
do_action( 'wp2static_post_deploy_trigger', $deployer );

break;
default:
WsLog::l( 'Trying to process unknown job type' );
WsLog::l( 'Starting post-deployment actions' );
do_action( 'wp2static_post_deploy_trigger', $deployer );

break;
default:
WsLog::l( 'Trying to process unknown job type' );
}

JobQueue::setStatus( $job->id, 'completed' );
} catch ( \Throwable $e ) {
JobQueue::setStatus( $job->id, 'failed' );
// We don't want to crawl and deploy if the detect step fails.
// Skip all waiting jobs when one fails.
$table_name = $wpdb->prefix . 'wp2static_jobs';
$wpdb->query(
"UPDATE $table_name
SET status = 'skipped'
WHERE status = 'waiting'"
);
throw $e;
} finally {
$wpdb->query( "DO RELEASE_LOCK('$lock')" );
}

JobQueue::setStatus( $job->id, 'completed' );
}
}

Expand Down
40 changes: 22 additions & 18 deletions src/JobQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,30 +240,34 @@ public static function truncate() : void {
public static function markFailedJobs() : void {
global $wpdb;

$job_types = [ 'detect', 'crawl', 'post_process', 'deploy' ];
$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." );
foreach ( $job_types as $type ) {
try {
$lock = "wp2static_jobs.$type";
$query = "SELECT IS_FREE_LOCK('$lock') AS free";
$free = intval( $wpdb->get_row( $query )->free );

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

$wpdb->query( 'COMMIT' );
} catch ( \Throwable $e ) {
$wpdb->query( 'ROLLBACK' );
throw $e;
$wpdb->query( 'COMMIT' );
} catch ( \Throwable $e ) {
$wpdb->query( 'ROLLBACK' );
throw $e;
}
}
}
}

0 comments on commit a83d562

Please sign in to comment.