fix: add transaction control on execute BG procedures

This commit is contained in:
vnmedeiros 2024-04-18 17:46:27 -03:00
parent 68cf97f35c
commit 02013716cc
2 changed files with 19 additions and 6 deletions

View File

@ -104,6 +104,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
if ( ! empty( $this->data ) ) { if ( ! empty( $this->data ) ) {
global $wpdb; global $wpdb;
$wpdb->query('START TRANSACTION');
$wpdb->insert( $wpdb->insert(
$this->table, $this->table,
[ [
@ -119,6 +120,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
] ]
); );
$this->ID = $wpdb->insert_id; $this->ID = $wpdb->insert_id;
$wpdb->query('COMMIT');
} }
return $this; return $this;
@ -139,6 +141,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
if (!isset($batch->output)) { if (!isset($batch->output)) {
$batch->output = ''; $batch->output = '';
} }
$wpdb->query('START TRANSACTION');
$wpdb->update( $wpdb->update(
$this->table, $this->table,
[ [
@ -150,6 +153,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
], ],
['ID' => $key] ['ID' => $key]
); );
$wpdb->query('COMMIT');
} }
return $this; return $this;
@ -164,6 +168,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
*/ */
public function open( $key ) { public function open( $key ) {
global $wpdb; global $wpdb;
$wpdb->query('START TRANSACTION');
$wpdb->update( $wpdb->update(
$this->table, $this->table,
[ [
@ -171,6 +176,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
], ],
['ID' => $key] ['ID' => $key]
); );
$wpdb->query('COMMIT');
return $this; 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_label'] = __('Process completed with errors','tainacan');
$params['progress_value'] = 100; $params['progress_value'] = 100;
} }
$wpdb->query('START TRANSACTION');
$wpdb->update( $wpdb->update(
$this->table, $this->table,
$params, $params,
['ID' => $key] ['ID' => $key]
); );
$wpdb->query('COMMIT');
return $this; return $this;
} }
@ -214,10 +221,12 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
*/ */
public function delete( $key ) { public function delete( $key ) {
global $wpdb; global $wpdb;
$wpdb->query('START TRANSACTION');
$wpdb->delete( $wpdb->delete(
$this->table, $this->table,
['ID' => $key] ['ID' => $key]
); );
$wpdb->query('COMMIT');
return $this; return $this;
} }
@ -353,7 +362,8 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
$task = $this->task( $task ); $task = $this->task( $task );
} catch (\Exception $e) { } catch (\Exception $e) {
// TODO: Add Stacktrace // 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']]); $this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Process aborted']]);
$task = false; $task = false;
$close_status = 'errored'; $close_status = 'errored';
@ -398,9 +408,9 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
global $wpdb; global $wpdb;
$table = $this->table; $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( $wpdb->prepare( "DELETE FROM {$table} WHERE done = FALSE AND action LIKE %s", $this->action ) ); // @codingStandardsIgnoreLine.
$wpdb->query('COMMIT');
return $this; return $this;
} }

View File

@ -81,7 +81,7 @@
* @var string * @var string
* @access protected * @access protected
*/ */
protected $cron_interval; protected $cron_interval = 5;
/** /**
* Initiate new background process * Initiate new background process
@ -289,8 +289,11 @@
* @return $this * @return $this
*/ */
protected function unlock_process() { 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' ); delete_site_transient( $this->identifier . '_process_lock' );
$wpdb->query('COMMIT');
return $this; return $this;
} }