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

Extract UI related config params into a UIConfig type #76

Merged
merged 2 commits into from
Aug 11, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions examples/OddJobsCliExample.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ Ideally, this module should be compiled into a separate executable and should de
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}

module OddJobsCliExample where

import OddJobs.Job (Job(..), ConcurrencyControl(..), Config(..), throwParsePayload)
import OddJobs.ConfigBuilder (mkConfig, withConnectionPool, defaultTimedLogger, defaultLogStr, defaultJobType)
import OddJobs.Cli (defaultMain)
import OddJobs.Job (Job(..), ConcurrencyControl(..), Config(..), throwParsePayload, startJobRunner, LogLevel(..), LogEvent(..))
import OddJobs.ConfigBuilder (mkConfig, withConnectionPool, defaultTimedLogger, defaultLogStr, defaultJobType, mkUIConfig)
import OddJobs.Cli (runCli, defaultWebUI, CliType(..))

-- Note: It is not necessary to use fast-logger. You can use any logging library
-- that can give you a logging function in the IO monad.
import System.Log.FastLogger(withTimedFastLogger, LogType'(..), defaultBufSize)
import System.Log.FastLogger(withTimedFastLogger, LogType'(..), defaultBufSize, newTimedFastLogger)
import System.Log.FastLogger.Date (newTimeCache, simpleTimeFormat)

import Database.PostgreSQL.Simple as PGS
import Data.Pool
import Data.Text (Text)
import Data.Aeson as Aeson
import GHC.Generics
Expand Down Expand Up @@ -87,32 +89,23 @@ myJobRunner job = do

\begin{code}
main :: IO ()
main = do
defaultMain startJobMonitor
main = runCli CliBoth{..}
where
-- A callback-within-callback function. If the commands-line args contain a
-- `start` command, this function will be called. Once this function has
-- constructed the 'Config' (which requires setting up a logging function,
-- and a DB pool) it needs to execute the `callback` function that is passed
-- to it.
startJobMonitor callback =

-- a utility function provided by `OddJobs.ConfigBuilder` which ensures
-- that the DB pool is gracefully destroyed upon shutdown.
cliStartJobRunner cfgOverrideFn = do
withConnectionPool (Left "dbname=jobs_test user=jobs_test password=jobs_test host=localhost")$ \dbPool -> do

-- Boilerplate code to setup a TimedFastLogger (from the fast-logger library)
tcache <- newTimeCache simpleTimeFormat
withTimedFastLogger tcache (LogFileNoRotate "oddjobs.log" defaultBufSize) $ \logger -> do

-- Using the default string-based logging provided by
-- `OddJobs.ConfigBuilder`. If you want to actually use
-- structured-logging you'll need to define your own logging function.
let jobLogger = defaultTimedLogger logger (defaultLogStr defaultJobType)
cfg = mkConfig jobLogger "jobs" dbPool (MaxConcurrentJobs 50) myJobRunner Prelude.id
startJobRunner $
mkConfig jobLogger "jobs" dbPool (MaxConcurrentJobs 50) myJobRunner cfgOverrideFn

-- Finally, executing the callback function that was passed to me...
callback cfg
cliStartWebUI uiStartArgs cfgOverrideFn = do
withConnectionPool (Left "dbname=jobs_test user=jobs_test password=jobs_test host=localhost")$ \dbPool -> do
tcache <- newTimeCache simpleTimeFormat
withTimedFastLogger tcache (LogFileNoRotate "oddjobs-web.log" defaultBufSize) $ \logger -> do
let jobLogger = defaultTimedLogger logger (defaultLogStr defaultJobType)
defaultWebUI uiStartArgs $
mkUIConfig jobLogger "jobs" dbPool cfgOverrideFn
\end{code}

=== 6. Compile and start the Odd Jobs runner
Expand Down
10 changes: 5 additions & 5 deletions odd-jobs.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: 6677b61240c930d793f07c90cd9dc719586179d99bdc7dc7b3dbe9a32d9a2afa
-- hash: 81dabf5fa65d737ec98a4f07762968283cccc36292941ff7fff10a9a398e84b0

name: odd-jobs
version: 0.2.2
Expand Down Expand Up @@ -67,7 +67,7 @@ library
aeson
, base >=4.7 && <5
, bytestring
, direct-daemonize
, daemons
, directory
, either
, fast-logger
Expand Down Expand Up @@ -121,7 +121,7 @@ executable devel
aeson
, base >=4.7 && <5
, bytestring
, direct-daemonize
, daemons
, directory
, either
, fast-logger
Expand Down Expand Up @@ -168,7 +168,7 @@ executable odd-jobs-cli-example
aeson
, base >=4.7 && <5
, bytestring
, direct-daemonize
, daemons
, directory
, either
, fast-logger
Expand Down Expand Up @@ -226,7 +226,7 @@ test-suite jobrunner
, base >=4.7 && <5
, bytestring
, containers
, direct-daemonize
, daemons
, directory
, either
, fast-logger
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ dependencies:
- warp
- unordered-containers
- optparse-applicative
- direct-daemonize
- daemons
- filepath
- directory
- generic-deriving
Expand Down
Loading