private ModulePushNotification ScheduleJob(ModuleBackgroundJobOptions options)
        {
            var notification = new ModulePushNotification(_userNameResolver.GetCurrentUserName());

            switch (options.Action)
            {
            case ModuleAction.Install:
                notification.Title = "Install Module";
                notification.ProgressLog.Add(new ProgressMessage {
                    Level = ProgressMessageLevel.Info, Message = "Starting installation..."
                });
                break;

            case ModuleAction.Uninstall:
                notification.Title = "Uninstall Module";
                notification.ProgressLog.Add(new ProgressMessage {
                    Level = ProgressMessageLevel.Info, Message = "Starting uninstall..."
                });
                break;
            }

            _pushNotifier.Send(notification);

            BackgroundJob.Enqueue(() => ModuleBackgroundJob(options, notification));

            return(notification);
        }
        public ActionResult UninstallModule([FromBody] ModuleDescriptor[] modules)
        {
            EnsureModulesCatalogInitialized();

            var options = new ModuleBackgroundJobOptions
            {
                Action  = ModuleAction.Uninstall,
                Modules = modules
            };
            var result = ScheduleJob(options);

            return(Ok(result));
        }
        public ActionResult <ModulePushNotification> InstallModules(ModuleDescriptor[] modules)
        {
            EnsureModulesCatalogInitialized();

            var options = new ModuleBackgroundJobOptions
            {
                Action  = ModuleAction.Install,
                Modules = modules
            };
            var result = ScheduleJob(options);

            return(Ok(result));
        }
예제 #4
0
        public void ModuleBackgroundJob(ModuleBackgroundJobOptions options, ModulePushNotification notification)
        {
            try
            {
                notification.Started = DateTime.UtcNow;
                var moduleInfos = _externalModuleCatalog.Modules.OfType <ManifestModuleInfo>()
                                  .Where(x => options.Modules.Any(y => y.Identity.Equals(x.Identity)))
                                  .ToArray();
                var reportProgress = new Progress <ProgressMessage>(m =>
                {
                    lock (_lockObject)
                    {
                        notification.Description = m.Message;
                        notification.ProgressLog.Add(m);
                        _pushNotifier.Send(notification);
                    }
                });

                switch (options.Action)
                {
                case ModuleAction.Install:
                    _moduleInstaller.Install(moduleInfos, reportProgress);
                    break;

                case ModuleAction.Uninstall:
                    _moduleInstaller.Uninstall(moduleInfos, reportProgress);
                    break;
                }
            }
            catch (Exception ex)
            {
                notification.ProgressLog.Add(new ProgressMessage
                {
                    Level   = ProgressMessageLevel.Error,
                    Message = ex.ToString(),
                });
            }
            finally
            {
                _settingsManager.SetValue(PlatformConstants.Settings.Setup.ModulesAutoInstallState.Name, AutoInstallState.Completed);

                notification.Finished    = DateTime.UtcNow;
                notification.Description = "Installation finished.";
                notification.ProgressLog.Add(new ProgressMessage
                {
                    Level   = ProgressMessageLevel.Info,
                    Message = notification.Description,
                });
                _pushNotifier.Send(notification);
            }
        }
        public void ModuleBackgroundJob(ModuleBackgroundJobOptions options, ModulePushNotification notification)
        {
            try
            {
                notification.Started = DateTime.UtcNow;
                var moduleInfos = _moduleCatalog.Modules.OfType <ManifestModuleInfo>()
                                  .Where(x => options.Modules.Any(y => y.Identity.Equals(x.Identity)))
                                  .ToArray();
                var reportProgress = new Progress <ProgressMessage>(m =>
                {
                    lock (_lockObject)
                    {
                        notification.ProgressLog.Add(m);
                        _pushNotifier.Send(notification);
                    }
                });

                switch (options.Action)
                {
                case ModuleAction.Install:
                    _moduleInstaller.Install(moduleInfos, reportProgress);
                    break;

                case ModuleAction.Uninstall:
                    _moduleInstaller.Uninstall(moduleInfos, reportProgress);
                    break;
                }
            }
            catch (Exception ex)
            {
                notification.ProgressLog.Add(new ProgressMessage
                {
                    Level   = ProgressMessageLevel.Error,
                    Message = ex.ToString()
                });
            }
            finally
            {
                notification.Finished = DateTime.UtcNow;
                notification.ProgressLog.Add(new ProgressMessage
                {
                    Level   = ProgressMessageLevel.Info,
                    Message = "Installation finished.",
                });
                _pushNotifier.Send(notification);
            }
        }
        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));
        }