예제 #1
0
        private void RestoreFromDump(IDataReadOperator dataReader)
        {
            var keyBase  = KeyHelper.GetDatabaseSchema();
            var keys     = dataReader.GetEntries(keyBase).Select(r => Path.GetFileName(r)).ToList();
            var upgrades = new List <string>();

            if (!string.IsNullOrEmpty(UpgradesPath) && Directory.Exists(UpgradesPath))
            {
                upgrades = Directory.GetFiles(UpgradesPath).ToList();
            }

            var stepscount = keys.Count * 2 + upgrades.Count;

            SetStepsCount(ProcessStorage ? stepscount + 1 : stepscount);

            if (ProcessStorage)
            {
                var storageModules = StorageFactoryConfig.GetModuleList(ConfigPath).Where(IsStorageModuleAllowed);
                var tenants        = TenantManager.GetTenants(false);

                stepscount += storageModules.Count() * tenants.Count;

                SetStepsCount(stepscount + 1);

                DoDeleteStorage(storageModules, tenants);
            }
            else
            {
                SetStepsCount(stepscount);
            }

            for (var i = 0; i < keys.Count; i += TasksLimit)
            {
                var tasks = new List <Task>(TasksLimit * 2);

                for (var j = 0; j < TasksLimit && i + j < keys.Count; j++)
                {
                    var key1 = Path.Combine(KeyHelper.GetDatabaseSchema(), keys[i + j]);
                    tasks.Add(RestoreFromDumpFile(dataReader, key1).ContinueWith(r => RestoreFromDumpFile(dataReader, KeyHelper.GetDatabaseData(key1.Substring(keyBase.Length + 1)))));
                }

                Task.WaitAll(tasks.ToArray());
            }

            var comparer = new SqlComparer();

            foreach (var u in upgrades.OrderBy(Path.GetFileName, comparer))
            {
                RunMysqlFile(u, true);
                SetStepCompleted();
            }
        }
예제 #2
0
        private void RestoreFromDump(IDataReadOperator dataReader)
        {
            var keyBase  = KeyHelper.GetDatabaseSchema();
            var keys     = dataReader.GetEntries(keyBase).Select(r => Path.GetFileName(r)).ToList();
            var dbs      = dataReader.GetDirectories("").Where(r => Path.GetFileName(r).StartsWith("mailservice")).Select(r => Path.GetFileName(r)).ToList();
            var upgrades = new List <string>();

            var upgradesPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), UpgradesPath));

            if (!string.IsNullOrEmpty(upgradesPath) && Directory.Exists(upgradesPath))
            {
                upgrades = Directory.GetFiles(upgradesPath).ToList();
            }

            var stepscount = keys.Count * 2 + upgrades.Count;

            Dictionary <string, List <string> > databases = new Dictionary <string, List <string> >();

            foreach (var db in dbs)
            {
                var keys1 = dataReader.GetEntries(db + "/" + keyBase).Select(k => Path.GetFileName(k)).ToList();
                stepscount += keys1.Count() * 2;
                databases.Add(db, keys1);
            }

            if (ProcessStorage)
            {
                var storageModules = StorageFactory.GetModuleList(ConfigPath).Where(IsStorageModuleAllowed);
                var tenants        = CoreContext.TenantManager.GetTenants(false);

                stepscount += storageModules.Count() * tenants.Count;

                SetStepsCount(stepscount + 1);

                DoDeleteStorage(storageModules, tenants);
            }
            else
            {
                SetStepsCount(stepscount);
            }

            for (var i = 0; i < keys.Count; i += TasksLimit)
            {
                var tasks = new List <Task>(TasksLimit * 2);

                for (var j = 0; j < TasksLimit && i + j < keys.Count; j++)
                {
                    var key1 = Path.Combine(KeyHelper.GetDatabaseSchema(), keys[i + j]);
                    var key2 = Path.Combine(KeyHelper.GetDatabaseData(), keys[i + j]);
                    tasks.Add(RestoreFromDumpFile(dataReader, key1, key2));
                }

                Task.WaitAll(tasks.ToArray());
            }

            using (var dbManager = DbManager.FromHttpContext("default", 100000))
            {
                dbManager.ExecuteList("select id, connection_string from mail_server_server").ForEach(r =>
                {
                    RegisterDatabase((int)r[0], JsonConvert.DeserializeObject <Dictionary <string, object> >(Convert.ToString(r[1]))["DbConnection"].ToString());
                });
            }

            foreach (var database in databases)
            {
                for (var i = 0; i < database.Value.Count; i += TasksLimit)
                {
                    var tasks = new List <Task>(TasksLimit * 2);

                    for (var j = 0; j < TasksLimit && i + j < database.Value.Count; j++)
                    {
                        var key1 = Path.Combine(database.Key, KeyHelper.GetDatabaseSchema(), database.Value[i + j]);
                        var key2 = Path.Combine(database.Key, KeyHelper.GetDatabaseData(), database.Value[i + j]);
                        tasks.Add(RestoreFromDumpFile(dataReader, key1, key2, database.Key));
                    }

                    Task.WaitAll(tasks.ToArray());
                }
            }

            var comparer = new SqlComparer();

            foreach (var u in upgrades.OrderBy(Path.GetFileName, comparer))
            {
                using (var s = File.OpenRead(u))
                {
                    if (u.Contains(".upgrade."))
                    {
                        RunMysqlFile(s, null).Wait();
                    }
                    else if (u.Contains(".data") || u.Contains(".upgrade"))
                    {
                        RunMysqlProcedure(s).Wait();
                    }
                    else
                    {
                        RunMysqlFile(s, "default").Wait();
                    }
                }

                SetStepCompleted();
            }
        }