Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customer management implementation for multithreading #2

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 deletions customer-management/app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Main where

import Control.Concurrent.Async (async, wait)
import Control.Monad (forM_)
import Customer (Customer (..))
import CustomerLogger (CustomerLogger (..))
import CustomerMasker (CustomerMasker (..))
import CustomerVisualizer
( CustomerVisualizable (..),
CustomerVisualizer (..),
)
import InternalCustomer (InternalCustomer (..))
import VipCustomer (VipCustomer (..))
Expand Down Expand Up @@ -39,10 +39,18 @@ main = do
department = "IT"
}
]
forM_ customers doActions

let wrappedCustomers = fmap addWrappers customers
threads <- mapM (async . doActions wrappedCustomers) [1 .. 50]
forM_ threads wait
where
doActions :: forall a. CustomerVisualizer a => a -> IO ()
doActions customer = do
let customerMasker = CustomerMasker customer
let customerLogger = CustomerLogger customerMasker
V.printInfo customerLogger
addWrappers :: CustomerVisualizable -> CustomerVisualizable
addWrappers customer =
CustomerVisualizable
( CustomerLogger (CustomerMasker customer)
)

doActions :: [CustomerVisualizable] -> Int -> IO ()
doActions customers nth = do
forM_ customers V.printInfo
putStrLn ("#" <> show nth)
11 changes: 7 additions & 4 deletions customer-management/customer-management.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 025fdee50f6d19a67ef556e4e5e4dadfad44b41c7fa7114b6e1c8d33ec39880a
-- hash: 889d9b59a608d5741991d10e9e45d31a8ffd40ef2c4032b77c7a8f4f781c8778

name: customer-management
version: 0.1.0.0
Expand Down Expand Up @@ -40,7 +40,8 @@ library
src
default-extensions: DuplicateRecordFields ExistentialQuantification
build-depends:
base >=4.7 && <5
async >=2.1 && <3
, base >=4.7 && <5
default-language: Haskell2010

executable customer-management-exe
Expand All @@ -52,7 +53,8 @@ executable customer-management-exe
default-extensions: DuplicateRecordFields ExistentialQuantification
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
async >=2.1 && <3
, base >=4.7 && <5
, customer-management
default-language: Haskell2010

Expand All @@ -66,6 +68,7 @@ test-suite customer-management-test
default-extensions: DuplicateRecordFields ExistentialQuantification
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
async >=2.1 && <3
, base >=4.7 && <5
, customer-management
default-language: Haskell2010
1 change: 1 addition & 0 deletions customer-management/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ default-extensions:

dependencies:
- base >= 4.7 && < 5
- async >= 2.1 && < 3

library:
source-dirs: src
Expand Down