X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FMain.hs;h=9a9532124e53273ad0b2b475acd8df51151ed6cf;hb=0e37f70a58d512858b38e1458c6d83bc1727269c;hp=3ea260642286dc5cc44efa510152eb892c53e271;hpb=44e4325046b133ccda1f8548515641602d223ddb;p=dead%2Fhtsn-import.git diff --git a/src/Main.hs b/src/Main.hs index 3ea2606..9a95321 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,8 +1,11 @@ {-# LANGUAGE NoMonomorphismRestriction #-} +{-# LANGUAGE DoAndIfThenElse #-} module Main where import Control.Arrow ( (&&&), arr, returnA ) +import Control.Concurrent ( threadDelay ) +import Control.Exception ( SomeException, catch ) import Control.Monad ( when ) import Control.Monad.IO.Class ( MonadIO, liftIO ) import Database.Groundhog.Generic ( runDbConn ) @@ -13,6 +16,7 @@ import Database.Groundhog.Postgresql ( import Data.Monoid ( (<>) ) import Network.Services.TSN.Logging ( init_logging ) import System.Console.CmdArgs ( def ) +import System.Directory ( removeFile ) import System.Exit ( exitWith, ExitCode (ExitFailure) ) import System.IO.Error ( catchIOError ) import Text.XML.HXT.Core ( @@ -46,27 +50,31 @@ import Xml ( parse_opts ) -import_file :: Configuration -> FilePath -> IO () +import_file :: Configuration -> FilePath -> IO Bool import_file cfg path = do - results <- catchIOError - parse_and_import - (\e -> do - report_error (show e) - report_error $ "Failed to import file " ++ path ++ "." - -- Return a nonempty list so we don't claim incorrectly that - -- we couldn't parse the DTD. - return [ Nothing ] ) - + results <- parse_and_import `catch` exception_handler case results of -- If results' is empty, one of the arrows return "nothing." - [] -> report_error $ "Unable to determine DTD for file " ++ path ++ "." + [] -> do + report_error $ "Unable to determine DTD for file " ++ path ++ "." + return False (r:_) -> case r of - Nothing -> return () - Just cnt -> report_info $ "Successfully imported " ++ - (show cnt) ++ - " records from " ++ path ++ "." + Nothing -> return False + Just cnt -> do + report_info $ "Successfully imported " ++ + (show cnt) ++ + " records from " ++ path ++ "." + return True where + exception_handler :: SomeException -> IO [Maybe Int] + exception_handler e = do + report_error (show e) + report_error $ "Failed to import file " ++ path ++ "." + -- Return a nonempty list so we don't claim incorrectly that + -- we couldn't parse the DTD. + return [Nothing] + -- | An arrow that reads a document into an 'XmlTree'. readA :: IOStateArrow s a XmlTree readA = readDocument parse_opts path @@ -142,4 +150,33 @@ main = do -- We don't do this in parallel (for now?) to keep the error -- messages nice and linear. - mapM_ (import_file cfg) (OC.xml_files opt_config) + results <- mapM (import_file cfg) (OC.xml_files opt_config) + + -- Zip the results with the files list to find out which ones can be + -- deleted. + let result_pairs = zip (OC.xml_files opt_config) results + let victims = filter (\(_,result) -> result) result_pairs + mapM_ ((kill True) . fst) victims + + where + kill try_again path = do + removeFile path `catchIOError` exception_handler + report_info $ "Removed imported file " ++ path ++ "." + where + -- | A wrapper around threadDelay which takes seconds instead of + -- microseconds as its argument. + thread_sleep :: Int -> IO () + thread_sleep seconds = do + let microseconds = seconds * (10 ^ (6 :: Int)) + threadDelay microseconds + + exception_handler :: IOError -> IO () + exception_handler e = do + report_error (show e) + report_error $ "Failed to remove imported file " ++ path ++ "." + if try_again then do + report_info $ "Waiting 5 seconds to attempt removal again..." + thread_sleep 5 + kill False path + else + report_info $ "Giving up on " ++ path ++ "."