Esempio n. 1
0
        /// <summary>
        /// 说明:触发任务执行
        ///------
        ///地址格式:{执行器内嵌服务跟地址}/run
        ///Header:
        ///    XXL-JOB-ACCESS-TOKEN : {请求令牌}
        ///请求数据格式如下,放置在 RequestBody 中,JSON格式:
        ///    {
        ///        "jobId":1,                                  // 任务ID
        ///        "executorHandler":"demoJobHandler",         // 任务标识
        ///        "executorParams":"demoJobHandler",          // 任务参数
        ///        "executorBlockStrategy":"COVER_EARLY",      // 任务阻塞策略,可选值参考 com.xxl.job.core.enums.ExecutorBlockStrategyEnum
        ///        "executorTimeout":0,                        // 任务超时时间,单位秒,大于零时生效
        ///        "logId":1,                                  // 本次调度日志ID
        ///        "logDateTime":1586629003729,                // 本次调度日志时间
        ///        "glueType":"BEAN",                          // 任务模式,可选值参考 com.xxl.job.core.glue.GlueTypeEnum
        ///        "glueSource":"xxx",                         // GLUE脚本代码
        ///        "glueUpdatetime":1586629003727,             // GLUE脚本更新时间,用于判定脚本是否变更以及是否需要刷新
        ///        "broadcastIndex":0,                         // 分片参数:当前分片
        ///        "broadcastTotal":0                          // 分片参数:总分片
        ///    }
        ///响应数据格式:
        ///    {
        ///    "code": 200,      // 200 表示正常、其他失败
        ///      "msg": null       // 错误提示消息
        ///    }
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <IActionResponse> ActionAsync(HttpContext context)
        {
            var jobInfo = await context.Request.FromHttpRequestBody <RunRequest>();

            // 如果有积压任务,丢弃当前任务
            if (jobInfo.ExecutorBlockStrategy == JobConstant.EXECUTORBLOCKSTRATEGY_DISCARD_LATER)
            {
                if (await _dispatcher.Exist(jobInfo.JobId))
                {
                    return(await context.ResponseAsync("终止任务[执行策略: DISCARD_LATER]".Fail()));
                }
            }
            // 覆盖之前调度负载之前积压的任务
            else if (jobInfo.ExecutorBlockStrategy == JobConstant.EXECUTORBLOCKSTRATEGY_COVER_EARLY)
            {
                if (await _dispatcher.Exist(jobInfo.JobId))
                {
                    await _dispatcher.Kill(jobInfo.JobId, "终止任务[执行策略: COVER_EARLY]");
                }
            }

            await _dispatcher.Enqueue(new JobMessage(jobInfo));

            // action
            return(await context.ResponseAsync(string.Empty.Success()));
        }
        public async Task <IHttpActionResult> Post(CrateDTO raw)
        {
            if (raw == null)
            {
                return(BadRequest("Crate object is not specified"));
            }
            var curCrateStandardEventReport = _crate.FromDto(raw);

            if (!curCrateStandardEventReport.IsOfType <EventReportCM>())
            {
                return(BadRequest("Crate object doesn't contain EventReportCM manifest"));
            }
            if (curCrateStandardEventReport.Get() == null)
            {
                return(BadRequest("Crate content is empty"));
            }
            var eventReportMS = curCrateStandardEventReport.Get <EventReportCM>();

            Logger.GetLogger().Info($"Crate {raw.Id} with incoming event '{eventReportMS.EventNames}' is received for external account '{eventReportMS.ExternalAccountId}'");
            if (eventReportMS.EventPayload == null)
            {
                return(BadRequest("EventReport can't have a null payload"));
            }
            if (string.IsNullOrEmpty(eventReportMS.ExternalAccountId) && string.IsNullOrEmpty(eventReportMS.ExternalDomainId))
            {
                return(BadRequest("EventReport can't have both ExternalAccountId and ExternalDomainId empty"));
            }
            _jobDispatcher.Enqueue(() => ProcessEventsInternal(raw));
            return(Ok());
        }
Esempio n. 3
0
        public void Enqueue(Guid curPlanId, params Crate[] curEventReport)
        {
            // We convert incoming data to DTO objects because HangFire will serialize method parameters into JSON and serializing of Crate objects is forbidden
            var curEventReportDTO = curEventReport.Select(x => CrateStorageSerializer.Default.ConvertToDto(x)).ToArray();

            // We don't await this call as it will be awaited inside HangFire after job is launched
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            _dispatcher.Enqueue(() => LaunchPlanCallback(curPlanId, curEventReportDTO));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        }