-- | The worker pool's run state.
module Arbiter.Worker.WorkerState
  ( WorkerState (..)

    -- * Internal
  , isShuttingDown
  ) where

import UnliftIO.STM (STM, TVar, readTVar)

-- | A worker pool's effective state, read off the shutdown and pause flags on its
-- 'Arbiter.Worker.Config.WorkerConfig'. Shutdown wins, then pause, then running.
data WorkerState
  = -- | Claims and runs jobs.
    Running
  | -- | Claims no new jobs.
    Paused
  | -- | Claims no new jobs and stops.
    ShuttingDown
  deriving stock (WorkerState -> WorkerState -> Bool
(WorkerState -> WorkerState -> Bool)
-> (WorkerState -> WorkerState -> Bool) -> Eq WorkerState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WorkerState -> WorkerState -> Bool
== :: WorkerState -> WorkerState -> Bool
$c/= :: WorkerState -> WorkerState -> Bool
/= :: WorkerState -> WorkerState -> Bool
Eq, Int -> WorkerState -> ShowS
[WorkerState] -> ShowS
WorkerState -> String
(Int -> WorkerState -> ShowS)
-> (WorkerState -> String)
-> ([WorkerState] -> ShowS)
-> Show WorkerState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WorkerState -> ShowS
showsPrec :: Int -> WorkerState -> ShowS
$cshow :: WorkerState -> String
show :: WorkerState -> String
$cshowList :: [WorkerState] -> ShowS
showList :: [WorkerState] -> ShowS
Show)

-- | Whether the pool state is 'ShuttingDown'.
isShuttingDown :: TVar WorkerState -> STM Bool
isShuttingDown :: TVar WorkerState -> STM Bool
isShuttingDown TVar WorkerState
stateVar = (WorkerState -> WorkerState -> Bool
forall a. Eq a => a -> a -> Bool
== WorkerState
ShuttingDown) (WorkerState -> Bool) -> STM WorkerState -> STM Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar WorkerState -> STM WorkerState
forall a. TVar a -> STM a
readTVar TVar WorkerState
stateVar