Capstone C: the job model

Capstone C: a concurrent job runner

The third capstone is a pool of workers that take jobs from an STM queue, compute a result and report it, with a clean shutdown, a log and a benchmark. It is built over eight lessons from the concurrency part of the course. The first lesson is the data: a job has an id, a name and a cost, and a result has the id and the value.

data Job = Job { jobId :: Int , jobName :: String , jobCost :: Int } deriving (Show)

The program

module Main (main) where import Data.List (sortOn) data Job = Job { jobId :: Int , jobName :: String , jobCost :: Int } deriving (Show) data Result = Result { resultId :: Int , resultName :: String , resultValue :: Int } deriving (Show) runJob :: Job -> Result runJob job = Result (jobId job) (jobName job) (jobCost job * 2) jobs :: [Job] jobs = [Job 1 "alpha" 5, Job 2 "beta" 7, Job 3 "gamma" 3] main :: IO () main = do mapM_ print jobs mapM_ print (sortOn resultId (map runJob jobs))

ghc -Wall -O1 -o jobs jobs.hs
./jobs

Job {jobId = 1, jobName = "alpha", jobCost = 5}
Job {jobId = 2, jobName = "beta", jobCost = 7}
Job {jobId = 3, jobName = "gamma", jobCost = 3}
Result {resultId = 1, resultName = "alpha", resultValue = 10}
Result {resultId = 2, resultName = "beta", resultValue = 14}
Result {resultId = 3, resultName = "gamma", resultValue = 6}

The model

The next lesson puts the jobs in a bounded STM queue.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why does a job carry an id?

    Answer

    The results come back in completion order and the id restores the job order

    A pool does not promise an order, so the id is what a reader sorts by.

  2. What does runJob do?

    Answer

    Turns a job into a result with a pure function

    The work is pure and the pool only schedules it.

  3. What does the derived Show print?

    Answer

    The constructor and the named fields

    The record syntax of Show names every field.

  4. What does this program print? module Main (main) where main :: IO () main = print (2 * 5 :: Int)

    Answer

    10

    Two times five is ten.

  5. Why is the cost a number and not a function?

    Answer

    The work is a pure function of the cost and the job stays a value

    A number keeps the job inspectable and the result checkable.

  6. What does this program print? module Main (main) where main :: IO () main = print (length [1, 2, 3 :: Int])

    Answer

    3

    The list has three elements.

  7. What are the two records of the lesson?

    Answer

    A job and a result

    The job is the input and the result the output of a run.

  8. What does this program print? module Main (main) where main :: IO () main = print (3 * 3 :: Int)

    Answer

    9

    Three times three is nine.

  9. Which result does a real pool print first?

    Answer

    The one that finished first, which may not be job 1

    Completion order is not job order, which is why the id exists.

  10. What does this program print? module Main (main) where main :: IO () main = print (sum [1 .. 3 :: Int])

    Answer

    6

    The sum of 1, 2 and 3 is 6.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev