{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.Batch.Types.RetryStrategy
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.Batch.Types.RetryStrategy where

import Amazonka.Batch.Types.EvaluateOnExit
import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude

-- | The retry strategy that\'s associated with a job. For more information,
-- see
-- <https://docs.aws.amazon.com/batch/latest/userguide/job_retries.html Automated job retries>
-- in the /Batch User Guide/.
--
-- /See:/ 'newRetryStrategy' smart constructor.
data RetryStrategy = RetryStrategy'
  { -- | The number of times to move a job to the @RUNNABLE@ status. You can
    -- specify between 1 and 10 attempts. If the value of @attempts@ is greater
    -- than one, the job is retried on failure the same number of attempts as
    -- the value.
    RetryStrategy -> Maybe Int
attempts :: Prelude.Maybe Prelude.Int,
    -- | Array of up to 5 objects that specify the conditions where jobs are
    -- retried or failed. If this parameter is specified, then the @attempts@
    -- parameter must also be specified. If none of the listed conditions
    -- match, then the job is retried.
    RetryStrategy -> Maybe [EvaluateOnExit]
evaluateOnExit :: Prelude.Maybe [EvaluateOnExit]
  }
  deriving (RetryStrategy -> RetryStrategy -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RetryStrategy -> RetryStrategy -> Bool
$c/= :: RetryStrategy -> RetryStrategy -> Bool
== :: RetryStrategy -> RetryStrategy -> Bool
$c== :: RetryStrategy -> RetryStrategy -> Bool
Prelude.Eq, ReadPrec [RetryStrategy]
ReadPrec RetryStrategy
Int -> ReadS RetryStrategy
ReadS [RetryStrategy]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RetryStrategy]
$creadListPrec :: ReadPrec [RetryStrategy]
readPrec :: ReadPrec RetryStrategy
$creadPrec :: ReadPrec RetryStrategy
readList :: ReadS [RetryStrategy]
$creadList :: ReadS [RetryStrategy]
readsPrec :: Int -> ReadS RetryStrategy
$creadsPrec :: Int -> ReadS RetryStrategy
Prelude.Read, Int -> RetryStrategy -> ShowS
[RetryStrategy] -> ShowS
RetryStrategy -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RetryStrategy] -> ShowS
$cshowList :: [RetryStrategy] -> ShowS
show :: RetryStrategy -> String
$cshow :: RetryStrategy -> String
showsPrec :: Int -> RetryStrategy -> ShowS
$cshowsPrec :: Int -> RetryStrategy -> ShowS
Prelude.Show, forall x. Rep RetryStrategy x -> RetryStrategy
forall x. RetryStrategy -> Rep RetryStrategy x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RetryStrategy x -> RetryStrategy
$cfrom :: forall x. RetryStrategy -> Rep RetryStrategy x
Prelude.Generic)

-- |
-- Create a value of 'RetryStrategy' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'attempts', 'retryStrategy_attempts' - The number of times to move a job to the @RUNNABLE@ status. You can
-- specify between 1 and 10 attempts. If the value of @attempts@ is greater
-- than one, the job is retried on failure the same number of attempts as
-- the value.
--
-- 'evaluateOnExit', 'retryStrategy_evaluateOnExit' - Array of up to 5 objects that specify the conditions where jobs are
-- retried or failed. If this parameter is specified, then the @attempts@
-- parameter must also be specified. If none of the listed conditions
-- match, then the job is retried.
newRetryStrategy ::
  RetryStrategy
newRetryStrategy :: RetryStrategy
newRetryStrategy =
  RetryStrategy'
    { $sel:attempts:RetryStrategy' :: Maybe Int
attempts = forall a. Maybe a
Prelude.Nothing,
      $sel:evaluateOnExit:RetryStrategy' :: Maybe [EvaluateOnExit]
evaluateOnExit = forall a. Maybe a
Prelude.Nothing
    }

