@@ -8,20 +8,23 @@ import Data.Either.Extra
88import Control.Applicative
99import Control.Concurrent.Async
1010import Control.Concurrent.STM
11+ import Control.Exception
1112import Data.Aeson
1213import Data.Aeson.Encode
1314import Data.ByteString.Builder
1415import qualified Data.ByteString.Lazy as L
16+ import Data.List
1517import Data.Serialize
1618import Text.PrettyPrint.ANSI.Leijen (putDoc , pretty )
1719import System.Environment
20+ import System.Exit
1821import System.IO
1922
2023main :: IO ()
21- main = do
24+ main = handle handler $ do
2225 args <- getArgs
2326 case args of
24- [" fetch" , iw] -> maybeDo (writer iw) $ \ w -> do
27+ [" fetch" , ithreads, iw] -> maybeDo (liftA2 (,) (maybeRead ithreads) ( writer iw)) $ \ (threads, w) -> do
2528 -- TChan for counting pages, TVar for storing people
2629 (ticks, ppl) <- atomically $ (,) <$> newTChan <*> newTVar ([] :: [Person ])
2730 let -- Loop to update progress message in stderr
@@ -30,7 +33,7 @@ main = do
3033 atomically $ readTChan ticks
3134 prog (n + 1 )
3235 -- Actual fetching
33- fetch = fetchPeople 10 $ \ ps' -> atomically $ do
36+ fetch = fetchPeople threads $ \ ps' -> atomically $ do
3437 writeTChan ticks ()
3538 ps <- readTVar ppl
3639 writeTVar ppl (ps' ++ ps)
@@ -45,17 +48,25 @@ main = do
4548 _ -> oops
4649 where
4750 maybeDo = flip (maybe oops)
51+ maybeRead = fmap (fst . fst ) . uncons . reads
4852 renderProgress n = hPutStr stderr $ " \r\ESC [K" ++ show n ++ " /676 pages scraped"
4953 oops = hPutStr stderr (unlines usage)
50- usage = [ " Usage: stalk fetch (binary|json|pretty)"
54+ usage = [ " Usage: stalk fetch <threads> (binary|json|pretty)"
5155 , " - fetch all data from stalkernet and"
52- , " write it to stdout in the given format"
56+ , " write it to stdout in the given format,"
57+ , " with a maximum of <threads> outstanding"
58+ , " requests at once."
5359 , " "
5460 , " stalk dump (binary|json) (binary|json|pretty)"
5561 , " - read stalkernet date from stdin in the first"
56- , " format, and write it to stdout in the second"
62+ , " format, and write it to stdout in the second. "
5763 ]
5864
65+ handler :: SomeException -> IO ()
66+ handler e = do
67+ hPutStrLn stderr $ " \n stalk crashed: " ++ displayException e
68+ exitFailure
69+
5970-- | Parse a person parser from command line argument
6071reader :: String -> Maybe (L. ByteString -> Maybe [Person ])
6172reader " binary" = Just (eitherToMaybe . Data.Serialize. decodeLazy)
0 commit comments