aboutsummaryrefslogtreecommitdiff
path: root/Assistant/Common.hs
blob: d6df77f69ad133c654b1e91c0e0fd2314085a18b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{- Common infrastructure for the git-annex assistant threads.
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Assistant.Common (
	module X,
	ThreadName,
	NamedThread(..),
	runNamedThread,
	debug
) where

import Common.Annex as X
import Assistant.DaemonStatus
import Assistant.Alert

import System.Log.Logger
import qualified Control.Exception as E

type ThreadName = String
data NamedThread = NamedThread ThreadName (IO ())

debug :: ThreadName -> [String] -> IO ()
debug threadname ws = debugM threadname $ unwords $ (threadname ++ ":") : ws

runNamedThread :: DaemonStatusHandle -> NamedThread -> IO ()
runNamedThread dstatus (NamedThread name a) = go
	where
		go = do
			r <- E.try a :: IO (Either E.SomeException ())
			case r of
				Right _ -> noop
				Left e -> do
					let msg = unwords
						[ name
						, "crashed:"
						, show e
						]
					hPutStrLn stderr msg
					-- TODO click to restart
					void $ addAlert dstatus $
						warningAlert name msg