2012-06-13 21:54:23 +00:00
|
|
|
{- git-annex assistant sanity checker
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
2012-06-23 05:20:40 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
2012-06-13 21:54:23 +00:00
|
|
|
-}
|
|
|
|
|
2012-06-25 20:10:10 +00:00
|
|
|
module Assistant.Threads.SanityChecker (
|
2012-06-13 21:54:23 +00:00
|
|
|
sanityCheckerThread
|
|
|
|
) where
|
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
import Assistant.Common
|
2012-06-13 21:54:23 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-07-29 21:53:18 +00:00
|
|
|
import Assistant.Alert
|
2012-07-20 23:29:59 +00:00
|
|
|
import qualified Git.LsFiles
|
2012-06-13 21:54:23 +00:00
|
|
|
import Utility.ThreadScheduler
|
2012-06-25 20:10:10 +00:00
|
|
|
import qualified Assistant.Threads.Watcher as Watcher
|
2012-06-13 21:54:23 +00:00
|
|
|
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
|
|
|
|
{- This thread wakes up occasionally to make sure the tree is in good shape. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
sanityCheckerThread :: NamedThread
|
|
|
|
sanityCheckerThread = NamedThread "SanityChecker" $ forever $ do
|
|
|
|
waitForNextCheck
|
2012-06-13 21:54:23 +00:00
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
debug ["starting sanity check"]
|
2012-10-29 20:49:47 +00:00
|
|
|
void $ alertWhile sanityCheckAlert go
|
2012-10-29 06:21:04 +00:00
|
|
|
debug ["sanity check complete"]
|
|
|
|
where
|
|
|
|
go = do
|
2012-10-30 19:39:15 +00:00
|
|
|
modifyDaemonStatus_ $ \s -> s { sanityCheckRunning = True }
|
2012-10-29 06:21:04 +00:00
|
|
|
|
|
|
|
now <- liftIO $ getPOSIXTime -- before check started
|
|
|
|
r <- either showerr return =<< tryIO <~> check
|
2012-07-29 21:53:18 +00:00
|
|
|
|
2012-10-30 19:39:15 +00:00
|
|
|
modifyDaemonStatus_ $ \s -> s
|
2012-10-29 06:21:04 +00:00
|
|
|
{ sanityCheckRunning = False
|
|
|
|
, lastSanityCheck = Just now
|
|
|
|
}
|
2012-07-29 21:53:18 +00:00
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
return r
|
2012-07-20 23:29:59 +00:00
|
|
|
|
2012-10-29 06:21:04 +00:00
|
|
|
showerr e = do
|
|
|
|
liftAnnex $ warning $ show e
|
|
|
|
return False
|
2012-07-30 06:07:02 +00:00
|
|
|
|
2012-06-13 21:54:23 +00:00
|
|
|
{- Only run one check per day, from the time of the last check. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
waitForNextCheck :: Assistant ()
|
|
|
|
waitForNextCheck = do
|
2012-10-30 18:44:18 +00:00
|
|
|
v <- lastSanityCheck <$> getDaemonStatus
|
2012-10-29 06:21:04 +00:00
|
|
|
now <- liftIO getPOSIXTime
|
|
|
|
liftIO $ threadDelaySeconds $ Seconds $ calcdelay now v
|
|
|
|
where
|
|
|
|
calcdelay _ Nothing = oneDay
|
|
|
|
calcdelay now (Just lastcheck)
|
|
|
|
| lastcheck < now = max oneDay $
|
|
|
|
oneDay - truncate (now - lastcheck)
|
|
|
|
| otherwise = oneDay
|
2012-06-13 21:54:23 +00:00
|
|
|
|
|
|
|
oneDay :: Int
|
|
|
|
oneDay = 24 * 60 * 60
|
2012-06-13 23:25:47 +00:00
|
|
|
|
|
|
|
{- It's important to stay out of the Annex monad as much as possible while
|
|
|
|
- running potentially expensive parts of this check, since remaining in it
|
|
|
|
- will block the watcher. -}
|
2012-10-29 06:21:04 +00:00
|
|
|
check :: Assistant Bool
|
|
|
|
check = do
|
|
|
|
g <- liftAnnex gitRepo
|
2012-06-13 23:25:47 +00:00
|
|
|
-- Find old unstaged symlinks, and add them to git.
|
2012-10-29 06:21:04 +00:00
|
|
|
(unstaged, cleanup) <- liftIO $ Git.LsFiles.notInRepo False ["."] g
|
|
|
|
now <- liftIO $ getPOSIXTime
|
2012-06-13 23:25:47 +00:00
|
|
|
forM_ unstaged $ \file -> do
|
2012-10-29 06:21:04 +00:00
|
|
|
ms <- liftIO $ catchMaybeIO $ getSymbolicLinkStatus file
|
2012-06-13 23:25:47 +00:00
|
|
|
case ms of
|
|
|
|
Just s | toonew (statusChangeTime s) now -> noop
|
2012-10-29 06:21:04 +00:00
|
|
|
| isSymbolicLink s -> addsymlink file ms
|
2012-06-13 23:25:47 +00:00
|
|
|
_ -> noop
|
2012-10-29 06:21:04 +00:00
|
|
|
liftIO $ void cleanup
|
2012-07-30 06:07:02 +00:00
|
|
|
return True
|
2012-10-29 06:21:04 +00:00
|
|
|
where
|
|
|
|
toonew timestamp now = now < (realToFrac (timestamp + slop) :: POSIXTime)
|
|
|
|
slop = fromIntegral tenMinutes
|
|
|
|
insanity msg = do
|
|
|
|
liftAnnex $ warning msg
|
2012-10-30 19:39:15 +00:00
|
|
|
void $ addAlert $ sanityCheckFixAlert msg
|
2012-10-29 06:21:04 +00:00
|
|
|
addsymlink file s = do
|
2012-10-29 13:55:40 +00:00
|
|
|
Watcher.runHandler Watcher.onAddSymlink file s
|
2012-10-29 06:21:04 +00:00
|
|
|
insanity $ "found unstaged symlink: " ++ file
|