Add OutOfRetries exception
... and clean up documentation.
This commit is contained in:
parent
f187790136
commit
db69f168de
2 changed files with 17 additions and 7 deletions
|
|
@ -96,8 +96,7 @@ module EuphApi.Bot (
|
||||||
, BotException(..)
|
, BotException(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
-- TODO: Move 'PasswordNeeded' to 'ExitState'?
|
-- TODO: Add 'AuthenticationFailed' exception.
|
||||||
-- TODO: Add 'WrongPassword' exception or 'ExitState'.
|
|
||||||
|
|
||||||
import Control.Concurrent
|
import Control.Concurrent
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
|
|
@ -122,8 +121,8 @@ infoM :: String -> IO ()
|
||||||
infoM = L.infoM moduleName
|
infoM = L.infoM moduleName
|
||||||
noticeM :: String -> IO ()
|
noticeM :: String -> IO ()
|
||||||
noticeM = L.noticeM moduleName
|
noticeM = L.noticeM moduleName
|
||||||
warningM :: String -> IO ()
|
--warningM :: String -> IO ()
|
||||||
warningM = L.warningM moduleName
|
--warningM = L.warningM moduleName
|
||||||
--errorM :: String -> IO ()
|
--errorM :: String -> IO ()
|
||||||
--errorM = L.errorM moduleName
|
--errorM = L.errorM moduleName
|
||||||
--criticalM :: String -> IO ()
|
--criticalM :: String -> IO ()
|
||||||
|
|
@ -135,7 +134,6 @@ warningM = L.warningM moduleName
|
||||||
|
|
||||||
data ExitState = Stopping
|
data ExitState = Stopping
|
||||||
| Restarting
|
| Restarting
|
||||||
| OutOfRetries
|
|
||||||
|
|
||||||
data BotState b c = BotState
|
data BotState b c = BotState
|
||||||
{ bAddress :: TVar String
|
{ bAddress :: TVar String
|
||||||
|
|
@ -279,7 +277,6 @@ runBot ioConfig = do
|
||||||
result <- runBotOnce config
|
result <- runBotOnce config
|
||||||
case result of
|
case result of
|
||||||
Stopping -> void $ noticeM "Bot has stopped."
|
Stopping -> void $ noticeM "Bot has stopped."
|
||||||
OutOfRetries -> void $ warningM "Bot ran out of retries."
|
|
||||||
Restarting -> noticeM "Bot has restarted." >> runBot ioConfig
|
Restarting -> noticeM "Bot has restarted." >> runBot ioConfig
|
||||||
|
|
||||||
reconnect :: Integer -> Bot b c ExitState
|
reconnect :: Integer -> Bot b c ExitState
|
||||||
|
|
@ -290,7 +287,7 @@ reconnect retries = do
|
||||||
Just s -> return s
|
Just s -> return s
|
||||||
Nothing ->
|
Nothing ->
|
||||||
case bReconnectPolicy state retries of
|
case bReconnectPolicy state retries of
|
||||||
Nothing -> return OutOfRetries
|
Nothing -> liftIO $ throwIO OutOfRetries
|
||||||
Just delay -> do
|
Just delay -> do
|
||||||
liftIO $ infoM $ "Attempting reconnect in " ++ show (delay `div` 1000000)
|
liftIO $ infoM $ "Attempting reconnect in " ++ show (delay `div` 1000000)
|
||||||
++ "s (" ++ show delay ++ "µs)."
|
++ "s (" ++ show delay ++ "µs)."
|
||||||
|
|
@ -462,10 +459,14 @@ data BotException = NoOwnViewYet
|
||||||
-- valid methods of authentication (password).
|
-- valid methods of authentication (password).
|
||||||
-- As long as the server is working properly, this exception should
|
-- As long as the server is working properly, this exception should
|
||||||
-- not occur.
|
-- not occur.
|
||||||
|
| OutOfRetries
|
||||||
|
-- ^ The bot's 'reconnectPolicy' has returned a @Nothing@ value,
|
||||||
|
-- meaning that the bot should not attempt to reconnect any further.
|
||||||
|
|
||||||
instance Show BotException where
|
instance Show BotException where
|
||||||
show NoOwnViewYet = "Bot hasn't received a SessionView of itself yet."
|
show NoOwnViewYet = "Bot hasn't received a SessionView of itself yet."
|
||||||
show PasswordNeeded = "Bot needs to authenticate, but has no password."
|
show PasswordNeeded = "Bot needs to authenticate, but has no password."
|
||||||
show NoValidAuthenticationMethods = "Server gave no valid authentication methods."
|
show NoValidAuthenticationMethods = "Server gave no valid authentication methods."
|
||||||
|
show OutOfRetries = "Bot has ran out of reconnect retries."
|
||||||
|
|
||||||
instance Exception BotException
|
instance Exception BotException
|
||||||
|
|
|
||||||
|
|
@ -91,13 +91,22 @@ killCommand t = E.specificCommand "kill" $ \msg -> do
|
||||||
void $ B.reply (E.msgID msg) t
|
void $ B.reply (E.msgID msg) t
|
||||||
B.stop
|
B.stop
|
||||||
|
|
||||||
|
-- | Version of 'killCommand' where the bot does not reply to the message which kills it.
|
||||||
killCommandSilent :: E.Command b c
|
killCommandSilent :: E.Command b c
|
||||||
killCommandSilent = E.specificCommand "kill" $ const B.stop
|
killCommandSilent = E.specificCommand "kill" $ const B.stop
|
||||||
|
|
||||||
|
-- | Specific restart command: @!restart \@botname@
|
||||||
|
--
|
||||||
|
-- When restarted, the bot receiving the command should be !killed and a
|
||||||
|
-- new instance of the same bot should be started.
|
||||||
|
--
|
||||||
|
-- Bots __may implement__ this command.
|
||||||
restartCommand :: T.Text -> E.Command b c
|
restartCommand :: T.Text -> E.Command b c
|
||||||
restartCommand t = E.specificCommand "restart" $ \msg -> do
|
restartCommand t = E.specificCommand "restart" $ \msg -> do
|
||||||
void $ B.reply (E.msgID msg) t
|
void $ B.reply (E.msgID msg) t
|
||||||
B.restart
|
B.restart
|
||||||
|
|
||||||
|
-- | Version of 'restartCommand' where the bot does not reply to the message
|
||||||
|
-- which restarts it.
|
||||||
restartCommandSilent :: E.Command b c
|
restartCommandSilent :: E.Command b c
|
||||||
restartCommandSilent = E.specificCommand "restart" $ const B.restart
|
restartCommandSilent = E.specificCommand "restart" $ const B.restart
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue