示例#1
0
    /// <summary>
    ///     This function copies the schemas and geography data off an existing Swarmops installation. Runs in its own thread.
    /// </summary>
    public static void InitDatabaseThread()
    {
        // Ignore the session object, that method of sharing data didn't work, but a static variable did.

        _initProgress = 1;
        _initMessage  = "Loading schema from Swarmops servers; creating tables and procs...";
        Thread.Sleep(100);

        try
        {
            // Get the schema and initialize the database structures. Requires ADMIN access to database.

            DatabaseMaintenance.FirstInitialization();

            _initProgress = 3;
            _initMessage  = "Applying all post-baseline database schema upgrades...";
            DatabaseMaintenance.UpgradeSchemata();
            Thread.Sleep(100);

            _initProgress = 5;
            _initMessage  = "Getting list of countries from Swarmops servers...";
            Thread.Sleep(100);

            // Create translation lists

            Dictionary <string, int> countryIdTranslation = new Dictionary <string, int>();

            // Initialize the root geography (which becomes #1 if everything works)

            int rootGeographyId = SwarmDb.GetDatabaseForWriting().CreateGeography("[LOC]World", 0);

            // Get the list of countries

            GetGeographyData geoDataFetcher = new GetGeographyData();

            Country[] countries = geoDataFetcher.GetCountries();

            _initProgress = 7;
            _initMessage  = "Creating all countries on local server...";
            Thread.Sleep(100);
            int count = 0;
            int total = countries.Length;

            // Create all countries in our own database

            foreach (Country country in countries)
            {
                countryIdTranslation[country.Code] = SwarmDb.GetDatabaseForWriting().CreateCountry(country.Name,
                                                                                                   country.Code,
                                                                                                   country.Culture,
                                                                                                   rootGeographyId, country.PostalCodeLength,
                                                                                                   string.Empty);

                count++;
                _initMessage = String.Format("Creating all countries on local server... ({0}%)", count * 100 / total);
            }

            _initProgress = 10;

            // Construct list of countries that have geographic data

            List <string> initializableCountries = new List <string>();

            foreach (Country country in countries)
            {
                if (country.GeographyId != 1)
                {
                    initializableCountries.Add(country.Code);
                }
            }

            float initStepPerCountry = 90f / initializableCountries.Count;
            int   countryCount       = 0;

            // For each country...

            foreach (string countryCode in initializableCountries)
            {
                // Get the geography layout

                _initMessage = "Initializing geography for country " + countryCode + "... ";
                Thread.Sleep(100);

                GeographyUpdate.PrimeCountry(countryCode);
                GuidCache.Set("DbInitProgress", string.Empty);

                countryCount++;

                _initProgress = 10 + (int)(countryCount * initStepPerCountry);
            }

            // Set Geography at baseline (TODO: Ask for what baseline we got)

            Persistence.Key["LastGeographyUpdateId"] = "0";
            Persistence.Key["LastGeographyUpdate"]   = "1900-01-01";

            // Set an installation ID

            Persistence.Key["SwarmopsInstallationId"] = Guid.NewGuid().ToString();

            _initMessage = "Initializing currencies...";

            // Create initial currencies (European et al)

            Currency.Create("EUR", "Euros", "€");
            Currency.Create("USD", "US Dollars", "$");
            Currency.Create("CAD", "Canadian Dollars", "CA$");
            Currency.Create("SEK", "Swedish Krona", string.Empty);
            Currency.Create("NOK", "Norwegian Krona", string.Empty);
            Currency.Create("DKK", "Danish Krona", string.Empty);
            Currency.Create("ISK", "Icelandic Krona", string.Empty);
            Currency.Create("CHF", "Swiss Franc", string.Empty);
            Currency.Create("GBP", "Pounds Sterling", "£");
            Currency.Create("BTC", "Bitcoin", "฿");

            // Fetch the first set of exchange rates, completing the currency collection

            ExchangeRateSnapshot.Create();

            // Create the sandbox

            Organization.Create(0, "Sandbox", "Sandbox", "Sandbox", "swarmops.com", "Ops",
                                rootGeographyId, true,
                                true, 0).EnableEconomy(Currency.FromCode("EUR"));

            _initProgress = 100;
            _initMessage  = "Complete.";
        }
        catch (Exception failedException)
        {
            // Use initMessage to push info about what went wrong to the user

            _initMessage = failedException.ToString();
        }

        Thread.Sleep(1000);  // give some time for static var to stick and web interface to react before killing thread
    }