-- | The number of times to move a job to the @RUNNABLE@ status. You can
-- specify between 1 and 10 attempts. If the value of @attempts@ is greater
-- than one, the job is retried on failure the same number of attempts as
-- the value.
retryStrategy_attempts :: Lens.Lens' RetryStrategy (Prelude.Maybe Prelude.Int)
retryStrategy_attempts :: Lens' RetryStrategy (Maybe Int)
retryStrategy_attempts = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RetryStrategy' {Maybe Int
attempts :: Maybe Int
$sel:attempts:RetryStrategy' :: RetryStrategy -> Maybe Int
attempts} -> Maybe Int
attempts) (\s :: RetryStrategy
s@RetryStrategy' {} Maybe Int
a -> RetryStrategy
s {$sel:attempts:RetryStrategy' :: Maybe Int
attempts = Maybe Int
a} :: RetryStrategy)

-- | Array of up to 5 objects that specify the conditions where jobs are
-- retried or failed. If this parameter is specified, then the @attempts@
-- parameter must also be specified. If none of the listed conditions
-- match, then the job is retried.
retryStrategy_evaluateOnExit :: Lens.Lens' RetryStrategy (Prelude.Maybe [EvaluateOnExit])
retryStrategy_evaluateOnExit :: Lens' RetryStrategy (Maybe [EvaluateOnExit])
retryStrategy_evaluateOnExit = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RetryStrategy' {Maybe [EvaluateOnExit]
evaluateOnExit :: Maybe [EvaluateOnExit]
$sel:evaluateOnExit:RetryStrategy' :: RetryStrategy -> Maybe [EvaluateOnExit]
evaluateOnExit} -> Maybe [EvaluateOnExit]
evaluateOnExit) (\s :: RetryStrategy
s@RetryStrategy' {} Maybe [EvaluateOnExit]
a -> RetryStrategy
s {$sel:evaluateOnExit:RetryStrategy' :: Maybe [EvaluateOnExit]
evaluateOnExit = Maybe [EvaluateOnExit]
a} :: RetryStrategy) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Data.FromJSON RetryStrategy where
  parseJSON :: Value -> Parser RetryStrategy
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"RetryStrategy"
      ( \Object
x ->
          Maybe Int -> Maybe [EvaluateOnExit] -> RetryStrategy
RetryStrategy'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"attempts")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> ( Object
x
                            forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"evaluateOnExit"
                            forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty
                        )
      )

instance Prelude.Hashable RetryStrategy where
  hashWithSalt :: Int -> RetryStrategy -> Int
hashWithSalt Int
_salt RetryStrategy' {Maybe Int
Maybe [EvaluateOnExit]
evaluateOnExit :: Maybe [EvaluateOnExit]
attempts :: Maybe Int
$sel:evaluateOnExit:RetryStrategy' :: RetryStrategy -> Maybe [EvaluateOnExit]
$sel:attempts:RetryStrategy' :: RetryStrategy -> Maybe Int
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Int
attempts
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [EvaluateOnExit]
evaluateOnExit

instance Prelude.NFData RetryStrategy where
  rnf :: RetryStrategy -> ()
rnf RetryStrategy' {Maybe Int
Maybe [EvaluateOnExit]
evaluateOnExit :: Maybe [EvaluateOnExit]
attempts :: Maybe Int
$sel:evaluateOnExit:RetryStrategy' :: RetryStrategy -> Maybe [EvaluateOnExit]
$sel:attempts:RetryStrategy' :: RetryStrategy -> Maybe Int
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Int
attempts
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe [EvaluateOnExit]
evaluateOnExit

instance Data.ToJSON RetryStrategy where
  toJSON :: RetryStrategy -> Value
toJSON RetryStrategy' {Maybe Int
Maybe [EvaluateOnExit]
evaluateOnExit :: Maybe [EvaluateOnExit]
attempts :: Maybe Int
$sel:evaluateOnExit:RetryStrategy' :: RetryStrategy -> Maybe [EvaluateOnExit]
$sel:attempts:RetryStrategy' :: RetryStrategy -> Maybe Int
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"attempts" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Int
attempts,
            (Key
"evaluateOnExit" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [EvaluateOnExit]
evaluateOnExit
          ]
      )