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()))); }
public async Task <IActionResult> Update(string jobId, [FromBody] Job value) { if (ModelState.IsValid) { if (string.IsNullOrWhiteSpace(jobId)) { return(new JsonResult(new ApiResult(ApiResult.Error, "Id is empty/null"))); } if (!await _store.CheckJobExists(jobId)) { return(new JsonResult(new ApiResult(ApiResult.Error, $"Job {jobId} not exists"))); } var qzJob = value.ToQuartzJob(); var trigger = TriggerFactory.Create(value.Trigger, jobId, value.Properties); // TODO: need test if need delete qzJob firstly? value.Id = jobId; await _store.UpdateJob(value); await _scheduler.UnscheduleJob(new TriggerKey(jobId)); await _scheduler.ScheduleJob(qzJob, trigger); return(new JsonResult(new ApiResult(ApiResult.SuccessCode, "success"))); } return(new JsonResult(new ApiResult(ApiResult.ModelNotValid, GetModelStateErrorMsg()))); }