Skip to content
Closed
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
26 changes: 25 additions & 1 deletion includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,27 @@ public function run_cron( $max = 100, $job_id = 0 ) {

$feedzy_imports = get_posts( $args );
foreach ( $feedzy_imports as $job ) {
$lock_key = 'feedzy_import_lock_' . $job->ID;

// Determine lock TTL based on max execution time (plus buffer) and allow filtering.
$max_execution_time = (int) apply_filters( 'feedzy_max_execution_time', 500 );
if ( $max_execution_time <= 0 ) {
$max_execution_time = 300; // Fallback to 5 minutes if unlimited or not set.
}
$lock_ttl = (int) apply_filters( 'feedzy_import_lock_ttl', $max_execution_time + 60, $job );

// Atomically try to acquire lock - wp_cache_add returns false if key already exists.
if ( ! wp_cache_add( $lock_key, time(), 'feedzy_import_locks', $lock_ttl ) ) {
Feedzy_Rss_Feeds_Log::info(
'Skipping job: ' . $job->post_title,
array(
'job_id' => $job->ID,
'reason' => 'concurrent_execution_prevented',
)
);
continue;
}

try {
$result = $this->run_job( $job, $max );

Expand All @@ -1567,7 +1588,7 @@ public function run_cron( $max = 100, $job_id = 0 ) {
);
}
do_action( 'feedzy_run_cron_extra', $job );
} catch ( Exception $e ) {
} catch ( Throwable $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[Feedzy Run Cron][Post title: ' . ( ! empty( $job->post_title ) ? $job->post_title : '' ) . '] Error: ' . $e->getMessage() );
}
Expand All @@ -1580,6 +1601,9 @@ public function run_cron( $max = 100, $job_id = 0 ) {
'error' => $e->getMessage(),
)
);
} finally {
// Always release the lock, even on error.
wp_cache_delete( $lock_key, 'feedzy_import_locks' );
}
}
}
Expand Down