Пример #1
0
        /// <summary>
        /// Process system configuration initialisation
        /// This will parse the configuration text, you should call this
        /// function from within Application_BeginRequest of Global.asax.  It can run multiple times, once the config
        /// has loaded the system won't re-load the config until you call ProcessConfig.unload() followed by
        /// ProcessConfig.initialiseFileSystem(...), so it's safe to not surround it with ifs.
        ///
        /// NOTE: If a cluster is specified in the cluster.conf and its 'node-name' matches nodeName, then those settings
        /// will be used to connect to the cluster.  This allows for different staging environments to be setup.
        /// </summary>
        /// <param name="nodeName">If a cluster is specified in the cluster.conf and its 'node-name' matches nodeName, then
        /// those settings will be used to connect to the cluster.  This allows for different staging environments to be
        /// setup.</param>
        /// <param name="setup">A setup function to call on successful loading of the configuration files - this will
        /// happen once only.</param>
        /// <param name="strategyFuncs">Plugin extra strategy behaviours by passing in a list of FuncSpecs.</param>
        public static AppProfile initialise(string configText, Option <string> nodeName, Action <AppProfile> setup = null, IEnumerable <FuncSpec> strategyFuncs = null)
        {
            if (appProfile != null)
            {
                return(appProfile);
            }
            lock (sync)
            {
                if (appProfile != null)
                {
                    return(appProfile);
                }

                config = new ProcessSystemConfig(nodeName.IfNone(""), strategyFuncs);
                config.ParseConfigText(configText);

                appProfile = AppProfile.NonClustered;

                nodeName.Iter(node =>
                {
                    var provider    = config.GetClusterSetting("provider", "value", "redis");
                    var role        = config.GetClusterSetting("role", "value", name => clusterSettingMissing <string>(name));
                    var clusterConn = config.GetClusterSetting("connection", "value", "localhost");
                    var clusterDb   = config.GetClusterSetting("database", "value", "0");
                    var env         = config.GetClusterSetting <string>("env", "value");
                    var userEnv     = config.GetClusterSetting <string>("user-env", "value");

                    appProfile = new AppProfile(
                        node,
                        role,
                        clusterConn,
                        clusterDb,
                        env,
                        userEnv
                        );

                    Cluster.connect(provider, node, clusterConn, clusterDb, role);
                });

                config.PostConnect();

                setup(appProfile);
                return(appProfile);
            }
        }
Пример #2
0
        private static void StartFromConfig(ProcessSystemConfig config)
        {
            lock (sync)
            {
                InitLocalScheduler();

                config.Cluster.Match(
                    Some: _ =>
                {
                    // Extract cluster settings
                    var provider    = config.GetClusterSetting("provider", "value", "redis");
                    var role        = config.GetClusterSetting("role", "value", name => clusterSettingMissing <string>(name));
                    var clusterConn = config.GetClusterSetting("connection", "value", "localhost");
                    var clusterDb   = config.GetClusterSetting("database", "value", "0");
                    var env         = config.SystemName;
                    var userEnv     = config.GetClusterSetting <string>("user-env", "value");

                    var appProfile = new AppProfile(
                        config.NodeName,
                        role,
                        clusterConn,
                        clusterDb,
                        env,
                        userEnv
                        );

                    // Look for an existing actor-system with the same system name
                    var current = ActorContext.Systems.Filter(c => c.Value == env).HeadOrNone();

                    // Work out if something significant has changed that will cause us to restart
                    var restart = current.Map(ActorContext.System)
                                  .Map(c => c.AppProfile.NodeName != appProfile.NodeName ||
                                       c.AppProfile.Role != appProfile.Role ||
                                       c.AppProfile.ClusterConn != appProfile.ClusterConn ||
                                       c.AppProfile.ClusterDb != appProfile.ClusterDb);

                    // Either restart / update settings / or start new
                    restart.Match(
                        Some: r =>
                    {
                        if (r)
                        {
                            // Restart
                            try
                            {
                                ActorContext.StopSystem(env);
                            }
                            catch (Exception e)
                            {
                                logErr(e);
                            }
                            StartFromConfig(config);
                        }
                        else
                        {
                            // Update settings
                            ActorContext.System(env).UpdateSettings(config, appProfile);
                            var cluster = from systm in current.Map(ActorContext.System)
                                          from clstr in systm.Cluster
                                          select clstr;
                        }
                    },
                        None: () =>
                    {
                        // Start new
                        ICluster cluster = Cluster.connect(
                            provider,
                            config.NodeName,
                            clusterConn,
                            clusterDb,
                            role
                            );

                        ActorContext.StartSystem(env, Optional(cluster), appProfile, config);
                        config.PostConnect();
                    });
                },
                    None: () =>
                {
                    ActorContext.StartSystem(new SystemName(""), None, AppProfile.NonClustered, config);
                });
            }
        }