feat(M01-C): 建立MySQL异步任务基础

This commit is contained in:
Codex
2026-06-18 10:20:39 +08:00
parent a2d578f73b
commit 2b90d1f101
15 changed files with 609 additions and 30 deletions
@@ -0,0 +1,5 @@
-- Roll back M01-C asynchronous task tables.
DELETE FROM qipai_schema_migrations WHERE version = '2026061802';
DROP TABLE IF EXISTS qipai_async_tasks;
DROP TABLE IF EXISTS qipai_outbox_events;
@@ -0,0 +1,48 @@
-- M01-C lightweight asynchronous task foundation.
-- Tasks and outbox events are durable in MySQL and safe to resume after worker restarts.
CREATE TABLE IF NOT EXISTS qipai_outbox_events (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
tenant_id BIGINT UNSIGNED NOT NULL,
aggregate_type VARCHAR(64) NOT NULL,
aggregate_id VARCHAR(64) NOT NULL,
event_type VARCHAR(128) NOT NULL,
idempotency_key VARCHAR(191) NOT NULL,
payload JSON NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'PENDING',
available_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
published_at DATETIME(3) NULL,
attempts INT UNSIGNED NOT NULL DEFAULT 0,
last_error VARCHAR(1000) NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
CONSTRAINT fk_qipai_outbox_events_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
UNIQUE KEY uq_qipai_outbox_events_idempotency (tenant_id, idempotency_key),
KEY idx_qipai_outbox_events_dispatch (status, available_at, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS qipai_async_tasks (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
tenant_id BIGINT UNSIGNED NOT NULL,
task_type VARCHAR(128) NOT NULL,
idempotency_key VARCHAR(191) NOT NULL,
payload JSON NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'PENDING',
priority INT NOT NULL DEFAULT 0,
available_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
lease_owner VARCHAR(128) NULL,
lease_expires_at DATETIME(3) NULL,
attempts INT UNSIGNED NOT NULL DEFAULT 0,
max_attempts INT UNSIGNED NOT NULL DEFAULT 8,
last_error VARCHAR(1000) NULL,
completed_at DATETIME(3) NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
CONSTRAINT fk_qipai_async_tasks_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
UNIQUE KEY uq_qipai_async_tasks_idempotency (tenant_id, task_type, idempotency_key),
KEY idx_qipai_async_tasks_claim (status, available_at, priority, id),
KEY idx_qipai_async_tasks_lease (status, lease_expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026061802', 'm01c_async_tasks');
@@ -0,0 +1,29 @@
-- Verify M01-C asynchronous task foundation.
SELECT table_name
FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name IN ('qipai_outbox_events', 'qipai_async_tasks')
ORDER BY table_name;
SELECT table_name, index_name
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND (
(table_name = 'qipai_outbox_events' AND index_name IN (
'uq_qipai_outbox_events_idempotency',
'idx_qipai_outbox_events_dispatch'
))
OR
(table_name = 'qipai_async_tasks' AND index_name IN (
'uq_qipai_async_tasks_idempotency',
'idx_qipai_async_tasks_claim',
'idx_qipai_async_tasks_lease'
))
)
GROUP BY table_name, index_name
ORDER BY table_name, index_name;
SELECT version, name
FROM qipai_schema_migrations
WHERE version = '2026061802';