[tasks] implement exponential backoff

This is to avoid bombarding users with task errors.
parent 83bd57b5
...@@ -6,6 +6,7 @@ module Gargantext.Context.Progress ...@@ -6,6 +6,7 @@ module Gargantext.Context.Progress
import Gargantext.Prelude import Gargantext.Prelude
import Data.Either (Either(..))
import Data.Maybe (Maybe(..)) import Data.Maybe (Maybe(..))
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Effect (Effect) import Effect (Effect)
...@@ -35,15 +36,17 @@ here :: R2.Here ...@@ -35,15 +36,17 @@ here :: R2.Here
here = R2.here "Gargantext.Context.Progress" here = R2.here "Gargantext.Context.Progress"
asyncProgress :: R2.Component AsyncProps asyncProgress :: R2.Component AsyncProps
asyncProgress = R2.component component asyncProgress = R2.component asyncProgressCpt
component :: R.Component AsyncProps asyncProgressCpt :: R.Component AsyncProps
component = R.hooksComponent "asyncProgressContext" cpt where asyncProgressCpt = R.hooksComponent "asyncProgress" cpt where
cpt props@{ errors cpt props@{ errors
, onFinish , onFinish
} children = do } children = do
-- States -- States
progress /\ progressBox <- R2.useBox' 0.0 progress /\ progressBox <- R2.useBox' 0.0
intervalIdRef <- R.useRef (Nothing :: Maybe IntervalId) intervalIdRef <- R.useRef (Nothing :: Maybe IntervalId)
-- exponential backoff is used when errors are reported
interval <- T.useBox 1000
-- Methods -- Methods
let let
...@@ -52,6 +55,13 @@ component = R.hooksComponent "asyncProgressContext" cpt where ...@@ -52,6 +55,13 @@ component = R.hooksComponent "asyncProgressContext" cpt where
let rdata = (RX.pick props :: Record QueryProgressData) let rdata = (RX.pick props :: Record QueryProgressData)
eAsyncProgress <- queryProgress rdata eAsyncProgress <- queryProgress rdata
-- exponential backoff in case of errors
liftEffect $ do
case eAsyncProgress of
Left _ -> T.modify_ (_ * 2) interval
Right _ -> T.write_ 1000 interval
interval' <- T.read interval
resetInterval intervalIdRef (Just interval') exec
handleRESTError here errors eAsyncProgress onProgress handleRESTError here errors eAsyncProgress onProgress
onProgress :: AsyncProgress -> Aff Unit onProgress :: AsyncProgress -> Aff Unit
...@@ -64,9 +74,10 @@ component = R.hooksComponent "asyncProgressContext" cpt where ...@@ -64,9 +74,10 @@ component = R.hooksComponent "asyncProgressContext" cpt where
(status == GT.IsKilled) || (status == GT.IsKilled) ||
(status == GT.IsFailure) (status == GT.IsFailure)
then do then do
case R.readRef intervalIdRef of resetInterval intervalIdRef Nothing exec
Nothing -> R.nothing -- case R.readRef intervalIdRef of
Just iid -> clearInterval iid -- Nothing -> R.nothing
-- Just iid -> clearInterval iid
handleErrorInAsyncProgress errors value handleErrorInAsyncProgress errors value
onFinish unit onFinish unit
else else
...@@ -74,8 +85,9 @@ component = R.hooksComponent "asyncProgressContext" cpt where ...@@ -74,8 +85,9 @@ component = R.hooksComponent "asyncProgressContext" cpt where
-- Hooks -- Hooks
useFirstEffect' do useFirstEffect' do
intervalId <- setInterval 1000 $ exec unit resetInterval intervalIdRef (Just 1000) exec
R.setRef intervalIdRef $ Just intervalId -- intervalId <- setInterval interval' $ exec unit
-- R.setRef intervalIdRef $ Just intervalId
-- Render -- Render
pure $ pure $
...@@ -83,5 +95,21 @@ component = R.hooksComponent "asyncProgressContext" cpt where ...@@ -83,5 +95,21 @@ component = R.hooksComponent "asyncProgressContext" cpt where
R.provideContext asyncContext (progress) R.provideContext asyncContext (progress)
children children
resetInterval :: R.Ref (Maybe IntervalId) -> Maybe Int -> (Unit -> Effect Unit) -> Effect Unit
resetInterval ref mInt exec = do
case R.readRef ref /\ mInt of
Nothing /\ Nothing ->
pure unit
Nothing /\ Just interval' -> do
intervalId <- setInterval interval' $ exec unit
R.setRef ref $ Just intervalId
Just iid /\ Nothing -> do
clearInterval iid
R.setRef ref Nothing
Just iid /\ Just interval' -> do
clearInterval iid
intervalId <- setInterval interval' $ exec unit
R.setRef ref $ Just intervalId
asyncContext :: R.Context (Number) asyncContext :: R.Context (Number)
asyncContext = R.createContext 0.0 asyncContext = R.createContext 0.0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment