public async Task <ServiceResponseMessage> Create([FromForm] ScheduleInfo task)
        {
            if (!ModelState.IsValid)
            {
                return(ApiResponse(ResultStatus.ParamError, "非法提交参数"));
            }
            ScheduleEntity main = new ScheduleEntity
            {
                MetaType         = task.MetaType,
                CreateTime       = DateTime.Now,
                CronExpression   = task.CronExpression,
                EndDate          = task.EndDate,
                Remark           = task.Remark,
                StartDate        = task.StartDate,
                Title            = task.Title,
                Status           = (int)ScheduleStatus.Stop,
                CustomParamsJson = task.CustomParamsJson,
                RunLoop          = task.RunLoop,
                TotalRunCount    = 0,
                CreateUserName   = CurrentUserName,
                SourceApp        = task.SourceApp,
                Topic            = task.Topic,
                ContentKey       = task.ContentKey
            };

            if (task.MetaType == (int)ScheduleMetaType.Assembly)
            {
                main.AssemblyName = task.AssemblyName;
                main.ClassName    = task.ClassName;
            }
            ScheduleHttpOptionEntity httpOption = null;

            if (task.MetaType == (int)ScheduleMetaType.Http)
            {
                httpOption = new ScheduleHttpOptionEntity
                {
                    RequestUrl  = task.HttpRequestUrl,
                    Method      = task.HttpMethod,
                    ContentType = task.HttpContentType,
                    Headers     = task.HttpHeaders,
                    Body        = task.HttpBody
                };
            }
            var result = _scheduleService.Add(main, httpOption, task.Keepers, task.Nexts, task.Executors);

            if (result.Status == ResultStatus.Success)
            {
                if (task.RunNow)
                {
                    var start = await _scheduleService.Start(main);

                    return(ApiResponse(ResultStatus.Success, "任务创建成功!启动状态为:" + (start.Status == ResultStatus.Success ? "成功" : "失败"), result.Data));
                }
            }
            return(result);
        }
Beispiel #2
0
        public async Task <ActionResult> Create(ScheduleInfo task)
        {
            if (!ModelState.IsValid)
            {
                return(this.JsonNet(false, "数据验证失败!"));
            }
            var            admin = CurrentAdmin;
            ScheduleEntity main  = new ScheduleEntity
            {
                MetaType         = task.MetaType,
                CronExpression   = task.CronExpression,
                EndDate          = task.EndDate,
                Remark           = task.Remark,
                StartDate        = task.StartDate,
                Title            = task.Title,
                Status           = (int)ScheduleStatus.Stop,
                CustomParamsJson = task.CustomParamsJson,
                RunLoop          = task.RunLoop,
                TotalRunCount    = 0,
                CreateUserName   = admin.UserName
            };

            if (task.MetaType == (int)ScheduleMetaType.Assembly)
            {
                main.AssemblyName = task.AssemblyName;
                main.ClassName    = task.ClassName;
            }
            ScheduleHttpOptionEntity httpOption = null;

            if (task.MetaType == (int)ScheduleMetaType.Http)
            {
                httpOption = new ScheduleHttpOptionEntity
                {
                    RequestUrl  = task.HttpRequestUrl,
                    Method      = task.HttpMethod,
                    ContentType = task.HttpContentType,
                    Headers     = task.HttpHeaders,
                    Body        = task.HttpBody
                };
            }
            var result = _scheduleService.Add(main, httpOption, task.Keepers, task.Nexts, task.Executors);

            if (result.Status == ResultStatus.Success)
            {
                if (task.RunNow)
                {
                    var start = await _scheduleService.Start(main);

                    return(this.JsonNet(true, "任务创建成功!启动状态为:" + (start.Status == ResultStatus.Success ? "成功" : "失败"), Url.Action("Index")));
                }
                return(this.JsonNet(true, "任务创建成功!", Url.Action("Index")));
            }
            return(this.JsonNet(false, "任务创建失败!"));
        }
