Ejemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] Job value)
        {
            if (ModelState.IsValid)
            {
                var apiResult = ValidateProperties(value);
                if (apiResult != null)
                {
                    return(apiResult);
                }

                if (await _store.IsJobExists(value.Name, value.Group))
                {
                    return(new JsonResult(new ApiResult(ApiResult.Error, $"Job [{value.Name}, {value.Group}] exists")));
                }

                await _store.AddJob(value);

                if (string.IsNullOrWhiteSpace(value.Id))
                {
                    return(new JsonResult(new ApiResult(ApiResult.DbError, "Save job failed")));
                }

                var qzJob   = value.ToQuartzJob();
                var trigger = TriggerFactory.Create(value.Trigger, value.Id, value.Properties);

                await _scheduler.ScheduleJob(qzJob, trigger);

                _logger.LogInformation(
                    $"Create job {JsonConvert.SerializeObject(value)} success");
                return(new JsonResult(new ApiResult(ApiResult.SuccessCode, null, value.Id)));
            }

            return(new JsonResult(new ApiResult(ApiResult.ModelNotValid, GetModelStateErrorMsg())));
        }