Exemple #1
0
        public async Task <ApiResult> Create(Job job)
        {
            // 参数验证
            var apiResult = ValidateProperties(job);

            if (apiResult != null)
            {
                return(apiResult);
            }

            if (await _store.GetJob(job.Name, job.Group) != null)
            {
                return(new ApiResult(ApiResult.Error, $"Job [{job.Name}, {job.Group}] exists"));
            }

            var node = await _sharding.GetShardingNode();

            if (node == null)
            {
                return(new ApiResult(ApiResult.Error, "Swarm cluster has no available node"));
            }

            var sched = _schedCache.GetOrCreate(node.SchedName, node.SchedInstanceId, node.Provider,
                                                node.ConnectionString);

            var qzJob   = job.ToQuartzJob();
            var trigger = TriggerFactory.Create(job.Trigger, job.Id, job.Properties);
            await sched.ScheduleJob(qzJob, trigger);

            job.SchedInstanceId = node.SchedInstanceId;
            job.SchedName       = node.SchedName;

            await _store.AddJob(job);

            if (string.IsNullOrWhiteSpace(job.Id))
            {
                await sched.DeleteJob(qzJob.Key);

                await sched.UnscheduleJob(trigger.Key);

                return(new ApiResult(ApiResult.DbError, "Save job failed"));
            }

            _logger.LogInformation(
                $"Create job {JsonConvert.SerializeObject(job)} success");
            return(new ApiResult(ApiResult.SuccessCode, null, job.Id));
        }
Exemple #2
0
        public async Task <IActionResult> GetJobInfo(string jobId)
        {
            if (string.IsNullOrWhiteSpace(jobId))
            {
                return(new JsonResult(new ApiResult(ApiResult.ModelNotValid, "The id is required")));
            }

            var job = await _store.GetJob(jobId);

            if (job == null)
            {
                return(new JsonResult(new ApiResult(ApiResult.ModelNotValid, $"Job {jobId} is not exists")));
            }

            var result = job.ToPropertyArray();

            return(new JsonResult(new ApiResult(ApiResult.SuccessCode, null, result)));
        }