From 02013716cc16294bfdc3644c1e36945b637c8ccd Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Thu, 18 Apr 2024 17:46:27 -0300 Subject: [PATCH] fix: add transaction control on execute BG procedures --- .../class-tainacan-background-process.php | 18 ++++++++++++++---- src/classes/libs/wp-background-process.php | 7 +++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/classes/class-tainacan-background-process.php b/src/classes/class-tainacan-background-process.php index 1f932839d..1dfa5db38 100644 --- a/src/classes/class-tainacan-background-process.php +++ b/src/classes/class-tainacan-background-process.php @@ -104,6 +104,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { if ( ! empty( $this->data ) ) { global $wpdb; + $wpdb->query('START TRANSACTION'); $wpdb->insert( $this->table, [ @@ -119,6 +120,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { ] ); $this->ID = $wpdb->insert_id; + $wpdb->query('COMMIT'); } return $this; @@ -139,6 +141,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { if (!isset($batch->output)) { $batch->output = ''; } + $wpdb->query('START TRANSACTION'); $wpdb->update( $this->table, [ @@ -150,6 +153,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { ], ['ID' => $key] ); + $wpdb->query('COMMIT'); } return $this; @@ -164,6 +168,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { */ public function open( $key ) { global $wpdb; + $wpdb->query('START TRANSACTION'); $wpdb->update( $this->table, [ @@ -171,6 +176,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { ], ['ID' => $key] ); + $wpdb->query('COMMIT'); return $this; } @@ -196,12 +202,13 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { $params['progress_label'] = __('Process completed with errors','tainacan'); $params['progress_value'] = 100; } + $wpdb->query('START TRANSACTION'); $wpdb->update( $this->table, $params, ['ID' => $key] ); - + $wpdb->query('COMMIT'); return $this; } @@ -214,10 +221,12 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { */ public function delete( $key ) { global $wpdb; + $wpdb->query('START TRANSACTION'); $wpdb->delete( $this->table, ['ID' => $key] ); + $wpdb->query('COMMIT'); return $this; } @@ -353,7 +362,8 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { $task = $this->task( $task ); } catch (\Exception $e) { // TODO: Add Stacktrace - $this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Fatal Error: ' . $e->getMessage()]]); + $this->debug('Fatal Error: ' . $e->getMessage()); + $this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Try Fatal Error: ' . $e->getMessage()]]); $this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Process aborted']]); $task = false; $close_status = 'errored'; @@ -398,9 +408,9 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { global $wpdb; $table = $this->table; - + $wpdb->query('START TRANSACTION'); $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE done = FALSE AND action LIKE %s", $this->action ) ); // @codingStandardsIgnoreLine. - + $wpdb->query('COMMIT'); return $this; } diff --git a/src/classes/libs/wp-background-process.php b/src/classes/libs/wp-background-process.php index 4acd9cf8f..a9108964b 100644 --- a/src/classes/libs/wp-background-process.php +++ b/src/classes/libs/wp-background-process.php @@ -81,7 +81,7 @@ * @var string * @access protected */ - protected $cron_interval; + protected $cron_interval = 5; /** * Initiate new background process @@ -289,8 +289,11 @@ * @return $this */ protected function unlock_process() { - $this->debug('unlocking process'); + $this->debug('unlocking process: '. $this->identifier . '_process_lock'); + global $wpdb; + $wpdb->query('START TRANSACTION'); delete_site_transient( $this->identifier . '_process_lock' ); + $wpdb->query('COMMIT'); return $this; }