chore: using `uniqid` to generate log file names

This commit is contained in:
vnmedeiros 2024-03-10 18:18:22 -03:00
parent b61b4e5836
commit ca733b17ea
4 changed files with 53 additions and 6 deletions

View File

@ -242,8 +242,9 @@ class REST_Background_Processes_Controller extends REST_Controller {
}
public function prepare_item_for_response($item, $request) {
$item->log = $this->get_log_url($item->ID, $item->action);
$item->error_log = $this->get_log_url($item->ID, $item->action, 'error');
$key_log = $item->bg_uuid ?? $item->ID;
$item->log = $this->get_log_url($key_log, $item->action);
$item->error_log = $this->get_log_url($key_log, $item->action, 'error');
$nonce = wp_create_nonce( 'wp_rest' );
$item->output = str_replace("&_wpnonce=[nonce]", "&_wpnonce=$nonce", $item->output);
return $item;

View File

@ -114,7 +114,8 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
'action' => $this->action,
'name' => $this->get_name(),
'queued_on' => date('Y-m-d H:i:s'),
'status' => 'waiting'
'status' => 'waiting',
'bg_uuid' => uniqid(),
]
);
$this->ID = $wpdb->insert_id;
@ -263,6 +264,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
$batch->key = $query->ID;
$batch->data = maybe_unserialize( $query->data );
$batch->status = $query->status;
$batch->bg_uuid = $query->bg_uuid;
if ($batch->status != 'running') {
$this->open($batch->key);
@ -271,6 +273,33 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
return $batch;
}
/**
* Get batch by key ID
*
* @return stdClass Return the batch
*/
protected function get_batch_by_key($key) {
global $wpdb;
$table = $this->table;
$query = $wpdb->get_row( $wpdb->prepare( "
SELECT *
FROM {$table}
WHERE action = %s
AND ID = %s
LIMIT 1
", $this->action, $key ) );
$batch = new \stdClass();
$batch->key = $query->ID;
$batch->data = maybe_unserialize( $query->data );
$batch->status = $query->status;
$batch->bg_uuid = $query->bg_uuid;
return $batch;
}
/**
* Handle
*
@ -428,10 +457,14 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
}
protected function write_log($key, $log) {
$this->write_log_to_file($key, $log);
$batch = $this->get_batch_by_key($key);
$key_log = $batch->bg_uuid ?? $key;
$this->write_log_to_file($key_log, $log);
}
protected function write_error_log($key, $log) {
$this->write_log_to_file($key, $log, 'error');
$batch = $this->get_batch_by_key($key);
$key_log = $batch->bg_uuid ?? $key;
$this->write_log_to_file($key_log, $log, 'error');
}
private function recursive_stingify_log_array(array $log, $break = true) {

View File

@ -689,7 +689,6 @@ abstract class Exporter {
$upload_dir_info = wp_upload_dir();
$prefix = $this->get_id();
$upload_dir = trailingslashit( $upload_dir_info['basedir'] );
// $upload_url = trailingslashit( $upload_dir_info['baseurl'] );
$exporter_folder = 'tainacan/exporter';
$file_suffix = "{$exporter_folder}/{$prefix}_{$key}";

View File

@ -509,6 +509,20 @@ class Migrations {
);
}
static function alter_table_tnc_bg_process_add_uuid() {
global $wpdb;
// update default order by "creation_date" to "date"
$table_name = $wpdb->prefix . 'tnc_bg_process';
$column_exists = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table_name' AND column_name = 'bg_uuid'" );
if(empty($column_exists)) {
$wpdb->query("
ALTER TABLE $table_name
ADD bg_uuid text NULL
");
}
}
}