public ActionResult TryToAutoInstallModules()
        {
            var notification = new ModuleAutoInstallPushNotification(User.Identity.Name)
            {
                Title = "Modules installation",
                //set completed by default
                Finished = DateTime.UtcNow
            };


            if (!_settingsManager.GetValue("VirtoCommerce.ModulesAutoInstalled", false))
            {
                lock (_lockObject)
                {
                    if (!_settingsManager.GetValue("VirtoCommerce.ModulesAutoInstalled", false))
                    {
                        var moduleBundles = _extModuleOptions.AutoInstallModuleBundles;
                        if (!moduleBundles.IsNullOrEmpty())
                        {
                            _settingsManager.SetValue("VirtoCommerce.ModulesAutoInstalled", true);

                            EnsureModulesCatalogInitialized();

                            var modules             = new List <ManifestModuleInfo>();
                            var moduleVersionGroups = _moduleCatalog.Modules
                                                      .OfType <ManifestModuleInfo>()
                                                      .Where(x => x.Groups.Intersect(moduleBundles, StringComparer.OrdinalIgnoreCase).Any())
                                                      .GroupBy(x => x.Id);

                            //Need install only latest versions
                            foreach (var moduleVersionGroup in moduleVersionGroups)
                            {
                                var alreadyInstalledModule = _moduleCatalog.Modules.OfType <ManifestModuleInfo>().FirstOrDefault(x => x.IsInstalled && x.Id.EqualsInvariant(moduleVersionGroup.Key));
                                //skip already installed modules
                                if (alreadyInstalledModule == null)
                                {
                                    var latestVersion = moduleVersionGroup.OrderBy(x => x.Version).LastOrDefault();
                                    if (latestVersion != null)
                                    {
                                        modules.Add(latestVersion);
                                    }
                                }
                            }

                            var modulesWithDependencies = _moduleCatalog.CompleteListWithDependencies(modules)
                                                          .OfType <ManifestModuleInfo>()
                                                          .Where(x => !x.IsInstalled)
                                                          .Select(x => AbstractTypeFactory <ModuleDescriptor> .TryCreateInstance().FromModel(x))
                                                          .ToArray();

                            if (modulesWithDependencies.Any())
                            {
                                var options = new ModuleBackgroundJobOptions
                                {
                                    Action  = ModuleAction.Install,
                                    Modules = modulesWithDependencies
                                };
                                //reset finished date
                                notification.Finished = null;

                                BackgroundJob.Enqueue(() => ModuleBackgroundJob(options, notification));
                            }
                        }
                    }
                }
            }
            return(Ok(notification));
        }
        public ActionResult <ModuleAutoInstallPushNotification> TryToAutoInstallModules()
        {
            var notification = new ModuleAutoInstallPushNotification(User.Identity.Name)
            {
                Title = "Modules installation",
                //set completed by default
                Finished = DateTime.UtcNow
            };

            if (!_settingsManager.GetValue(PlatformConstants.Settings.Setup.ModulesAutoInstalled.Name, false))
            {
                lock (_lockObject)
                {
                    if (!_settingsManager.GetValue(PlatformConstants.Settings.Setup.ModulesAutoInstalled.Name, false))
                    {
                        var moduleBundles = _externalModuleCatalogOptions.AutoInstallModuleBundles;
                        if (!moduleBundles.IsNullOrEmpty())
                        {
                            _settingsManager.SetValue(PlatformConstants.Settings.Setup.ModulesAutoInstalled.Name, true);
                            _settingsManager.SetValue(PlatformConstants.Settings.Setup.ModulesAutoInstallState.Name, AutoInstallState.Processing);

                            EnsureModulesCatalogInitialized();

                            var modules             = new List <ManifestModuleInfo>();
                            var moduleVersionGroups = _externalModuleCatalog.Modules
                                                      .OfType <ManifestModuleInfo>()
                                                      .Where(x => x.Groups.Intersect(moduleBundles, StringComparer.OrdinalIgnoreCase).Any())
                                                      .GroupBy(x => x.Id);

                            //Need install only latest versions
                            foreach (var moduleVersionGroup in moduleVersionGroups)
                            {
                                var alreadyInstalledModule = _externalModuleCatalog.Modules.OfType <ManifestModuleInfo>().FirstOrDefault(x => x.IsInstalled && x.Id.EqualsInvariant(moduleVersionGroup.Key));
                                //skip already installed modules
                                if (alreadyInstalledModule == null)
                                {
                                    var latestVersion = moduleVersionGroup.OrderBy(x => x.Version).LastOrDefault();
                                    if (latestVersion != null)
                                    {
                                        modules.Add(latestVersion);
                                    }
                                }
                            }

                            var modulesWithDependencies = _externalModuleCatalog.CompleteListWithDependencies(modules)
                                                          .OfType <ManifestModuleInfo>()
                                                          .Where(x => !x.IsInstalled)
                                                          .Select(x => new ModuleDescriptor(x))
                                                          .ToArray();

                            if (modulesWithDependencies.Any())
                            {
                                var options = new ModuleBackgroundJobOptions
                                {
                                    Action  = ModuleAction.Install,
                                    Modules = modulesWithDependencies
                                };
                                //reset finished date
                                notification.Finished = null;

                                // can't use Hangfire.BackgroundJob.Enqueue(...), because Hangfire tables might be missing in new DB
                                new Thread(() =>
                                {
                                    Thread.CurrentThread.IsBackground = true;
                                    ModuleBackgroundJob(options, notification);
                                }).Start();
                            }
                        }
                    }
                }
            }
            return(Ok(notification));
        }