Skip to content

Commit 1fab7e2

Browse files
committed
move exported types from C.D.P.P.Supervisor to .Types
This is in preparation for some changes to the handling of the StarterProcess variants of ToChildStart. See discussion on PR haskell-distributed#77.
1 parent 7e4060b commit 1fab7e2

File tree

3 files changed

+373
-307
lines changed

3 files changed

+373
-307
lines changed

distributed-process-platform.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ library
7070
Control.Distributed.Process.Platform.Service.Registry,
7171
Control.Distributed.Process.Platform.Service.SystemLog,
7272
Control.Distributed.Process.Platform.Supervisor,
73+
Control.Distributed.Process.Platform.Supervisor.Types,
7374
Control.Distributed.Process.Platform.Task.Queue.BlockingQueue,
7475
Control.Distributed.Process.Platform.Test,
7576
Control.Distributed.Process.Platform.Time,

src/Control/Distributed/Process/Platform/Supervisor.hs

+7-307
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ module Control.Distributed.Process.Platform.Supervisor
270270
) where
271271

272272
import Control.DeepSeq (NFData)
273+
274+
import Control.Distributed.Process.Platform.Supervisor.Types
273275
import Control.Distributed.Process hiding (call)
274276
import Control.Distributed.Process.Serializable()
275277
import Control.Distributed.Process.Platform.Internal.Primitives hiding (monitor)
@@ -333,7 +335,7 @@ import Control.Distributed.Process.Platform.Service.SystemLog
333335
)
334336
import qualified Control.Distributed.Process.Platform.Service.SystemLog as Log
335337
import Control.Distributed.Process.Platform.Time
336-
import Control.Exception (SomeException, Exception, throwIO)
338+
import Control.Exception (SomeException, throwIO)
337339

338340
import Control.Monad.Error
339341

@@ -380,278 +382,8 @@ import GHC.Generics
380382
-- Types --
381383
--------------------------------------------------------------------------------
382384

