我有一个愚蠢的问题,scotty web应用程序和mongodb服务以正确的顺序开始。我使用systemd首先启动mongodb,然后是scotty web应用程序。由于某种原因,它不起作用。应用程序与mongodb驱动程序的connect: does not exist (Connection refused)
错误,这意味着连接尚未准备好。
我的问题是。如何以0.5s的间隔测试连接可用性三次,然后才出现错误?
这是应用程序的主函数
main :: IO ()
main = do
pool <- createPool (runIOE $ connect $ host "127.0.0.1") close 1 300 5
clearSessions pool
let r = x -> runReaderT x pool
scottyT 3000 r r basal
basal :: ScottyD ()
basal = do
middleware $ staticPolicy (noDots >-> addBase "static")
notFound $ runSession
routes
虽然app服务在mongodb服务之后排序,但在app启动期间,与mongodb的连接仍然不可用。所以我得到了上面提到的误差。这是systemd服务文件,用于避免有关正确服务排序的问题。
[Unit]
Description=Basal Web Application
Requires=mongodb.service
After=mongodb.service iptables.service network-online.target
[Service]
User=http
Group=http
WorkingDirectory=/srv/http/basal/
ExecStart=/srv/http/basal/bin/basal
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
我不知道为什么连接到mongodb是不可用的给定正确的服务顺序。所以我想在haskell代码中探测连接可用性三次,延迟0.5s,然后出错。我该怎么做呢?
谢谢。
从你使用的函数来看,我猜你使用的是mongoDB 1.5.0
之类的东西。
这里,connect
返回IOE
单子中的某个东西,这是ErrorT
IOError
IO
的别名。
所以最好的方法是使用ErrorT
提供的重试机制。因为它是MonadPlus
的一个实例,如果我们不关心检查特定的错误,我们可以只使用mplus
:
retryConnect :: Int -> Int -> Host -> IOE Pipe
retryConnect retries delayInMicroseconds host
| retries > 0 =
connect host `mplus`
(liftIO (threadDelay delayInMicroseconds) >>
retryConnect (retries - 1) delayInMicroseconds host)
| otherwise = connect host
(threadDelay
来自Control.Concurrent
)。
然后将connect
替换为retryConnect 2 500000
,它将在第一次失败后重试两次,间隔500,000微秒(即0.5s)。
如果你想检查一个特定的错误,然后使用catchError
代替,检查错误,以决定是否吞下它或重新抛出它