Ejemplo n.º 1
0
        /// <summary>
        /// INTERNAL API
        ///
        /// Primes the <see cref="CoordinatedShutdown"/> with the default phase for
        /// <see cref="ActorSystem.Terminate"/>
        /// </summary>
        /// <param name="system">The actor system for this extension.</param>
        /// <param name="conf">The HOCON configuration.</param>
        /// <param name="coord">The <see cref="CoordinatedShutdown"/> plugin instance.</param>
        internal static void InitPhaseActorSystemTerminate(ActorSystem system, Config conf, CoordinatedShutdown coord)
        {
            var terminateActorSystem = conf.GetBoolean("terminate-actor-system");
            var exitClr = conf.GetBoolean("exit-clr");

            if (terminateActorSystem || exitClr)
            {
                coord.AddTask(PhaseActorSystemTerminate, "terminate-system", () =>
                {
                    if (exitClr && terminateActorSystem)
                    {
                        // In case ActorSystem shutdown takes longer than the phase timeout,
                        // exit the JVM forcefully anyway.

                        // We must spawn a separate Task to not block current thread,
                        // since that would have blocked the shutdown of the ActorSystem.
                        var timeout = coord.Timeout(PhaseActorSystemTerminate);
                        return(Task.Run(() =>
                        {
                            if (!system.WhenTerminated.Wait(timeout) && !coord._runningClrHook)
                            {
                                Environment.Exit(0);
                            }
                            return Done.Instance;
                        }));
                    }

                    if (terminateActorSystem)
                    {
                        return(system.Terminate().ContinueWith(tr =>
                        {
                            if (exitClr && !coord._runningClrHook)
                            {
                                Environment.Exit(0);
                            }
                            return Done.Instance;
                        }));
                    }
                    else if (exitClr)
                    {
                        Environment.Exit(0);
                        return(TaskEx.Completed);
                    }
                    else
                    {
                        return(TaskEx.Completed);
                    }
                });
            }
        }