Beispiel #3
0
        // [ApiParamValidation]
        public ServiceResponseMessage Create([FromForm] ScheduleInfo task)
        {
            ScheduleEntity main = new ScheduleEntity
            {
                MetaType         = 1,
                CreateTime       = DateTime.Now,
                CronExpression   = task.CronExpression,
                EndDate          = task.EndDate,
                Remark           = task.Remark,
                StartDate        = task.StartDate,
                Title            = task.Title,
                Status           = (int)ScheduleStatus.Stop,
                CustomParamsJson = task.CustomParamsJson,
                RunLoop          = task.RunLoop,
                TotalRunCount    = 0,
                CreateUserName   = task.CreateUserName
            };

            if (task.MetaType == 1)
            {
                main.AssemblyName = task.AssemblyName;
                main.ClassName    = task.ClassName;
            }
            ScheduleHttpOptionEntity httpOption = null;

            if (task.MetaType == 2)
            {
                httpOption = new ScheduleHttpOptionEntity
                {
                    RequestUrl  = task.HttpRequestUrl,
                    Method      = task.HttpMethod,
                    ContentType = task.HttpContentType,
                    Headers     = task.HttpHeaders,
                    Body        = task.HttpBody
                };
            }
            var result = _scheduleService.Add(main, httpOption, task.Keepers, task.Nexts, task.Executors);

            if (result.Status == ResultStatus.Success)
            {
                if (task.RunNow)
                {
                    var start = _scheduleService.Start(main);
                    return(ApiResponse(ResultStatus.Success, "任务创建成功!启动状态为:" + (start.Status == ResultStatus.Success ? "成功" : "失败"), main.Id));
                }
            }
            return(result);
        }
        public HttpTask(ScheduleHttpOptionEntity httpOption)
        {
            if (httpOption != null)
            {
                _option = httpOption;

                _headers = HosScheduleFactory.ConvertParamsJson(httpOption.Headers);

                if (_headers.ContainsKey(HEADER_TIMEOUT) && int.TryParse(_headers[HEADER_TIMEOUT].ToString(), out int result) && result > 0)
                {
                    _timeout = TimeSpan.FromSeconds(result);
                }
                else
                {
                    int config = ConfigurationCache.GetField <int>("Http_RequestTimeout");
                    if (config > 0)
                    {
                        _timeout = TimeSpan.FromSeconds(config);
                    }
                }

                string requestBody = string.Empty;
                string url         = httpOption.RequestUrl;
                if (httpOption.ContentType == "application/json")
                {
                    requestBody = httpOption.Body?.Replace("\r\n", "");
                }
                else if (httpOption.ContentType == "application/x-www-form-urlencoded")
                {
                    var formData = HosScheduleFactory.ConvertParamsJson(httpOption.Body);
                    requestBody = string.Join('&', formData.Select(x => $"{x.Key}={System.Net.WebUtility.UrlEncode(x.Value.ToString())}"));
                    if (httpOption.Method.ToLower() == "get" && formData.Count > 0)
                    {
                        url = $"{httpOption.RequestUrl}?{requestBody}";
                    }
                }
                _option.RequestUrl = url;
                _option.Body       = requestBody;
            }
        }
