我的API中有一条路线将作业启动到消息队列(我使用AMQP包)。我希望保持连接打开并在作业完成时响应Web请求。
使用AMQP,我无法知道我开始的工作何时完成。相反,我可以订阅一个将在作业完成时报告的频道。
这里有一些伪代码:
main = do
doneChannel = openAMQPChannel "done"
-- assume some other program subscribes to jobs, performs them
-- and puts them onto "done" when finished.
jobsChannel = openAMQPChannel "jobs"
forkIO $ subscribeAMQP doneChannel onDone
startServer
onDone jobId info = do
-- How can I return this info in my request?
-- assume it has unique identifying information
???
startServer jobs = do
Warp.run 8000 $ do
post "/jobs" $ \payload -> do
jobId <- generateUniqueId
publishAMQP jobs jobId payload
-- TODO wait until onDone happens for my particular request
info <- waitForJobToComplete jobId
return info
(有关真实的AMQP界面,请参阅here。我使用Servant进行路由)
有没有办法用Control.Concurrent
执行此操作? MVar
和Chan
似乎无法通过此类ID进行寻址。我怎么能以一种能够一次处理数千个请求的方式实现它呢?
还有其他想法吗?