Skip to content

Commit 52018e3

Browse files
committed
Added threads option to executable
1 parent c48e237 commit 52018e3

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ It may someday evolve into a full-featured search tool (possibly as a web servic
2121
```
2222
Usage: stalk fetch (binary|json|pretty)
2323
- fetch all data from stalkernet and
24-
write it to stdout in the given format
24+
write it to stdout in the given format,
25+
with a maximum of <threads> outstanding
26+
requests at once.
2527
2628
stalk dump (binary|json) (binary|json|pretty)
2729
- read stalkernet date from stdin in the first
28-
format, and write it to stdout in the second
30+
format, and write it to stdout in the second.
2931
```
3032

33+
Be careful with `<threads>`.
34+
Too many will either make Mike Tie angry or cause a response timeout
35+
(which, at this point, crashes the program).
36+
3137
## Building
3238

3339
Run: `cabal install` in the root directory.

exe/Main.hs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ import Data.Either.Extra
88
import Control.Applicative
99
import Control.Concurrent.Async
1010
import Control.Concurrent.STM
11+
import Control.Exception
1112
import Data.Aeson
1213
import Data.Aeson.Encode
1314
import Data.ByteString.Builder
1415
import qualified Data.ByteString.Lazy as L
16+
import Data.List
1517
import Data.Serialize
1618
import Text.PrettyPrint.ANSI.Leijen (putDoc, pretty)
1719
import System.Environment
20+
import System.Exit
1821
import System.IO
1922

2023
main :: 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 $ "\nstalk crashed: " ++ displayException e
68+
exitFailure
69+
5970
-- | Parse a person parser from command line argument
6071
reader :: String -> Maybe (L.ByteString -> Maybe [Person])
6172
reader "binary" = Just (eitherToMaybe . Data.Serialize.decodeLazy)

0 commit comments

Comments
 (0)