packages feed

faktory-1.1.0.0: library/Faktory/Ent/Batch/Status.hs

module Faktory.Ent.Batch.Status
  ( jobBatchId
  , BatchStatus(..)
  , batchStatus
  ) where

import Faktory.Prelude

import Control.Error.Util (hush)
import Data.Aeson
import Data.ByteString.Lazy as BSL
import Data.Text.Encoding (encodeUtf8)
import Data.Time (UTCTime)
import Faktory.Client
import Faktory.Ent.Batch
import Faktory.Job (Job, jobOptions)
import Faktory.Job.Custom
import Faktory.JobOptions (JobOptions(..))
import Faktory.Producer
import GHC.Generics

data BatchStatus = BatchStatus
  { bid :: BatchId
  , total :: Int
  , pending :: Int
  , failed :: Int
  , created_at :: UTCTime
  , description :: Text
  }
  deriving stock Generic
  deriving anyclass FromJSON

newtype ReadCustomBatchId = ReadCustomBatchId
  { _bid :: BatchId
  }
  deriving stock Generic
  deriving anyclass FromJSON

jobBatchId :: Job arg -> Maybe BatchId
jobBatchId job = do
  custom <- joCustom $ jobOptions job
  _bid <$> hush (fromCustom custom)

batchStatus :: Producer -> BatchId -> IO (Either String (Maybe BatchStatus))
batchStatus producer (BatchId bid) = commandJSON
  (producerClient producer)
  "BATCH STATUS"
  [BSL.fromStrict $ encodeUtf8 bid]