383-
-- external client/configuration API
384-
385-
newtype MaxRestarts = MaxR { maxNumberOfRestarts :: Int }
386-
deriving (Typeable, Generic, Show)
387-
instance Binary MaxRestarts where
388-
instance NFData MaxRestarts where
389-
390-
-- | Smart constructor for @MaxRestarts@. The maximum
391-
-- restart count must be a positive integer.
392-
maxRestarts :: Int -> MaxRestarts
393-
maxRestarts r | r >= 0 = MaxR r
394-
| otherwise = error "MaxR must be >= 0"
395-
396-
-- | A compulsary limit on the number of restarts that a supervisor will
397-
-- tolerate before it terminates all child processes and then itself.
398-
-- If > @MaxRestarts@ occur within the specified @TimeInterval@, termination
399-
-- will occur. This prevents the supervisor from entering an infinite loop of
400-
-- child process terminations and restarts.
401-
--
402-
data RestartLimit =
403-
RestartLimit
404-
{ maxR :: !MaxRestarts
405-
, maxT :: !TimeInterval
406-
}
407-
deriving (Typeable, Generic, Show)
408-
instance Binary RestartLimit where
409-
instance NFData RestartLimit where
410-
411-
limit :: MaxRestarts -> TimeInterval -> RestartLimit
412-
limit mr ti = RestartLimit mr ti
413-
414-
defaultLimits :: RestartLimit
415-
defaultLimits = limit (MaxR 1) (seconds 1)
416-
417-
data RestartOrder = LeftToRight | RightToLeft
418-
deriving (Typeable, Generic, Eq, Show)
419-
instance Binary RestartOrder where
420-
instance NFData RestartOrder where
421-
422-
-- TODO: rename these, somehow...
423-
data RestartMode =
424-
RestartEach { order :: !RestartOrder }
425-
{- ^ stop then start each child sequentially, i.e., @foldlM stopThenStart children@ -}
426-
| RestartInOrder { order :: !RestartOrder }
427-
{- ^ stop all children first, then restart them sequentially -}
428-
| RestartRevOrder { order :: !RestartOrder }
429-
{- ^ stop all children in the given order, but start them in reverse -}
430-
deriving (Typeable, Generic, Show, Eq)
431-
instance Binary RestartMode where
432-
instance NFData RestartMode where
433-
434-
data ShutdownMode = SequentialShutdown !RestartOrder
435-
| ParallelShutdown
436-
deriving (Typeable, Generic, Show, Eq)
437-
instance Binary ShutdownMode where
438-
instance NFData ShutdownMode where
439-
440-
-- | Strategy used by a supervisor to handle child restarts, whether due to
441-
-- unexpected child failure or explicit restart requests from a client.
442-
--
443-
-- Some terminology: We refer to child processes managed by the same supervisor
444-
-- as /siblings/. When restarting a child process, the 'RestartNone' policy
445-
-- indicates that sibling processes should be left alone, whilst the 'RestartAll'
446-
-- policy will cause /all/ children to be restarted (in the same order they were
447-
-- started).
448-
--
449-
-- The other two restart strategies refer to /prior/ and /subsequent/
450-
-- siblings, which describe's those children's configured position
451-
-- (i.e., insertion order). These latter modes allow one to control the order
452-
-- in which siblings are restarted, and to exclude some siblings from the restart
453-
-- without having to resort to grouping them using a child supervisor.
454-
--
455-
data RestartStrategy =
456-
RestartOne
457-
{ intensity :: !RestartLimit
458-
} -- ^ restart only the failed child process
459-
| RestartAll
460-
{ intensity :: !RestartLimit
461-
, mode :: !RestartMode
462-
} -- ^ also restart all siblings
463-
| RestartLeft
464-
{ intensity :: !RestartLimit
465-
, mode :: !RestartMode
466-
} -- ^ restart prior siblings (i.e., prior /start order/)
467-
| RestartRight
468-
{ intensity :: !RestartLimit
469-
, mode :: !RestartMode
470-
} -- ^ restart subsequent siblings (i.e., subsequent /start order/)
471-
deriving (Typeable, Generic, Show)
472-
instance Binary RestartStrategy where
473-
instance NFData RestartStrategy where
474-
475-
-- | Provides a default 'RestartStrategy' for @RestartOne@.
476-
-- > restartOne = RestartOne defaultLimits
477-
--
478-
restartOne :: RestartStrategy
479-
restartOne = RestartOne defaultLimits
480-
481-
-- | Provides a default 'RestartStrategy' for @RestartAll@.
482-
-- > restartOne = RestartAll defaultLimits (RestartEach LeftToRight)
483-
--
484-
restartAll :: RestartStrategy
485-
restartAll = RestartAll defaultLimits (RestartEach LeftToRight)
486-
487-
-- | Provides a default 'RestartStrategy' for @RestartLeft@.
488-
-- > restartOne = RestartLeft defaultLimits (RestartEach LeftToRight)
489-
--
490-
restartLeft :: RestartStrategy
491-
restartLeft = RestartLeft defaultLimits (RestartEach LeftToRight)
492-
493-
-- | Provides a default 'RestartStrategy' for @RestartRight@.
494-
-- > restartOne = RestartRight defaultLimits (RestartEach LeftToRight)
495-
--
496-
restartRight :: RestartStrategy
497-
restartRight = RestartRight defaultLimits (RestartEach LeftToRight)
498-
499-
-- | Identifies a child process by name.
500-
type ChildKey = String
501-
502-
-- | A reference to a (possibly running) child.
503-
data ChildRef =
504-
ChildRunning !ProcessId -- ^ a reference to the (currently running) child
505-
| ChildRunningExtra !ProcessId !Message -- ^ also a currently running child, with /extra/ child info
506-
| ChildRestarting !ProcessId -- ^ a reference to the /old/ (previous) child (now restarting)
507-
| ChildStopped -- ^ indicates the child is not currently running
508-
| ChildStartIgnored -- ^ a non-temporary child exited with 'ChildInitIgnore'
509-
deriving (Typeable, Generic, Show)
510-
instance Binary ChildRef where
511-
instance NFData ChildRef where
512-
513-
instance Eq ChildRef where
514-
ChildRunning p1 == ChildRunning p2 = p1 == p2
515-
ChildRunningExtra p1 _ == ChildRunningExtra p2 _ = p1 == p2
516-
ChildRestarting p1 == ChildRestarting p2 = p1 == p2
517-
ChildStopped == ChildStopped = True
518-
ChildStartIgnored == ChildStartIgnored = True
519-
_ == _ = False
520-
521-
isRunning :: ChildRef -> Bool
522-
isRunning (ChildRunning _) = True
523-
isRunning (ChildRunningExtra _ _) = True
524-
isRunning _ = False
525-
526-
isRestarting :: ChildRef -> Bool
527-
isRestarting (ChildRestarting _) = True
528-
isRestarting _ = False
529-
530-
instance Resolvable ChildRef where
531-
resolve (ChildRunning pid) = return $ Just pid
532-
resolve (ChildRunningExtra pid _) = return $ Just pid
533-
resolve _ = return Nothing
534-
535-
-- these look a bit odd, but we basically want to avoid resolving
536-
-- or sending to (ChildRestarting oldPid)
537-
instance Routable ChildRef where
538-
sendTo (ChildRunning addr) = sendTo addr
539-
sendTo _ = error "invalid address for child process"
540-
541-
unsafeSendTo (ChildRunning ch) = unsafeSendTo ch
542-
unsafeSendTo _ = error "invalid address for child process"
543-
544-
-- | Specifies whether the child is another supervisor, or a worker.
545-
data ChildType = Worker | Supervisor
546-
deriving (Typeable, Generic, Show, Eq)
547-
instance Binary ChildType where
548-
instance NFData ChildType where
549-
550-
-- | Describes when a terminated child process should be restarted.
551-
data RestartPolicy =
552-
Permanent -- ^ a permanent child will always be restarted
553-
| Temporary -- ^ a temporary child will /never/ be restarted
554-
| Transient -- ^ A transient child will be restarted only if it terminates abnormally
555-
| Intrinsic -- ^ as 'Transient', but if the child exits normally, the supervisor also exits normally
556-
deriving (Typeable, Generic, Eq, Show)
557-
instance Binary RestartPolicy where
558-
instance NFData RestartPolicy where
559-
560-
{-
561-
data ChildRestart =
562-
Restart RestartPolicy -- ^ restart according to the given policy
563-
| DelayedRestart RestartPolicy TimeInterval -- ^ perform a /delayed restart/
564-
deriving (Typeable, Generic, Eq, Show)
565-
instance Binary ChildRestart where
566-
-}
567-
568-
data ChildTerminationPolicy =
569-
TerminateTimeout !Delay
570-
| TerminateImmediately
571-
deriving (Typeable, Generic, Eq, Show)
572-
instance Binary ChildTerminationPolicy where
573-
instance NFData ChildTerminationPolicy where
574-
575-
data RegisteredName =
576-
LocalName !String
577-
| GlobalName !String
578-
| CustomRegister !(Closure (ProcessId -> Process ()))
579-
deriving (Typeable, Generic)
580-
instance Binary RegisteredName where
581-
instance NFData RegisteredName where
582-
583-
instance Show RegisteredName where
584-
show (CustomRegister _) = "Custom Register"
585-
show (LocalName n) = n
586-
show (GlobalName n) = "global::" ++ n
587-
588-
data ChildStart =
589-
RunClosure !(Closure (Process ()))
590-
| CreateHandle !(Closure (SupervisorPid -> Process (ProcessId, Message)))
591-
| StarterProcess !ProcessId
592-
deriving (Typeable, Generic, Show)
593-
instance Binary ChildStart where
594-
instance NFData ChildStart where
595-
596-
-- | Specification for a child process. The child must be uniquely identified
597-
-- by it's @childKey@ within the supervisor. The supervisor will start the child
598-
-- itself, therefore @childRun@ should contain the child process' implementation
599-
-- e.g., if the child is a long running server, this would be the server /loop/,
600-
-- as with e.g., @ManagedProces.start@.
601-
data ChildSpec = ChildSpec {
602-
childKey :: !ChildKey
603-
, childType :: !ChildType
604-
, childRestart :: !RestartPolicy
605-
, childStop :: !ChildTerminationPolicy
606-
, childStart :: !ChildStart
607-
, childRegName :: !(Maybe RegisteredName)
608-
} deriving (Typeable, Generic, Show)
609-
instance Binary ChildSpec where
610-
instance NFData ChildSpec where
611-
612-
data ChildInitFailure =
613-
ChildInitFailure !String
614-
| ChildInitIgnore
615-
deriving (Typeable, Generic, Show)
616-
instance Exception ChildInitFailure where
617-
618-
data SupervisorStats = SupervisorStats {
619-
_children :: Int
620-
, _supervisors :: Int
621-
, _workers :: Int
622-
, _running :: Int
623-
, _activeSupervisors :: Int
624-
, _activeWorkers :: Int
625-
-- TODO: usage/restart/freq stats
626-
, totalRestarts :: Int
627-
} deriving (Typeable, Generic, Show)
628-
instance Binary SupervisorStats where
629-
instance NFData SupervisorStats where
630-
631-
-- | Static labels (in the remote table) are strings.
632-
type StaticLabel = String
633-
634-
-- | Provides failure information when (re-)start failure is indicated.
635-
data StartFailure =
636-
StartFailureDuplicateChild !ChildRef -- ^ a child with this 'ChildKey' already exists
637-
| StartFailureAlreadyRunning !ChildRef -- ^ the child is already up and running
638-
| StartFailureBadClosure !StaticLabel -- ^ a closure cannot be resolved
639-
| StartFailureDied !DiedReason -- ^ a child died (almost) immediately on starting
640-
deriving (Typeable, Generic, Show, Eq)
641-
instance Binary StartFailure where
642-
instance NFData StartFailure where
643-
644-
-- | The result of a call to 'removeChild'.
645-
data DeleteChildResult =
646-
ChildDeleted -- ^ the child specification was successfully removed
647-
| ChildNotFound -- ^ the child specification was not found
648-
| ChildNotStopped !ChildRef -- ^ the child was not removed, as it was not stopped.
649-
deriving (Typeable, Generic, Show, Eq)
650-
instance Binary DeleteChildResult where
651-
instance NFData DeleteChildResult where
652-
653-
type Child = (ChildRef, ChildSpec)
654-
type SupervisorPid = ProcessId
385+
-- TODO: ToChildStart belongs with rest of types in
386+
-- Control.Distributed.Process.Platform.Supervisor.Types
655387