Beispiel #5
0
        /// <summary>
        /// 添加一个任务
        /// </summary>
        /// <param name="model"></param>
        /// <param name="httpOption"></param>
        /// <param name="keepers"></param>
        /// <param name="nexts"></param>
        /// <param name="executors"></param>
        /// <returns></returns>
        public ServiceResponseMessage Add(ScheduleEntity model, ScheduleHttpOptionEntity httpOption, List <int> keepers, List <Guid> nexts, List <string> executors = null)
        {
            if (executors == null || !executors.Any())
            {
                //没有指定worker就根据权重选择2个
                executors = _nodeService.GetAvaliableWorkerByPriority(null, 2).Select(x => x.NodeName).ToList();
            }
            if (!executors.Any())
            {
                return(ServiceResult(ResultStatus.Failed, "没有可用节点!"));
            }
            model.CreateTime = DateTime.Now;
            var user = _repositoryFactory.SystemUsers.FirstOrDefault(x => x.UserName == model.CreateUserName);

            if (user != null)
            {
                model.CreateUserId = user.Id;
            }
            //保存主信息
            _repositoryFactory.Schedules.Add(model);
            //创建并保存任务锁
            _repositoryFactory.ScheduleLocks.Add(new ScheduleLockEntity {
                ScheduleId = model.Id, Status = 0
            });
            //保存http数据
            if (httpOption != null)
            {
                httpOption.ScheduleId = model.Id;
                _repositoryFactory.ScheduleHttpOptions.Add(httpOption);
            }
            //保存运行节点
            _repositoryFactory.ScheduleExecutors.AddRange(executors.Select(x => new ScheduleExecutorEntity
            {
                ScheduleId = model.Id,
                WorkerName = x
            }));
            //保存监护人
            if (keepers != null && keepers.Count > 0)
            {
                _repositoryFactory.ScheduleKeepers.AddRange(keepers.Select(x => new ScheduleKeeperEntity
                {
                    ScheduleId = model.Id,
                    UserId     = x
                }));
            }
            //保存子任务
            if (nexts != null && nexts.Count > 0)
            {
                _repositoryFactory.ScheduleReferences.AddRange(nexts.Select(x => new ScheduleReferenceEntity
                {
                    ScheduleId = model.Id,
                    ChildId    = x
                }));
            }
            //事务提交
            if (_unitOfWork.Commit() > 0)
            {
                return(ServiceResult(ResultStatus.Success, "任务创建成功!", model.Id));
            }
            return(ServiceResult(ResultStatus.Failed, "数据保存失败!"));
        }
        public async Task <ServiceResponseMessage> Create(ScheduleInfo task)
        {
            var main = new ScheduleEntity
            {
                MetaType         = task.MetaType,
                CronExpression   = task.CronExpression,
                EndDate          = task.EndDate,
                Remark           = task.Remark,
                StartDate        = task.StartDate,
                Title            = task.Title,
                Status           = (int)ScheduleStatus.Stop,
                CustomParamsJson = task.CustomParamsJson,
                RunLoop          = task.RunLoop,
                TotalRunCount    = 0,
                CreateUserName   = "******"
            };

            if (task.MetaType == (int)ScheduleMetaType.Assembly)
            {
                main.AssemblyName = task.AssemblyName;
                main.ClassName    = task.ClassName;
            }

            ScheduleHttpOptionEntity httpOption = null;

            if (task.MetaType == (int)ScheduleMetaType.Http)
            {
                httpOption = new ScheduleHttpOptionEntity
                {
                    RequestUrl  = task.HttpRequestUrl,
                    Method      = task.HttpMethod,
                    ContentType = task.HttpContentType,
                    Headers     = task.HttpHeaders,
                    Body        = task.HttpBody
                };
            }

            ServiceResponseMessage result;

            try
            {
                result = _scheduleService.Add(main, httpOption, task.Keepers, task.Nexts, task.Executors);
            }
            catch (Exception e)
            {
                return(ApiResponse(ResultStatus.Illegal, e.Message));
            }


            if (result.Status != ResultStatus.Success)
            {
                return(ApiResponse(ResultStatus.Failed, "创建任务失败"));
            }

            if (!task.RunNow)
            {
                return(ApiResponse(ResultStatus.Success, "任务创建成功!",
                                   data: new { Id = result.Data }));
            }

            var start = await _scheduleService.Start(main);

            var resp = ApiResponse(ResultStatus.Success,
                                   "任务创建成功!启动状态为:" + (start.Status == ResultStatus.Success ? "成功" : "失败"),
                                   data: new { Id = result.Data });

            return(resp);
        }