CREATE TABLE sim_job_queue ( id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, job_type TEXT NOT NULL, sim_id INT, -- NOT NULL??? payload JSONB NOT NULL, status TEXT NOT NULL DEFAULT "pending", -- pasar a enum priority SMALLINT NOT NULL DEFAULT 100, -- trabajamos entre 200 y 0 para 0 la maxima prioridad retry_count INT NOT NULL DEFAULT 0, max_retries INT NOT NULL DEFAULT 3, -- lo definiria en runtime scheduled_for TIMESTAMP NOT NULL DEFAULT now(), created_at TIMESTAMP NOT NULL DEFAULT now(), started_at TIMESTAMP, completed_at TIMESTAMP, error_msg TEXT, -- En teoria el check es mas flexible que el enum CONSTRAINT valid_status CHECK ( status IN ('pending', 'processing', 'completed', 'failed', 'dead') ) -- Revisar si interesa asociar cada trabajo a un id o permitir que sean independientes CONSTRAINT fk_sim_id FOREIGN KEY(sim_id) REFERENCES tableName(tarjetas_sim) ) -- Indice de pendientes de consumir CREATE INDEX idx_job_fetch ON sim_job_queue (status, scheduled_for, priority DESC, created_at) WHERE status = 'pending'; -- Indice del resto de procesos CREATE INDEX idx_job_monitor ON sim_job_queue (job_type, status, created_at);