Exemplo n.º 1
0
        private List <JobQueueViewModel> GetJbQueues(AccountViewModel account)
        {
            if (account == null || account.GroupSettingsId == null)
            {
                return(null);
            }

            var jobQueues = _jobQueueService.GetQueuesByAccountId(new JobQueueViewModel
            {
                AccountId = account.Id,
                IsForSpy  = false
            });

            var enabledGroupFunctions = new GetGroupFunctionsByGroupIdQueryHandler(new DataBaseContext()).Handle(
                new GetGroupFunctionsByGroupIdQuery
            {
                GroupId = (long)account.GroupSettingsId
            });

            var result = enabledGroupFunctions.Select(queue => new JobQueueViewModel
            {
                FunctionName       = queue,
                FunctionStringName = new GetFunctionNameByNameQueryHandler(new DataBaseContext()).Handle(new GetFunctionNameByNameQuery
                {
                    FunctionName = queue
                }),
                IsProcessed = jobQueues.Any(model => model.FunctionName == queue && model.IsProcessed)
            }).ToList();

            return(result);
        }
Exemplo n.º 2
0
        public List <FunctionViewModel> GetEnabledFunctionsByGroupId(long groupId)
        {
            var functions = new GetGroupFunctionsByGroupIdQueryHandler(new DataBaseContext()).Handle(new GetGroupFunctionsByGroupIdQuery
            {
                GroupId = groupId
            });

            var result = functions.Select(function => new FunctionViewModel
            {
                Name         = function.Name,
                FunctionName = function.FunctionName,
                FunctionId   = function.FunctionId
            }).ToList();

            return(result);
        }
Exemplo n.º 3
0
        public void SaveGroupFunctions(long groupId, List <long> funtions, IBackgroundJobService backgroundJobService)
        {
            var functionsIdForRun = new List <FunctionName>();

            var oldFuntions =
                new GetGroupFunctionsByGroupIdQueryHandler(new DataBaseContext()).Handle(new GetGroupFunctionsByGroupIdQuery
            {
                GroupId = groupId
            });

            new SaveGroupFunctionsCommandHandler(new DataBaseContext()).Handle(new SaveGroupFunctionsCommand
            {
                GroupId   = groupId,
                Functions = funtions
            });

            var newFunctions =
                new GetGroupFunctionsByGroupIdQueryHandler(new DataBaseContext()).Handle(new GetGroupFunctionsByGroupIdQuery
            {
                GroupId = groupId
            });

            foreach (var funtion in newFunctions)
            {
                if (oldFuntions.Any(data => data.FunctionId == funtion.FunctionId))
                {
                    continue;
                }

                if (oldFuntions.All(data => data.FunctionId != funtion.FunctionId))
                {
                    functionsIdForRun.Add(funtion.FunctionName);
                }
            }
            var accounts =
                new GetAccountsByGroupSettingsIdQueryHandler(new DataBaseContext()).Handle(new GetAccountsByGroupSettingsIdQuery
            {
                GroupSettingsId = groupId
            });

            var accountsViewModel = accounts.Select(model => new AccountViewModel
            {
                Id                        = model.Id,
                PageUrl                   = model.PageUrl,
                Login                     = model.Login,
                Password                  = model.Password,
                FacebookId                = model.FacebookId,
                Proxy                     = model.Proxy,
                ProxyLogin                = model.ProxyLogin,
                ProxyPassword             = model.ProxyPassword,
                Cookie                    = model.Cookie.CookieString,
                Name                      = model.Name,
                GroupSettingsId           = model.GroupSettingsId,
                AuthorizationDataIsFailed = model.AuthorizationDataIsFailed,
                ProxyDataIsFailed         = model.ProxyDataIsFailed,
                IsDeleted                 = model.IsDeleted,
                UserAgentId               = model.UserAgentId
            }).ToList();

            foreach (var accountModel in accountsViewModel)
            {
                //удаляем выключенные задачи
                foreach (var oldFuntion in oldFuntions)
                {
                    if (newFunctions.All(data => data.FunctionId != oldFuntion.FunctionId))
                    {
                        var stateList = _jobStateService.GetStatesByAccountAndFunctionName(new JobStateViewModel
                        {
                            AccountId    = accountModel.Id,
                            FunctionName = oldFuntion.FunctionName,
                            IsForSpy     = false
                        });

                        foreach (var state in stateList)
                        {
                            _jobStateService.DeleteJobState(state);

                            backgroundJobService.RemoveBackgroundJobById(state.JobId);
                        }
                    }
                }
                foreach (var function in functionsIdForRun)
                {
                    var delayTime = _settingsManager.GetTimeSpanByFunctionName(function, groupId);

                    var model = new CreateBackgroundJobModel
                    {
                        Account          = accountModel,
                        CheckPermissions = true,
                        FunctionName     = function,
                        LaunchTime       = delayTime
                    };

                    backgroundJobService.CreateBackgroundJob(model);
                }
            }
        }