我们有一些在用户上传媒体时创建的不同任务(例如,过程图像,过程视频)。我们目前的概念是拥有一个主dest
,它是所有任务类型的容器,而Task
具有处理任务所需的所有元数据。
我看过无数个案例,答案是使用Redis,但我们希望保留一个任务历史来计算平均任务时间等内容,并且子任务的元数据可能很复杂。
我对PostgreSQL不太熟悉,所以把它看作伪代码:
Subtask
这会有用吗?
修改1:使用BEGIN TRANSACTION;
-- Find an unclaimed task.
-- Claim the task.
-- Prevent another worker from also claiming this task.
UPDATE (
SELECT FROM subtasks
INNER JOIN tasks
ON tasks.id = subtasks.id
WHERE tasks.started_at = NULL -- Not claimed
ORDER BY tasks.created_at ASC -- First in, first out
LIMIT 1
FOR UPDATE SKIP LOCKED -- Don't wait for it, keep looking.
)
SET tasks.started_at = now()
RETURNING *
-- Use metadata from the subtask to perform the task.
-- If an error occurs we can roll back, unlocking the row.
-- Will this also roll back if the worker dies?
-- Mark the task as complete.
UPDATE tasks
SET completed_at = now()
WHERE tasks.id = $id
END TRANSACTION;
并且没有子选择。
clock_timestamp()