{-# LANGUAGE DerivingVia #-}
module Faktory.JobOptions
( JobOptions(..)
, retry
, once
, queue
, jobtype
, at
, in_
, custom
, getAtFromSchedule
, namespaceQueue
) where
import Faktory.Prelude
import Data.Aeson
import Data.Semigroup (Last(..))
import Data.Semigroup.Generic
import Data.Time
import Faktory.Job.Custom
import Faktory.Settings (Namespace, Queue)
import qualified Faktory.Settings as Settings
import GHC.Generics
data JobOptions = JobOptions
{ JobOptions -> Maybe (Last String)
joJobtype :: Maybe (Last String)
, JobOptions -> Maybe (Last Int)
joRetry :: Maybe (Last Int)
, JobOptions -> Maybe (Last Queue)
joQueue :: Maybe (Last Queue)
, JobOptions -> Maybe (Last (Either UTCTime NominalDiffTime))
joSchedule :: Maybe (Last (Either UTCTime NominalDiffTime))
, JobOptions -> Maybe Custom
joCustom :: Maybe Custom
}
deriving stock (forall x. JobOptions -> Rep JobOptions x)
-> (forall x. Rep JobOptions x -> JobOptions) -> Generic JobOptions
forall x. Rep JobOptions x -> JobOptions
forall x. JobOptions -> Rep JobOptions x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep JobOptions x -> JobOptions
$cfrom :: forall x. JobOptions -> Rep JobOptions x
Generic
deriving (b -> JobOptions -> JobOptions
NonEmpty JobOptions -> JobOptions
JobOptions -> JobOptions -> JobOptions
(JobOptions -> JobOptions -> JobOptions)
-> (NonEmpty JobOptions -> JobOptions)
-> (forall b. Integral b => b -> JobOptions -> JobOptions)
-> Semigroup JobOptions
forall b. Integral b => b -> JobOptions -> JobOptions
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: b -> JobOptions -> JobOptions
$cstimes :: forall b. Integral b => b -> JobOptions -> JobOptions
sconcat :: NonEmpty JobOptions -> JobOptions
$csconcat :: NonEmpty JobOptions -> JobOptions
<> :: JobOptions -> JobOptions -> JobOptions
$c<> :: JobOptions -> JobOptions -> JobOptions
Semigroup, Semigroup JobOptions
JobOptions
Semigroup JobOptions
-> JobOptions
-> (JobOptions -> JobOptions -> JobOptions)
-> ([JobOptions] -> JobOptions)
-> Monoid JobOptions
[JobOptions] -> JobOptions
JobOptions -> JobOptions -> JobOptions
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [JobOptions] -> JobOptions
$cmconcat :: [JobOptions] -> JobOptions
mappend :: JobOptions -> JobOptions -> JobOptions
$cmappend :: JobOptions -> JobOptions -> JobOptions
mempty :: JobOptions
$cmempty :: JobOptions
$cp1Monoid :: Semigroup JobOptions
Monoid) via GenericSemigroupMonoid JobOptions
instance FromJSON JobOptions where
parseJSON :: Value -> Parser JobOptions
parseJSON = String
-> (Object -> Parser JobOptions) -> Value -> Parser JobOptions
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"JobOptions" ((Object -> Parser JobOptions) -> Value -> Parser JobOptions)
-> (Object -> Parser JobOptions) -> Value -> Parser JobOptions
forall a b. (a -> b) -> a -> b
$ \Object
o ->
Maybe (Last String)
-> Maybe (Last Int)
-> Maybe (Last Queue)
-> Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe Custom
-> JobOptions
JobOptions
(Maybe (Last String)
-> Maybe (Last Int)
-> Maybe (Last Queue)
-> Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe Custom
-> JobOptions)
-> Parser (Maybe (Last String))
-> Parser
(Maybe (Last Int)
-> Maybe (Last Queue)
-> Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe Custom
-> JobOptions)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> Parser (Maybe (Last String))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"jobtype"
Parser
(Maybe (Last Int)
-> Maybe (Last Queue)
-> Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe Custom
-> JobOptions)
-> Parser (Maybe (Last Int))
-> Parser
(Maybe (Last Queue)
-> Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe Custom
-> JobOptions)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Text -> Parser (Maybe (Last Int))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"retry"
Parser
(Maybe (Last Queue)
-> Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe Custom
-> JobOptions)
-> Parser (Maybe (Last Queue))
-> Parser
(Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe Custom -> JobOptions)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Text -> Parser (Maybe (Last Queue))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"queue"
Parser
(Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe Custom -> JobOptions)
-> Parser (Maybe (Last (Either UTCTime NominalDiffTime)))
-> Parser (Maybe Custom -> JobOptions)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((UTCTime -> Last (Either UTCTime NominalDiffTime))
-> Maybe UTCTime -> Maybe (Last (Either UTCTime NominalDiffTime))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either UTCTime NominalDiffTime
-> Last (Either UTCTime NominalDiffTime)
forall a. a -> Last a
Last (Either UTCTime NominalDiffTime
-> Last (Either UTCTime NominalDiffTime))
-> (UTCTime -> Either UTCTime NominalDiffTime)
-> UTCTime
-> Last (Either UTCTime NominalDiffTime)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> Either UTCTime NominalDiffTime
forall a b. a -> Either a b
Left) (Maybe UTCTime -> Maybe (Last (Either UTCTime NominalDiffTime)))
-> Parser (Maybe UTCTime)
-> Parser (Maybe (Last (Either UTCTime NominalDiffTime)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> Parser (Maybe UTCTime)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"at")
Parser (Maybe Custom -> JobOptions)
-> Parser (Maybe Custom) -> Parser JobOptions
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Text -> Parser (Maybe Custom)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"custom"
getAtFromSchedule :: JobOptions -> IO (Maybe UTCTime)
getAtFromSchedule :: JobOptions -> IO (Maybe UTCTime)
getAtFromSchedule JobOptions
options = Maybe (Either UTCTime NominalDiffTime)
-> (Either UTCTime NominalDiffTime -> IO UTCTime)
-> IO (Maybe UTCTime)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for (Last (Either UTCTime NominalDiffTime)
-> Either UTCTime NominalDiffTime
forall a. Last a -> a
getLast (Last (Either UTCTime NominalDiffTime)
-> Either UTCTime NominalDiffTime)
-> Maybe (Last (Either UTCTime NominalDiffTime))
-> Maybe (Either UTCTime NominalDiffTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JobOptions -> Maybe (Last (Either UTCTime NominalDiffTime))
joSchedule JobOptions
options) ((Either UTCTime NominalDiffTime -> IO UTCTime)
-> IO (Maybe UTCTime))
-> (Either UTCTime NominalDiffTime -> IO UTCTime)
-> IO (Maybe UTCTime)
forall a b. (a -> b) -> a -> b
$ \case
Left UTCTime
t -> UTCTime -> IO UTCTime
forall (f :: * -> *) a. Applicative f => a -> f a
pure UTCTime
t
Right NominalDiffTime
nd -> NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
nd (UTCTime -> UTCTime) -> IO UTCTime -> IO UTCTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO UTCTime
getCurrentTime
namespaceQueue :: Namespace -> JobOptions -> JobOptions
namespaceQueue :: Namespace -> JobOptions -> JobOptions
namespaceQueue Namespace
namespace JobOptions
options = case JobOptions -> Maybe (Last Queue)
joQueue JobOptions
options of
Maybe (Last Queue)
Nothing -> JobOptions
options
Just (Last Queue
q) -> JobOptions
options JobOptions -> JobOptions -> JobOptions
forall a. Semigroup a => a -> a -> a
<> Queue -> JobOptions
queue (Namespace -> Queue -> Queue
Settings.namespaceQueue Namespace
namespace Queue
q)
retry :: Int -> JobOptions
retry :: Int -> JobOptions
retry Int
n = JobOptions
forall a. Monoid a => a
mempty { joRetry :: Maybe (Last Int)
joRetry = Last Int -> Maybe (Last Int)
forall a. a -> Maybe a
Just (Last Int -> Maybe (Last Int)) -> Last Int -> Maybe (Last Int)
forall a b. (a -> b) -> a -> b
$ Int -> Last Int
forall a. a -> Last a
Last Int
n }
once :: JobOptions
once :: JobOptions
once = Int -> JobOptions
retry (-Int
1)
queue :: Queue -> JobOptions
queue :: Queue -> JobOptions
queue Queue
q = JobOptions
forall a. Monoid a => a
mempty { joQueue :: Maybe (Last Queue)
joQueue = Last Queue -> Maybe (Last Queue)
forall a. a -> Maybe a
Just (Last Queue -> Maybe (Last Queue))
-> Last Queue -> Maybe (Last Queue)
forall a b. (a -> b) -> a -> b
$ Queue -> Last Queue
forall a. a -> Last a
Last Queue
q }
jobtype :: String -> JobOptions
jobtype :: String -> JobOptions
jobtype String
jt = JobOptions
forall a. Monoid a => a
mempty { joJobtype :: Maybe (Last String)
joJobtype = Last String -> Maybe (Last String)
forall a. a -> Maybe a
Just (Last String -> Maybe (Last String))
-> Last String -> Maybe (Last String)
forall a b. (a -> b) -> a -> b
$ String -> Last String
forall a. a -> Last a
Last String
jt }
at :: UTCTime -> JobOptions
at :: UTCTime -> JobOptions
at UTCTime
t = JobOptions
forall a. Monoid a => a
mempty { joSchedule :: Maybe (Last (Either UTCTime NominalDiffTime))
joSchedule = Last (Either UTCTime NominalDiffTime)
-> Maybe (Last (Either UTCTime NominalDiffTime))
forall a. a -> Maybe a
Just (Last (Either UTCTime NominalDiffTime)
-> Maybe (Last (Either UTCTime NominalDiffTime)))
-> Last (Either UTCTime NominalDiffTime)
-> Maybe (Last (Either UTCTime NominalDiffTime))
forall a b. (a -> b) -> a -> b
$ Either UTCTime NominalDiffTime
-> Last (Either UTCTime NominalDiffTime)
forall a. a -> Last a
Last (Either UTCTime NominalDiffTime
-> Last (Either UTCTime NominalDiffTime))
-> Either UTCTime NominalDiffTime
-> Last (Either UTCTime NominalDiffTime)
forall a b. (a -> b) -> a -> b
$ UTCTime -> Either UTCTime NominalDiffTime
forall a b. a -> Either a b
Left UTCTime
t }
in_ :: NominalDiffTime -> JobOptions
in_ :: NominalDiffTime -> JobOptions
in_ NominalDiffTime
i = JobOptions
forall a. Monoid a => a
mempty { joSchedule :: Maybe (Last (Either UTCTime NominalDiffTime))
joSchedule = Last (Either UTCTime NominalDiffTime)
-> Maybe (Last (Either UTCTime NominalDiffTime))
forall a. a -> Maybe a
Just (Last (Either UTCTime NominalDiffTime)
-> Maybe (Last (Either UTCTime NominalDiffTime)))
-> Last (Either UTCTime NominalDiffTime)
-> Maybe (Last (Either UTCTime NominalDiffTime))
forall a b. (a -> b) -> a -> b
$ Either UTCTime NominalDiffTime
-> Last (Either UTCTime NominalDiffTime)
forall a. a -> Last a
Last (Either UTCTime NominalDiffTime
-> Last (Either UTCTime NominalDiffTime))
-> Either UTCTime NominalDiffTime
-> Last (Either UTCTime NominalDiffTime)
forall a b. (a -> b) -> a -> b
$ NominalDiffTime -> Either UTCTime NominalDiffTime
forall a b. b -> Either a b
Right NominalDiffTime
i }
custom :: ToJSON a => a -> JobOptions
custom :: a -> JobOptions
custom a
v = JobOptions
forall a. Monoid a => a
mempty { joCustom :: Maybe Custom
joCustom = Custom -> Maybe Custom
forall a. a -> Maybe a
Just (Custom -> Maybe Custom) -> Custom -> Maybe Custom
forall a b. (a -> b) -> a -> b
$ a -> Custom
forall a. ToJSON a => a -> Custom
toCustom a
v }