656388
-- | A type that can be converted to a 'ChildStart'.
657389
class ToChildStart a where
@@ -725,7 +457,8 @@ injectIt proc' supervisor sendPidPort = do
725457
expectTriple :: Process (ProcessId, ChildKey, SendPort ProcessId)
726458
expectTriple = expect
727459

728-
-- internal APIs
460+
-- internal APIs. The corresponding XxxResult types are in
461+
-- Control.Distributed.Process.Platform.Supervisor.Types
729462

730463
data DeleteChild = DeleteChild !ChildKey
731464
deriving (Typeable, Generic)
@@ -756,27 +489,11 @@ instance NFData AddChildReq where
756489

757490
data AddChildRes = Exists ChildRef | Added State
758491

759-
data AddChildResult =
760-
ChildAdded !ChildRef
761-
| ChildFailedToStart !StartFailure
762-
deriving (Typeable, Generic, Show, Eq)
763-
instance Binary AddChildResult where
764-
instance NFData AddChildResult where
765-
766492
data StartChildReq = StartChild !ChildKey
767493
deriving (Typeable, Generic)
768494
instance Binary StartChildReq where
769495
instance NFData StartChildReq where
770496

771-
data StartChildResult =
772-
ChildStartOk !ChildRef
773-
| ChildStartFailed !StartFailure
774-
| ChildStartUnknownId
775-
| ChildStartInitIgnored
776-
deriving (Typeable, Generic, Show, Eq)
777-
instance Binary StartChildResult where
778-
instance NFData StartChildResult where
779-
780497
data RestartChildReq = RestartChildReq !ChildKey
781498
deriving (Typeable, Generic, Show, Eq)
782499
instance Binary RestartChildReq where
@@ -788,28 +505,11 @@ data DelayedRestartReq = DelayedRestartReq !ChildKey !DiedReason
788505
instance Binary DelayedRestartReq where
789506
-}
790507

791-
data RestartChildResult =
792-
ChildRestartOk !ChildRef
793-
| ChildRestartFailed !StartFailure
794-
| ChildRestartUnknownId
795-
| ChildRestartIgnored
796-
deriving (Typeable, Generic, Show, Eq)
797-
798-
instance Binary RestartChildResult where
799-
instance NFData RestartChildResult where
800-
801508
data TerminateChildReq = TerminateChildReq !ChildKey
802509
deriving (Typeable, Generic, Show, Eq)
803510
instance Binary TerminateChildReq where
804511
instance NFData TerminateChildReq where
805512

806-
data TerminateChildResult =
807-
TerminateChildOk
808-
| TerminateChildUnknownId
809-
deriving (Typeable, Generic, Show, Eq)
810-
instance Binary TerminateChildResult where
811-
instance NFData TerminateChildResult where
812-
813513
data IgnoreChildReq = IgnoreChildReq !ProcessId
814514
deriving (Typeable, Generic)
815515
instance Binary IgnoreChildReq where

0 commit comments

Comments
 (0)