Jake Vanderwerf
2026-03-03 772462eeca3002a1d52508aeba485aab2b4742ad
inc/managers/queue/Queue.php
@@ -4,6 +4,7 @@
   exit;
}
use JVBase\managers\CustomTable;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
@@ -18,6 +19,7 @@
   public function __construct()
   {
      $this->defineTables();
      $this->storage = new Storage();
      $this->registry = new TypeRegistry();
      $this->locker = new Locker();
@@ -39,6 +41,84 @@
      add_filter(BASE.'admin_action_filter', [$this, 'adminActionFilter'], 10, 3);
   }
   public static function defineTables():void
   {
      $queue = CustomTable::for('_operation_queue');
      $queue->setColumns([
         'id'        => 'VARCHAR(64) NOT NULL',
         'type'         => 'VARCHAR(50) NOT NULL',
         'user_id'      => $queue->getUserIDType().' NOT NULL',
         'request_data' => 'JSON NOT NULL CHECK (JSON_VALID(request_data))',
         'total_items'  => 'INT(11) NOT NULL DEFAULT 1',
         'processed_items' => 'INT(11) DEFAULT 0',
         'failed_items' => 'JSON',
         'priority'     => 'ENUM(\'high\',\'normal\',\'low\') DEFAULT \'normal\'',
         'state'        => 'ENUM(\'pending\', \'scheduled\', \'processing\', \'completed\') DEFAULT \'pending\'',
         'outcome'      => 'ENUM(\'pending\', \'success\',\'partial\',\'merged\',\'failed\',\'failed_permanent\') DEFAULT \'pending\'',
         'retries'      => 'INT(11) DEFAULT 0',
         'last_error_hash'=> 'CHAR(32) DEFAULT NULL',
         'error_message'   => 'TEXT',
         'scheduled_at' => 'DATETIME DEFAULT NULL',
         'started_at'   => 'DATETIME DEFAULT CURRENT_TIMESTAMP',
         'completed_at' => 'DATETIME DEFAULT NULL',
         'metadata'     => 'JSON DEFAULT NULL',
         'result'    => 'JSON',
         'dependencies' => 'JSON',
         'merged_into'  => 'VARCHAR(64) DEFAULT NULL',
         'user_dismissed'=> 'tinyint(1) DEFAULT 0',
         'created_at'   => 'DATETIME DEFAULT CURRENT_TIMESTAMP',
         'updated_at'   => 'DATETIME DEFAULT CURRENT_TIMESTAMP',
      ]);
      $queue->setKeys([
         ['key' => 'PRIMARY', 'value' => 'id'],
         '`idx_run_queue` (`state`, `priority`, `scheduled_at`)',
         '`idx_user_ops` (`user_id`, `state`)',
         '`idx_user_type_pending` (`user_id`, `type`, `state`)',
         '`idx_completed_at` (`completed_at`)',
         '`idx_processing_stuck` (`state`, `started_at`)'
      ]);
      $queue->defineTable();
      $stats = CustomTable::for('stats__operation_queue');
      $stats->setColumns([
         'id'  => 'BIGINT unsigned AUTO_INCREMENT',
         'date'   => 'DATE NOT NULL',
         'type'   => 'VARCHAR(50) NOT NULL',
         'total_operations'      => 'INT NOT NULL DEFAULT 0',
         'successful_operations' => 'INT NOT NULL DEFAULT 0',
         'partial_operations' => 'INT NOT NULL DEFAULT 0',
         'failed_operations'     => 'INT NOT NULL DEFAULT 0',
         'failed_permanent_operations'=> 'INT NOT NULL DEFAULT 0',
         'total_items_processed' => 'INT NOT NULL DEFAULT 0',
         'average_duration'      => 'FLOAT DEFAULT NULL',
         'max_duration'       => 'INT DEFAULT NULL',
         'peak_queue_size'    => 'INT NOT NULL DEFAULT 0',
         'peak_memory_usage'     => 'INT DEFAULT NULL',
         'peak_cpu_usage'     => 'FLOAT DEFAULT NULL',
         'created_at'         => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
      ]);
      $stats->setKeys([
         ['key' => 'PRIMARY', 'value' => 'id'],
         ['key' => 'UNIQUE', 'value' => '(`date`, `type`)'],
         '`date_idx` (`date`)',
         '`type_idx` (`type`)'
      ]);
      $stats->defineTable();
   }
   /**
    * Access type registry for registering operation configs
    */