示例#2
0
        /// <summary>
        ///     This function copies the schemas and geography data off an existing Swarmops installation. Runs in its own thread.
        /// </summary>
        public static void InitDatabaseThread()
        {
            // Ignore the session object, that method of sharing data didn't work, but a static variable did.

            _initProgress = 1;
            _initMessage  = "Loading schema from Swarmops servers; creating tables and procs...";
            Thread.Sleep(100);

            try
            {
                // Get the schema and initialize the database structures. Requires ADMIN access to database.

                DatabaseMaintenance.FirstInitialization();

                _initProgress = 3;
                _initMessage  = "Applying all post-baseline database schema upgrades...";
                DatabaseMaintenance.UpgradeSchemata();
                Thread.Sleep(100);

                // SECURITY: With schemata to hold them in place, initialize the encryption keys

                Authentication.InitializeSymmetricDatabaseKey();
                Authentication.InitializeSymmetricFileSystemKey();

                // Set Geography at baseline (TODO: Ask for what baseline we got)

                Persistence.Key["LastGeographyUpdateId"] = "0";
                Persistence.Key["LastGeographyUpdate"]   = Constants.DateTimeLow.ToString("yyyy-MM-dd");

                // Set an installation ID
                // Doubles as start signal to daemons (if installation ID exists, db is ready for processing)

                Persistence.Key["SwarmopsInstallationId"] = Guid.NewGuid().ToString();

                _initProgress = 4;
                _initMessage  = "Initializing currencies...";

                // Create initial currencies (European et al)

                Currency.CreateFiat("EUR", "Euros", "€");
                Currency.CreateFiat("USD", "US Dollars", "$");
                Currency.CreateFiat("CAD", "Canadian Dollars", "CA$");
                Currency.CreateFiat("SEK", "Swedish Krona", string.Empty);
                Currency.CreateFiat("NOK", "Norwegian Krona", string.Empty);
                Currency.CreateFiat("DKK", "Danish Krona", string.Empty);
                Currency.CreateFiat("ISK", "Icelandic Krona", string.Empty);
                Currency.CreateFiat("CHF", "Swiss Franc", string.Empty);
                Currency.CreateFiat("GBP", "Pounds Sterling", "£");
                Currency.CreateCrypto("BTC", "Bitcoin Core", "฿");
                Currency.CreateCrypto("BCH", "Bitcoin Cash", "฿");

                // Fetch the first set of exchange rates, completing the currency collection

                ExchangeRateSnapshot.Create();

                // Disable SSL required - the user must turn this on manually

                SystemSettings.RequireSsl = false;

                _initProgress = 5;
                _initMessage  = "Getting list of countries from Swarmops servers...";
                Thread.Sleep(100);

                // Create translation lists

                Dictionary <string, int> countryIdTranslation = new Dictionary <string, int>();

                // Initialize the root geography (which becomes #1 if everything works)

                int rootGeographyId = SwarmDb.GetDatabaseForWriting().CreateGeography("[LOC]World", 0);

                // Create the sandbox

                Organization sandbox = Organization.Create(0, "Sandbox", "Sandbox", "Sandbox", "swarmops.com", "Ops",
                                                           rootGeographyId, true,
                                                           true, 0);

                sandbox.EnableEconomy(Currency.FromCode("EUR"));

                Positions.CreateOrganizationDefaultPositions(sandbox);

                // Get the list of countries

                GetGeographyData geoDataFetcher = new GetGeographyData();

                Country[] countries = null;

                _initProgress = 7;
                _initMessage  = "Creating all countries on local server...";
                Thread.Sleep(100);
                int count          = 0;
                int countryRetries = 0;

                try
                {
                    countries = geoDataFetcher.GetCountries();
                }
                catch (Exception)
                {
                    // ignore for now, retrying below
                }

                while (++countryRetries < 10 && (countries == null || countries.Length < 20))
                {
                    _initMessage = "Network problem, retrying... ";
                    if (countryRetries > 1)
                    {
                        _initMessage += String.Format("({0})", countryRetries);
                    }
                    Thread.Sleep(500);

                    try
                    {
                        countries = geoDataFetcher.GetCountries();
                    }
                    catch (Exception)
                    {
                        if (countryRetries > 8)
                        {
                            throw;
                        }

                        // otherwise ignore for now
                    }
                }

                int total = countries.Length;



                // Create all countries in our own database

                foreach (Country country in countries)
                {
                    countryIdTranslation[country.Code] = SwarmDb.GetDatabaseForWriting().CreateCountry(country.Name,
                                                                                                       country.Code,
                                                                                                       country.Culture,
                                                                                                       rootGeographyId, country.PostalCodeLength,
                                                                                                       string.Empty);

                    count++;
                    _initMessage = String.Format("Creating all countries on local server... ({0}%)", count * 100 / total);
                }

                _initProgress = 10;

                // Construct list of countries that have geographic data

                List <string> initializableCountries = new List <string>();

                foreach (Country country in countries)
                {
                    if (country.GeographyId != 1)
                    {
                        initializableCountries.Add(country.Code);
                    }
                }

                float initStepPerCountry = 90f / initializableCountries.Count;
                int   countryCount       = 0;

                // For each country...

                foreach (string countryCode in initializableCountries)
                {
                    // Get the geography layout

                    _initMessage = "Initializing geography for country " + countryCode + "... ";
                    Thread.Sleep(100);

                    GeographyUpdate.PrimeCountry(countryCode);
                    GuidCache.Set("DbInitProgress", string.Empty);

                    countryCount++;

                    _initProgress = 10 + (int)(countryCount * initStepPerCountry);
                }

                _initProgress = 100;
                _initMessage  = "Complete.";
            }
            catch (Exception failedException)
            {
                // Use initMessage to push info about what went wrong to the user

                _initMessage = failedException.ToString();
            }

            Thread.Sleep(1000);
            // give some time for static var to stick and web interface to react before killing thread
        }